From bcf38b6c693d0d5da242bdc0b5d272a665fcedb6 Mon Sep 17 00:00:00 2001 From: Sachin Maharjan Date: Thu, 14 May 2015 03:46:26 -0700 Subject: [PATCH 1/3] Search for donors with blood type and pagination --- Gemfile | 2 +- Gemfile.lock | 2 + app/assets/javascripts/home.js | 12 ++++ app/assets/stylesheets/home.css.erb | 91 +++++++++++++++++++++++++++++ app/controllers/home_controller.rb | 8 +++ app/models/donor.rb | 9 +++ app/views/home/_donors.html.erb | 11 ++++ app/views/home/search.html.erb | 25 ++++++++ app/views/home/search.js.erb | 1 + config/routes.rb | 1 + 10 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 app/views/home/_donors.html.erb create mode 100644 app/views/home/search.html.erb create mode 100644 app/views/home/search.js.erb diff --git a/Gemfile b/Gemfile index c5f0f11..cb5330d 100644 --- a/Gemfile +++ b/Gemfile @@ -19,6 +19,7 @@ gem 'geocoder' gem 'jquery-turbolinks' gem 'simple_form' gem 'meta-tags' +gem 'will_paginate', '~> 3.0.5' group :production do gem 'rails_12factor' @@ -34,4 +35,3 @@ group :development, :test do gem 'faker' gem 'railroady' end - diff --git a/Gemfile.lock b/Gemfile.lock index 98020ba..e2a503a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -180,6 +180,7 @@ GEM binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) + will_paginate (3.0.7) PLATFORMS ruby @@ -210,3 +211,4 @@ DEPENDENCIES turbolinks uglifier (>= 1.3.0) web-console (~> 2.0) + will_paginate (~> 3.0.5) diff --git a/app/assets/javascripts/home.js b/app/assets/javascripts/home.js index 7aa14ac..dd97480 100644 --- a/app/assets/javascripts/home.js +++ b/app/assets/javascripts/home.js @@ -15,3 +15,15 @@ //= require jquery_ujs //= require vendor/smooth-scroll.js //= require vendor/share.min.js + +$(function() { + $("#donors .pagination a").bind("click", function() { + $.getScript(this.href); + return false; + }); + + $("#donor_search").keyup(function() { + $.get($("#donor_search").attr("action"), $("#donor_search").serialize(), null, "script"); + return false; + }); +}); diff --git a/app/assets/stylesheets/home.css.erb b/app/assets/stylesheets/home.css.erb index 9f30333..27eb6bc 100644 --- a/app/assets/stylesheets/home.css.erb +++ b/app/assets/stylesheets/home.css.erb @@ -1,6 +1,7 @@ /* *= require_self *= require vendor/share.css + *= require vendor/skeleton.css */ html, body{ @@ -125,3 +126,93 @@ p{ .share-buttons { width: 60%; margin: 0 auto; } + + +.search-results { + padding-top: 20px; + height: 100%; + min-height: 600px; + background-color: #f7f7f7; +} + +.search-results .donor { + padding-left: 30px; + text-align: left; + color: white !important; + border-bottom: 1px solid #dbdbdb; + padding: 10px 30px; +} + +.search-results .donor h1 { + padding: 0px; + color: #eb5d49; + font-size: 30px; + margin-bottom: 0px; +} + +.search-results .donor p { + font-size: 16px; + padding: 0px; + margin: 0px; + + color: #333333 +} + +.search-from { + padding-top: 80px; + text-align: center; + margin-bottom: -15px; +} + +.search-from select { + margin: 0px; + padding: 7px 10px; + vertical-align: middle; + font-size: 25px; + height: 50px; + width: 225px; +} + +.search-from .btn { + padding: 0px 10px; + text-decoration: none; + background-color:#8A0707; + color: white; + text-transform: uppercase; + height: 50px; + font-size: 15px; + width: 225px; +} + +div.inline { + display: inline-block; +} + +.search-paginate { + padding: 15px; +} +.pagination a, .pagination span, .pagination em { + padding: 0.2em 0.5em; + display: block; + float: left; + margin-right: 1px; +} + +.pagination .disabled { + color: #999999; + border: 1px solid #dddddd; +} + +.pagination .current { + font-style: normal; + font-weight: bold; + background: #2e6ab1; + color: white; + border: 1px solid #2e6ab1; +} + +.pagination a { + text-decoration: none; + color: #105cb6; + border: 1px solid #9aafe5; +} diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 95f2992..32e852e 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,4 +1,12 @@ class HomeController < ApplicationController + ITEMS_PER_PAGE = 20 + def index + + end + + def search + flash[:warning] = "You must specify some search criteria." if !params.has_key?(:bloodtype) + @donors = Donor.search(params[:bloodtype]).order("created_at ASC").paginate(:page => params[:page], :per_page => ITEMS_PER_PAGE) end end diff --git a/app/models/donor.rb b/app/models/donor.rb index 51d5cfd..943ef5b 100644 --- a/app/models/donor.rb +++ b/app/models/donor.rb @@ -57,4 +57,13 @@ def nepal_cellphone def convert_km_to_mile self.commute_radius = self.commute_radius.to_f * 0.621371 end + + def self.search(search) + if search + self.where(blood_type: search) + else + self.all + end + end + end diff --git a/app/views/home/_donors.html.erb b/app/views/home/_donors.html.erb new file mode 100644 index 0000000..14d1217 --- /dev/null +++ b/app/views/home/_donors.html.erb @@ -0,0 +1,11 @@ +<% @donors.each do |donor| %> +
+

<%= donor.full_name %>

+

Address: <%= donor.address %>

+

Cell Phone: <%= donor.cell_phone %>

+

Blood Type: <%= donor.blood_type.titleize %>

+
+<% end %> +
+ <%= will_paginate @donors, class: "pagination" %> +
diff --git a/app/views/home/search.html.erb b/app/views/home/search.html.erb new file mode 100644 index 0000000..261c2e2 --- /dev/null +++ b/app/views/home/search.html.erb @@ -0,0 +1,25 @@ + +
+ +
+ + +<%= form_tag("/search", :method => "get", class: 'search-from') do %> +
+
+ <%= select_tag :bloodtype, options_for_select(Donor.blood_type.values), class: 'select form-control'%> +
+
+ <%= submit_tag "Search for Donors", :name => nil, class: "btn btn-default btn-lg btn-block" %> +
+
+<% end %> +
+ +
+ <% if @donors.count == 0 %> +

No Donors Found.

+ <% else %> + <%= render 'donors' %> + <% end %> +
diff --git a/app/views/home/search.js.erb b/app/views/home/search.js.erb new file mode 100644 index 0000000..9e0956f --- /dev/null +++ b/app/views/home/search.js.erb @@ -0,0 +1 @@ +$("#donors").html("<%= escape_javascript(render("donors")) %>"); diff --git a/config/routes.rb b/config/routes.rb index a8d2328..bbbe689 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,5 +4,6 @@ resources :hospital, only: [:index, :new, :create, :show, :update] get 'hospital/:id/blood-status', to: 'hospital#edit', as: :hospital_blood_status + get '/search', to: 'home#search' match '/admin' => DelayedJobWeb, :anchor => false, via: [:get, :post] end From 4ff7be0f6493799076d1d0ee91fbf90d73a544a1 Mon Sep 17 00:00:00 2001 From: Sachin Maharjan Date: Thu, 14 May 2015 03:51:58 -0700 Subject: [PATCH 2/3] Added email --- app/views/home/_donors.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/home/_donors.html.erb b/app/views/home/_donors.html.erb index 14d1217..aa9892a 100644 --- a/app/views/home/_donors.html.erb +++ b/app/views/home/_donors.html.erb @@ -1,6 +1,7 @@ <% @donors.each do |donor| %>

<%= donor.full_name %>

+

Email: <%= donor.email %>

Address: <%= donor.address %>

Cell Phone: <%= donor.cell_phone %>

Blood Type: <%= donor.blood_type.titleize %>

From 6ccfa8823d18e92511bd343ef812e3d35528c663 Mon Sep 17 00:00:00 2001 From: Sachin Maharjan Date: Thu, 14 May 2015 03:56:14 -0700 Subject: [PATCH 3/3] Add radious --- app/views/home/_donors.html.erb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/views/home/_donors.html.erb b/app/views/home/_donors.html.erb index aa9892a..0819a71 100644 --- a/app/views/home/_donors.html.erb +++ b/app/views/home/_donors.html.erb @@ -1,10 +1,11 @@ <% @donors.each do |donor| %>

<%= donor.full_name %>

-

Email: <%= donor.email %>

+

Blood Type: <%= donor.blood_type.titleize %>

+

Radius(KM): <%= donor.commute_radius %>

Address: <%= donor.address %>

-

Cell Phone: <%= donor.cell_phone %>

-

Blood Type: <%= donor.blood_type.titleize %>

+

Email: <%= donor.email %>

+

Cell Phone: <%= donor.cell_phone %>

<% end %>