From cca0f546860507fd6df1ecd5fed264fa58a0fde1 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 10:20:24 +0100 Subject: [PATCH 01/24] Generate mushrooms controller --- app/assets/javascripts/mushrooms.coffee | 3 +++ app/assets/stylesheets/mushrooms.scss | 3 +++ app/controllers/mushrooms_controller.rb | 2 ++ app/helpers/mushrooms_helper.rb | 2 ++ test/controllers/mushrooms_controller_test.rb | 7 +++++++ 5 files changed, 17 insertions(+) create mode 100644 app/assets/javascripts/mushrooms.coffee create mode 100644 app/assets/stylesheets/mushrooms.scss create mode 100644 app/controllers/mushrooms_controller.rb create mode 100644 app/helpers/mushrooms_helper.rb create mode 100644 test/controllers/mushrooms_controller_test.rb diff --git a/app/assets/javascripts/mushrooms.coffee b/app/assets/javascripts/mushrooms.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/mushrooms.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/mushrooms.scss b/app/assets/stylesheets/mushrooms.scss new file mode 100644 index 0000000..326df7f --- /dev/null +++ b/app/assets/stylesheets/mushrooms.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the mushrooms controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/mushrooms_controller.rb b/app/controllers/mushrooms_controller.rb new file mode 100644 index 0000000..014918b --- /dev/null +++ b/app/controllers/mushrooms_controller.rb @@ -0,0 +1,2 @@ +class MushroomsController < ApplicationController +end diff --git a/app/helpers/mushrooms_helper.rb b/app/helpers/mushrooms_helper.rb new file mode 100644 index 0000000..feaa968 --- /dev/null +++ b/app/helpers/mushrooms_helper.rb @@ -0,0 +1,2 @@ +module MushroomsHelper +end diff --git a/test/controllers/mushrooms_controller_test.rb b/test/controllers/mushrooms_controller_test.rb new file mode 100644 index 0000000..7afdc3a --- /dev/null +++ b/test/controllers/mushrooms_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class MushroomsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From a5682c96534c325eb251feccbc0988ebc5daa91d Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 10:57:50 +0100 Subject: [PATCH 02/24] Setup tabular display of mushrooms on the homepage --- app/controllers/mushrooms_controller.rb | 3 ++ app/views/mushrooms/_table.html.erb | 59 +++++++++++++++++++++++++ app/views/mushrooms/index.html.erb | 5 +++ config/routes.rb | 2 +- 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 app/views/mushrooms/_table.html.erb create mode 100644 app/views/mushrooms/index.html.erb diff --git a/app/controllers/mushrooms_controller.rb b/app/controllers/mushrooms_controller.rb index 014918b..ef4dd6f 100644 --- a/app/controllers/mushrooms_controller.rb +++ b/app/controllers/mushrooms_controller.rb @@ -1,2 +1,5 @@ class MushroomsController < ApplicationController + def index + @mushrooms = Mushroom.limit(10) + end end diff --git a/app/views/mushrooms/_table.html.erb b/app/views/mushrooms/_table.html.erb new file mode 100644 index 0000000..74749d8 --- /dev/null +++ b/app/views/mushrooms/_table.html.erb @@ -0,0 +1,59 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <% @mushrooms.each do |mushroom| %> + + + + + + + + + + + + + + + + + + + + + + + + + + <% end %> + +
ClassCap ShapeCap SurfaceCap ColorBruisesOdorGill AttachmentGill SpacingGill SizeGill ColorStalk ShapeStalk RootStalk Surface Above RingStalk Surface Below RingStalk Color Above RingStalk Color Below RingVeil TypeVeil ColorRing NumberRing TypeSpore Print ColorPopulationHabitat
<%= mushroom.mushroom_class %><%= mushroom.cap_shape %><%= mushroom.cap_surface %><%= mushroom.cap_color %><%= mushroom.bruises %><%= mushroom.odor %><%= mushroom.gill_attachment %><%= mushroom.gill_spacing %><%= mushroom.gill_size %><%= mushroom.gill_color %><%= mushroom.stalk_shape %><%= mushroom.stalk_root %><%= mushroom.stalk_surface_above_ring %><%= mushroom.stalk_surface_below_ring %><%= mushroom.stalk_color_above_ring %><%= mushroom.stalk_color_below_ring %><%= mushroom.veil_type %><%= mushroom.veil_color %><%= mushroom.ring_number %><%= mushroom.ring_type %><%= mushroom.spore_print_color %><%= mushroom.population %><%= mushroom.habitat %>
diff --git a/app/views/mushrooms/index.html.erb b/app/views/mushrooms/index.html.erb new file mode 100644 index 0000000..05b0cf0 --- /dev/null +++ b/app/views/mushrooms/index.html.erb @@ -0,0 +1,5 @@ +

Mushrooms

+ +
+ <%= render 'table' %> +
diff --git a/config/routes.rb b/config/routes.rb index 787824f..268d4a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,3 @@ Rails.application.routes.draw do - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + root 'mushrooms#index' end From 54378b91a597428da0bd39eb07ef60604c9b1895 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 12:27:01 +0100 Subject: [PATCH 03/24] Add row number --- app/views/mushrooms/_table.html.erb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/views/mushrooms/_table.html.erb b/app/views/mushrooms/_table.html.erb index 74749d8..a48a752 100644 --- a/app/views/mushrooms/_table.html.erb +++ b/app/views/mushrooms/_table.html.erb @@ -1,6 +1,7 @@ + @@ -28,8 +29,13 @@ + <% row_count = 0 %> + <% @mushrooms.each do |mushroom| %> + <% row_count += 1 %> + + From bc16a6dcda6441adb4132fd87b31905e0ffeb100 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 12:28:17 +0100 Subject: [PATCH 04/24] Use Materialize CSS and add stylings --- app/assets/stylesheets/application.css | 9 +++++++++ app/views/layouts/application.html.erb | 11 +++++++++-- app/views/mushrooms/index.html.erb | 7 +++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index d05ea0f..4a30427 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -13,3 +13,12 @@ *= require_tree . *= require_self */ + +.body-container { + padding: 20px; + padding-right: 50px; +} + +.table-container { + overflow: scroll; +} diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index ffeef56..d104f74 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,15 +1,22 @@ + Mushrooms <%= csrf_meta_tags %> <%= csp_meta_tag %> - <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> + + <%# Materialize CDN %> + + - <%= yield %> +
+ <%= yield %> +
diff --git a/app/views/mushrooms/index.html.erb b/app/views/mushrooms/index.html.erb index 05b0cf0..49ebb69 100644 --- a/app/views/mushrooms/index.html.erb +++ b/app/views/mushrooms/index.html.erb @@ -1,5 +1,8 @@

Mushrooms

-
- <%= render 'table' %> +
+
Filters
+
+ <%= render 'table' %> +
From 66721404c2329cd2300cb49e774b50ee26622783 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 15:10:57 +0100 Subject: [PATCH 05/24] Install jquery-rails gem --- Gemfile | 1 + Gemfile.lock | 5 +++++ app/assets/javascripts/application.js | 1 + 3 files changed, 7 insertions(+) diff --git a/Gemfile b/Gemfile index 9a5d051..35877da 100644 --- a/Gemfile +++ b/Gemfile @@ -8,6 +8,7 @@ gem 'pg', '>= 0.18', '< 2.0' gem 'puma', '~> 3.11' gem 'sass-rails', '~> 5.0' gem 'uglifier', '>= 1.3.0' +gem 'jquery-rails' gem 'coffee-rails', '~> 4.2' gem 'turbolinks', '~> 5' gem 'jbuilder', '~> 2.5' diff --git a/Gemfile.lock b/Gemfile.lock index a5b5a1d..a456625 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -87,6 +87,10 @@ GEM io-like (0.3.0) jbuilder (2.9.1) activesupport (>= 4.2.0) + jquery-rails (4.3.5) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -204,6 +208,7 @@ DEPENDENCIES chromedriver-helper coffee-rails (~> 4.2) jbuilder (~> 2.5) + jquery-rails listen (>= 3.0.5, < 3.2) pg (>= 0.18, < 2.0) pry diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 82e6f0f..de6a7de 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -10,6 +10,7 @@ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // +//= require jquery //= require rails-ujs //= require activestorage //= require turbolinks From 88df93df03519ba0ebee2e8d366624987e8d52c2 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 15:12:59 +0100 Subject: [PATCH 06/24] Move MushroomAttributesInfo to concerns --- {db => app/models/concerns}/mushroom_attributes_info.rb | 0 db/seeds.rb | 1 - 2 files changed, 1 deletion(-) rename {db => app/models/concerns}/mushroom_attributes_info.rb (100%) diff --git a/db/mushroom_attributes_info.rb b/app/models/concerns/mushroom_attributes_info.rb similarity index 100% rename from db/mushroom_attributes_info.rb rename to app/models/concerns/mushroom_attributes_info.rb diff --git a/db/seeds.rb b/db/seeds.rb index a817ba4..1497a52 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,4 @@ require 'csv' -require_relative 'mushroom_attributes_info' include MushroomAttributesInfo From a7ef6a460cfe2bf7437820757250ba601d089a96 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 15:13:34 +0100 Subject: [PATCH 07/24] Increase batch_size --- db/seeds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 1497a52..f29ea4a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -43,7 +43,7 @@ } ] - imported_mushrooms = Mushroom.import mushrooms, batch_size: 100 + imported_mushrooms = Mushroom.import mushrooms, batch_size: 1000 imported_mushrooms.failed_instances.each do |failure| failed_row_count += 1 From 896ba69093b941b79f465c8653875e003f3562b7 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 15:46:28 +0100 Subject: [PATCH 08/24] Add service to handle mushroom filtering - Add filter form - Load filter options for mushroom class attributes --- app/controllers/mushrooms_controller.rb | 7 ++++++- app/helpers/mushrooms_helper.rb | 14 ++++++++++++++ app/services/filter_mushroom.rb | 9 +++++++++ app/views/mushrooms/_filter_form.html.erb | 13 +++++++++++++ app/views/mushrooms/_filter_option.html.erb | 12 ++++++++++++ app/views/mushrooms/index.html.erb | 4 +++- 6 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 app/services/filter_mushroom.rb create mode 100644 app/views/mushrooms/_filter_form.html.erb create mode 100644 app/views/mushrooms/_filter_option.html.erb diff --git a/app/controllers/mushrooms_controller.rb b/app/controllers/mushrooms_controller.rb index ef4dd6f..9f57312 100644 --- a/app/controllers/mushrooms_controller.rb +++ b/app/controllers/mushrooms_controller.rb @@ -1,5 +1,10 @@ class MushroomsController < ApplicationController + def index - @mushrooms = Mushroom.limit(10) + @mushrooms = [] + + if params[:filter].present? + @mushrooms = FilterMushroom.new(params[:filter]).run + end end end diff --git a/app/helpers/mushrooms_helper.rb b/app/helpers/mushrooms_helper.rb index feaa968..dedf86b 100644 --- a/app/helpers/mushrooms_helper.rb +++ b/app/helpers/mushrooms_helper.rb @@ -1,2 +1,16 @@ module MushroomsHelper + include MushroomAttributesInfo + + def mushroom_attributes_values(key) + MUSHROOM_ATTRIBUTES[key].values + end + + def filter_selected?(key, value) + if params[:filter].blank? + '' + elsif params[:filter][key].include?(value) + 'selected' + else + end + end end diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb new file mode 100644 index 0000000..39b0778 --- /dev/null +++ b/app/services/filter_mushroom.rb @@ -0,0 +1,9 @@ +class FilterMushroom + def initialize(filter_params) + @mushroom_class = filter_params[:mushroom_class] + end + + def run + Mushroom.where(mushroom_class: @mushroom_class).limit(10) + end +end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb new file mode 100644 index 0000000..622f9d1 --- /dev/null +++ b/app/views/mushrooms/_filter_form.html.erb @@ -0,0 +1,13 @@ +<%= form_for :filter, url: root_path, method: 'GET', id: 'my-form' do |f| %> + <%# Mushroom class %> + <%= render 'filter_option', field: :mushroom_class, placeholder: 'Select class' %> + + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> + <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> +<% end %> + + diff --git a/app/views/mushrooms/_filter_option.html.erb b/app/views/mushrooms/_filter_option.html.erb new file mode 100644 index 0000000..4a5dc80 --- /dev/null +++ b/app/views/mushrooms/_filter_option.html.erb @@ -0,0 +1,12 @@ +
+ + + +
diff --git a/app/views/mushrooms/index.html.erb b/app/views/mushrooms/index.html.erb index 49ebb69..8256fae 100644 --- a/app/views/mushrooms/index.html.erb +++ b/app/views/mushrooms/index.html.erb @@ -1,7 +1,9 @@

Mushrooms

-
Filters
+
Filters + <%= render 'filter_form' %> +
<%= render 'table' %>
From 1a85af01f18afa8c3c1edcf52545cabaabd8f718 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 16:02:42 +0100 Subject: [PATCH 09/24] Render table if filter return results - Add stylings --- app/assets/stylesheets/application.css | 8 ++++++++ app/views/mushrooms/index.html.erb | 12 ++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 4a30427..a4d6f5a 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -22,3 +22,11 @@ .table-container { overflow: scroll; } + +.select-wrapper { + padding-top: 15px; +} + +label { + font-size: 1.5em !important; +} diff --git a/app/views/mushrooms/index.html.erb b/app/views/mushrooms/index.html.erb index 8256fae..0027553 100644 --- a/app/views/mushrooms/index.html.erb +++ b/app/views/mushrooms/index.html.erb @@ -1,10 +1,14 @@

Mushrooms

-
Filters +
+

Filters


<%= render 'filter_form' %>
-
- <%= render 'table' %> -
+ + <% if @mushrooms.present? %> +
+ <%= render 'table' %> +
+ <% end %>
From f935d04c47fff16423bf5ddf8176643818eca6c0 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 16:21:44 +0100 Subject: [PATCH 10/24] Fix error --- app/helpers/mushrooms_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/mushrooms_helper.rb b/app/helpers/mushrooms_helper.rb index dedf86b..4b3efde 100644 --- a/app/helpers/mushrooms_helper.rb +++ b/app/helpers/mushrooms_helper.rb @@ -8,7 +8,7 @@ def mushroom_attributes_values(key) def filter_selected?(key, value) if params[:filter].blank? '' - elsif params[:filter][key].include?(value) + elsif params[:filter][key]&.include?(value) 'selected' else end From f4f9ef95fa65260ac5cdc7858a1caf62a0ed9ba6 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 16:22:58 +0100 Subject: [PATCH 11/24] Add filter for cap-shape attribute --- app/services/filter_mushroom.rb | 4 +++- app/views/mushrooms/_filter_form.html.erb | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 39b0778..67f25a0 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -1,9 +1,11 @@ class FilterMushroom def initialize(filter_params) @mushroom_class = filter_params[:mushroom_class] + @cap_shape = filter_params[:cap_shape] end def run - Mushroom.where(mushroom_class: @mushroom_class).limit(10) + Mushroom.where(mushroom_class: @mushroom_class).limit(10) if @mushroom_class.present? + Mushroom.where(cap_shape: @cap_shape).limit(10) if @cap_shape.present? end end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 622f9d1..a950bf8 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -1,7 +1,10 @@ <%= form_for :filter, url: root_path, method: 'GET', id: 'my-form' do |f| %> - <%# Mushroom class %> + <%# mushroom class %> <%= render 'filter_option', field: :mushroom_class, placeholder: 'Select class' %> + <%# cap-shape %> + <%= render 'filter_option', field: :cap_shape, placeholder: 'Select cap shape' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From 532728747bd7ff38084b9029d42d86af1a76b833 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 17:13:38 +0100 Subject: [PATCH 12/24] Install kaminari for pagination --- Gemfile | 1 + Gemfile.lock | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/Gemfile b/Gemfile index 35877da..975cc50 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,7 @@ gem 'turbolinks', '~> 5' gem 'jbuilder', '~> 2.5' gem 'bootsnap', '>= 1.1.0', require: false gem 'activerecord-import' +gem 'kaminari' group :development, :test do gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] diff --git a/Gemfile.lock b/Gemfile.lock index a456625..f9d5bbf 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,6 +91,18 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -209,6 +221,7 @@ DEPENDENCIES coffee-rails (~> 4.2) jbuilder (~> 2.5) jquery-rails + kaminari listen (>= 3.0.5, < 3.2) pg (>= 0.18, < 2.0) pry From aa1c80d9097f6237ee7daacf1d28a62a270a8626 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:13:16 +0100 Subject: [PATCH 13/24] Paginate query results - Pass params to FilterMushroom service to access params[:page] - Update run method to take into account all queries --- app/controllers/mushrooms_controller.rb | 3 ++- app/services/filter_mushroom.rb | 18 +++++++++++++----- app/views/mushrooms/index.html.erb | 1 + 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/mushrooms_controller.rb b/app/controllers/mushrooms_controller.rb index 9f57312..878cc05 100644 --- a/app/controllers/mushrooms_controller.rb +++ b/app/controllers/mushrooms_controller.rb @@ -4,7 +4,8 @@ def index @mushrooms = [] if params[:filter].present? - @mushrooms = FilterMushroom.new(params[:filter]).run + # pass params to the service, so it can access params[:page] + @mushrooms = FilterMushroom.new(params).run end end end diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 67f25a0..9f4a216 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -1,11 +1,19 @@ class FilterMushroom - def initialize(filter_params) - @mushroom_class = filter_params[:mushroom_class] - @cap_shape = filter_params[:cap_shape] + attr_reader :params + + def initialize(params) + # pass params from the controller for paginate + @params = params + @mushroom_class = params[:filter][:mushroom_class] + @cap_shape = params[:filter][:cap_shape] end def run - Mushroom.where(mushroom_class: @mushroom_class).limit(10) if @mushroom_class.present? - Mushroom.where(cap_shape: @cap_shape).limit(10) if @cap_shape.present? + mushroom_ids = [] + + mushroom_ids << Mushroom.where(mushroom_class: @mushroom_class).pluck(:id) if @mushroom_class.present? + mushroom_ids << Mushroom.where(cap_shape: @cap_shape).pluck(:id) if @cap_shape.present? + + Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end end diff --git a/app/views/mushrooms/index.html.erb b/app/views/mushrooms/index.html.erb index 0027553..e09b328 100644 --- a/app/views/mushrooms/index.html.erb +++ b/app/views/mushrooms/index.html.erb @@ -9,6 +9,7 @@ <% if @mushrooms.present? %>
<%= render 'table' %> + <%= paginate @mushrooms %>
<% end %>
From 23cc19c06df7a8f3d3ebd41b7766708749b82a00 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:29:44 +0100 Subject: [PATCH 14/24] Add filter for cap_surface, cap_color, bruises and odor attributes --- app/services/filter_mushroom.rb | 8 ++++++++ app/views/mushrooms/_filter_form.html.erb | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 9f4a216..1b96533 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -6,6 +6,10 @@ def initialize(params) @params = params @mushroom_class = params[:filter][:mushroom_class] @cap_shape = params[:filter][:cap_shape] + @cap_surface = params[:filter][:cap_surface] + @cap_color = params[:filter][:cap_color] + @bruises = params[:filter][:bruises] + @odor = params[:filter][:odor] end def run @@ -13,6 +17,10 @@ def run mushroom_ids << Mushroom.where(mushroom_class: @mushroom_class).pluck(:id) if @mushroom_class.present? mushroom_ids << Mushroom.where(cap_shape: @cap_shape).pluck(:id) if @cap_shape.present? + mushroom_ids << Mushroom.where(cap_surface: @cap_surface).pluck(:id) if @cap_surface.present? + mushroom_ids << Mushroom.where(cap_color: @cap_color).pluck(:id) if @cap_color.present? + mushroom_ids << Mushroom.where(bruises: @bruises).pluck(:id) if @bruises.present? + mushroom_ids << Mushroom.where(odor: @odor).pluck(:id) if @odor.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index a950bf8..2873a96 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -5,6 +5,18 @@ <%# cap-shape %> <%= render 'filter_option', field: :cap_shape, placeholder: 'Select cap shape' %> + <%# cap-surface %> + <%= render 'filter_option', field: :cap_surface, placeholder: 'Select cap surface' %> + + <%# cap-color %> + <%= render 'filter_option', field: :cap_color, placeholder: 'Select cap color' %> + + <%# bruises %> + <%= render 'filter_option', field: :bruises, placeholder: 'Select bruises' %> + + <%# odor %> + <%= render 'filter_option', field: :odor, placeholder: 'Select odor' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From a8b79aed36839350bcdb44d12883965f1d730b25 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:34:32 +0100 Subject: [PATCH 15/24] Add filter for gill_attachment, gill_spacing and gill_size --- app/services/filter_mushroom.rb | 6 ++++++ app/views/mushrooms/_filter_form.html.erb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 1b96533..0474ecd 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -10,6 +10,9 @@ def initialize(params) @cap_color = params[:filter][:cap_color] @bruises = params[:filter][:bruises] @odor = params[:filter][:odor] + @gill_attachment = params[:filter][:gill_attachment] + @gill_spacing = params[:filter][:gill_spacing] + @gill_size = params[:filter][:gill_size] end def run @@ -21,6 +24,9 @@ def run mushroom_ids << Mushroom.where(cap_color: @cap_color).pluck(:id) if @cap_color.present? mushroom_ids << Mushroom.where(bruises: @bruises).pluck(:id) if @bruises.present? mushroom_ids << Mushroom.where(odor: @odor).pluck(:id) if @odor.present? + mushroom_ids << Mushroom.where(gill_attachment: @gill_attachment).pluck(:id) if @gill_attachment.present? + mushroom_ids << Mushroom.where(gill_spacing: @gill_spacing).pluck(:id) if @gill_spacing.present? + mushroom_ids << Mushroom.where(gill_size: @gill_size).pluck(:id) if @gill_size.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 2873a96..575ad8f 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -17,6 +17,15 @@ <%# odor %> <%= render 'filter_option', field: :odor, placeholder: 'Select odor' %> + <%# gill-attachment %> + <%= render 'filter_option', field: :gill_attachment, placeholder: 'Select gill attachment' %> + + <%# gill-spacing %> + <%= render 'filter_option', field: :gill_spacing, placeholder: 'Select gill spacing' %> + + <%# gill-size %> + <%= render 'filter_option', field: :gill_size, placeholder: 'Select gill size' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From a6d6623622341e534825cab615bd58bb66b30d87 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:38:45 +0100 Subject: [PATCH 16/24] Add filter for gill_color, stalk_shape and stalk_root --- app/services/filter_mushroom.rb | 6 ++++++ app/views/mushrooms/_filter_form.html.erb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 0474ecd..249c3ec 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -13,6 +13,9 @@ def initialize(params) @gill_attachment = params[:filter][:gill_attachment] @gill_spacing = params[:filter][:gill_spacing] @gill_size = params[:filter][:gill_size] + @gill_color = params[:filter][:gill_color] + @stalk_shape = params[:filter][:stalk_shape] + @stalk_root = params[:filter][:stalk_root] end def run @@ -27,6 +30,9 @@ def run mushroom_ids << Mushroom.where(gill_attachment: @gill_attachment).pluck(:id) if @gill_attachment.present? mushroom_ids << Mushroom.where(gill_spacing: @gill_spacing).pluck(:id) if @gill_spacing.present? mushroom_ids << Mushroom.where(gill_size: @gill_size).pluck(:id) if @gill_size.present? + mushroom_ids << Mushroom.where(gill_color: @gill_color).pluck(:id) if @gill_color.present? + mushroom_ids << Mushroom.where(stalk_shape: @stalk_shape).pluck(:id) if @stalk_shape.present? + mushroom_ids << Mushroom.where(stalk_root: @stalk_root).pluck(:id) if @stalk_root.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 575ad8f..955fbf0 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -26,6 +26,15 @@ <%# gill-size %> <%= render 'filter_option', field: :gill_size, placeholder: 'Select gill size' %> + <%# gill-color %> + <%= render 'filter_option', field: :gill_color, placeholder: 'Select gill color' %> + + <%# stalk-shape %> + <%= render 'filter_option', field: :stalk_shape, placeholder: 'Select stalk shape' %> + + <%# stalk-root %> + <%= render 'filter_option', field: :stalk_root, placeholder: 'Select stalk root' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From 748adbaa27fb39088f2a65f2a804c191bee88424 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:43:20 +0100 Subject: [PATCH 17/24] Add filter for stalk_surface_above_ring, stalk_surface_below_ring - and stalk_color_above_ring attributes --- app/services/filter_mushroom.rb | 6 ++++++ app/views/mushrooms/_filter_form.html.erb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 249c3ec..9a7d4fd 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -16,6 +16,9 @@ def initialize(params) @gill_color = params[:filter][:gill_color] @stalk_shape = params[:filter][:stalk_shape] @stalk_root = params[:filter][:stalk_root] + @stalk_surface_above_ring = params[:filter][:stalk_surface_above_ring] + @stalk_surface_below_ring = params[:filter][:stalk_surface_below_ring] + @stalk_color_above_ring = params[:filter][:stalk_color_above_ring] end def run @@ -33,6 +36,9 @@ def run mushroom_ids << Mushroom.where(gill_color: @gill_color).pluck(:id) if @gill_color.present? mushroom_ids << Mushroom.where(stalk_shape: @stalk_shape).pluck(:id) if @stalk_shape.present? mushroom_ids << Mushroom.where(stalk_root: @stalk_root).pluck(:id) if @stalk_root.present? + mushroom_ids << Mushroom.where(stalk_surface_above_ring: @stalk_surface_above_ring).pluck(:id) if @stalk_surface_above_ring.present? + mushroom_ids << Mushroom.where(stalk_surface_below_ring: @stalk_surface_below_ring).pluck(:id) if @stalk_surface_below_ring.present? + mushroom_ids << Mushroom.where(stalk_color_above_ring: @stalk_color_above_ring).pluck(:id) if @stalk_color_above_ring.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 955fbf0..0c05a1b 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -35,6 +35,15 @@ <%# stalk-root %> <%= render 'filter_option', field: :stalk_root, placeholder: 'Select stalk root' %> + <%# stalk-surface-above-ring %> + <%= render 'filter_option', field: :stalk_surface_above_ring, placeholder: 'Select stalk surface above ring' %> + + <%# stalk-surface-below-ring %> + <%= render 'filter_option', field: :stalk_surface_below_ring, placeholder: 'Select stalk surface below ring' %> + + <%# stalk-color-above-ring %> + <%= render 'filter_option', field: :stalk_color_above_ring, placeholder: 'Select stalk color above ring' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From 0e15071159f01bf6e8a691334283c3f1f5e70c53 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:49:02 +0100 Subject: [PATCH 18/24] Add filter for stalk_color_below_ring, veil_type and veil_color --- app/services/filter_mushroom.rb | 6 ++++++ app/views/mushrooms/_filter_form.html.erb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 9a7d4fd..2c0e2a8 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -19,6 +19,9 @@ def initialize(params) @stalk_surface_above_ring = params[:filter][:stalk_surface_above_ring] @stalk_surface_below_ring = params[:filter][:stalk_surface_below_ring] @stalk_color_above_ring = params[:filter][:stalk_color_above_ring] + @stalk_color_below_ring = params[:filter][:stalk_color_below_ring] + @veil_type = params[:filter][:veil_type] + @veil_color = params[:filter][:veil_color] end def run @@ -39,6 +42,9 @@ def run mushroom_ids << Mushroom.where(stalk_surface_above_ring: @stalk_surface_above_ring).pluck(:id) if @stalk_surface_above_ring.present? mushroom_ids << Mushroom.where(stalk_surface_below_ring: @stalk_surface_below_ring).pluck(:id) if @stalk_surface_below_ring.present? mushroom_ids << Mushroom.where(stalk_color_above_ring: @stalk_color_above_ring).pluck(:id) if @stalk_color_above_ring.present? + mushroom_ids << Mushroom.where(stalk_color_below_ring: @stalk_color_below_ring).pluck(:id) if @stalk_color_below_ring.present? + mushroom_ids << Mushroom.where(veil_type: @veil_type).pluck(:id) if @veil_type.present? + mushroom_ids << Mushroom.where(veil_color: @veil_color).pluck(:id) if @veil_color.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 0c05a1b..9cd908f 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -44,6 +44,15 @@ <%# stalk-color-above-ring %> <%= render 'filter_option', field: :stalk_color_above_ring, placeholder: 'Select stalk color above ring' %> + <%# stalk-color-below-ring %> + <%= render 'filter_option', field: :stalk_color_below_ring, placeholder: 'Select stalk color below ring' %> + + <%# veil-type %> + <%= render 'filter_option', field: :veil_type, placeholder: 'Select veil type' %> + + <%# veil-color %> + <%= render 'filter_option', field: :veil_color, placeholder: 'Select veil color' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From 4564eb86cb71afbba0068f16aa46cf8c070c6b56 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 18:53:41 +0100 Subject: [PATCH 19/24] Add filter for ring_number, ring_number and spore_print_color --- app/services/filter_mushroom.rb | 6 ++++++ app/views/mushrooms/_filter_form.html.erb | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 2c0e2a8..a2e2a94 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -22,6 +22,9 @@ def initialize(params) @stalk_color_below_ring = params[:filter][:stalk_color_below_ring] @veil_type = params[:filter][:veil_type] @veil_color = params[:filter][:veil_color] + @ring_number = params[:filter][:ring_number] + @ring_type = params[:filter][:ring_type] + @spore_print_color = params[:filter][:spore_print_color] end def run @@ -45,6 +48,9 @@ def run mushroom_ids << Mushroom.where(stalk_color_below_ring: @stalk_color_below_ring).pluck(:id) if @stalk_color_below_ring.present? mushroom_ids << Mushroom.where(veil_type: @veil_type).pluck(:id) if @veil_type.present? mushroom_ids << Mushroom.where(veil_color: @veil_color).pluck(:id) if @veil_color.present? + mushroom_ids << Mushroom.where(ring_number: @ring_number).pluck(:id) if @ring_number.present? + mushroom_ids << Mushroom.where(ring_type: @ring_type).pluck(:id) if @ring_type.present? + mushroom_ids << Mushroom.where(spore_print_color: @spore_print_color).pluck(:id) if @spore_print_color.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 9cd908f..5514e17 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -53,6 +53,15 @@ <%# veil-color %> <%= render 'filter_option', field: :veil_color, placeholder: 'Select veil color' %> + <%# ring-number %> + <%= render 'filter_option', field: :ring_number, placeholder: 'Select ring number' %> + + <%# ring-type %> + <%= render 'filter_option', field: :ring_type, placeholder: 'Select ring type' %> + + <%# spore-print-color %> + <%= render 'filter_option', field: :spore_print_color, placeholder: 'Select spore print color' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From 92b66343d39733cbea44f299f9e40f7b382bbd0e Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 19:00:17 +0100 Subject: [PATCH 20/24] Add filter for population and habitat attributes --- app/services/filter_mushroom.rb | 4 ++++ app/views/mushrooms/_filter_form.html.erb | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index a2e2a94..2b62e55 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -25,6 +25,8 @@ def initialize(params) @ring_number = params[:filter][:ring_number] @ring_type = params[:filter][:ring_type] @spore_print_color = params[:filter][:spore_print_color] + @population = params[:filter][:population] + @habitat = params[:filter][:habitat] end def run @@ -51,6 +53,8 @@ def run mushroom_ids << Mushroom.where(ring_number: @ring_number).pluck(:id) if @ring_number.present? mushroom_ids << Mushroom.where(ring_type: @ring_type).pluck(:id) if @ring_type.present? mushroom_ids << Mushroom.where(spore_print_color: @spore_print_color).pluck(:id) if @spore_print_color.present? + mushroom_ids << Mushroom.where(population: @population).pluck(:id) if @population.present? + mushroom_ids << Mushroom.where(habitat: @habitat).pluck(:id) if @habitat.present? Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) end diff --git a/app/views/mushrooms/_filter_form.html.erb b/app/views/mushrooms/_filter_form.html.erb index 5514e17..0f6bc6a 100644 --- a/app/views/mushrooms/_filter_form.html.erb +++ b/app/views/mushrooms/_filter_form.html.erb @@ -62,6 +62,12 @@ <%# spore-print-color %> <%= render 'filter_option', field: :spore_print_color, placeholder: 'Select spore print color' %> + <%# population %> + <%= render 'filter_option', field: :population, placeholder: 'Select population' %> + + <%# habitat %> + <%= render 'filter_option', field: :habitat, placeholder: 'Select habitat' %> + <%= f.submit 'Apply Filter', class: 'btn btn-large right' %> <%= link_to 'Reset', root_path, class: 'btn btn-large red reset' %> <% end %> From 4ef32654200f7ce109cc591e9c159a4a89b0304d Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 19:55:10 +0100 Subject: [PATCH 21/24] Update stylings --- app/assets/stylesheets/application.css | 20 +++++++++++++++++++- app/views/mushrooms/index.html.erb | 4 ++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index a4d6f5a..0959a46 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -19,10 +19,28 @@ padding-right: 50px; } -.table-container { +.table-container, .filters { + height: 100vh; overflow: scroll; } +thead th, .title { + position: sticky; + top: 0; +} + +th, td { + padding: 8px 16px; +} + +th, .title { + background:#eee; +} + +.title { + padding: 15px; +} + .select-wrapper { padding-top: 15px; } diff --git a/app/views/mushrooms/index.html.erb b/app/views/mushrooms/index.html.erb index e09b328..67a9426 100644 --- a/app/views/mushrooms/index.html.erb +++ b/app/views/mushrooms/index.html.erb @@ -1,8 +1,8 @@

Mushrooms

-
-

Filters


+
+

Filters


<%= render 'filter_form' %>
From 4c77d9776bf2000cea5e415d364e6a898856d392 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 21:02:18 +0100 Subject: [PATCH 22/24] Refactor params --- app/controllers/mushrooms_controller.rb | 3 +- app/services/filter_mushroom.rb | 55 ++++++++++++------------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/app/controllers/mushrooms_controller.rb b/app/controllers/mushrooms_controller.rb index 878cc05..3766922 100644 --- a/app/controllers/mushrooms_controller.rb +++ b/app/controllers/mushrooms_controller.rb @@ -4,8 +4,7 @@ def index @mushrooms = [] if params[:filter].present? - # pass params to the service, so it can access params[:page] - @mushrooms = FilterMushroom.new(params).run + @mushrooms = FilterMushroom.new(params[:filter], params[:page]).run end end end diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 2b62e55..527daf4 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -1,32 +1,31 @@ class FilterMushroom - attr_reader :params + attr_reader :page - def initialize(params) - # pass params from the controller for paginate - @params = params - @mushroom_class = params[:filter][:mushroom_class] - @cap_shape = params[:filter][:cap_shape] - @cap_surface = params[:filter][:cap_surface] - @cap_color = params[:filter][:cap_color] - @bruises = params[:filter][:bruises] - @odor = params[:filter][:odor] - @gill_attachment = params[:filter][:gill_attachment] - @gill_spacing = params[:filter][:gill_spacing] - @gill_size = params[:filter][:gill_size] - @gill_color = params[:filter][:gill_color] - @stalk_shape = params[:filter][:stalk_shape] - @stalk_root = params[:filter][:stalk_root] - @stalk_surface_above_ring = params[:filter][:stalk_surface_above_ring] - @stalk_surface_below_ring = params[:filter][:stalk_surface_below_ring] - @stalk_color_above_ring = params[:filter][:stalk_color_above_ring] - @stalk_color_below_ring = params[:filter][:stalk_color_below_ring] - @veil_type = params[:filter][:veil_type] - @veil_color = params[:filter][:veil_color] - @ring_number = params[:filter][:ring_number] - @ring_type = params[:filter][:ring_type] - @spore_print_color = params[:filter][:spore_print_color] - @population = params[:filter][:population] - @habitat = params[:filter][:habitat] + def initialize(filter_params, page) + @page = page + @mushroom_class = filter_params[:mushroom_class] + @cap_shape = filter_params[:cap_shape] + @cap_surface = filter_params[:cap_surface] + @cap_color = filter_params[:cap_color] + @bruises = filter_params[:bruises] + @odor = filter_params[:odor] + @gill_attachment = filter_params[:gill_attachment] + @gill_spacing = filter_params[:gill_spacing] + @gill_size = filter_params[:gill_size] + @gill_color = filter_params[:gill_color] + @stalk_shape = filter_params[:stalk_shape] + @stalk_root = filter_params[:stalk_root] + @stalk_surface_above_ring = filter_params[:stalk_surface_above_ring] + @stalk_surface_below_ring = filter_params[:stalk_surface_below_ring] + @stalk_color_above_ring = filter_params[:stalk_color_above_ring] + @stalk_color_below_ring = filter_params[:stalk_color_below_ring] + @veil_type = filter_params[:veil_type] + @veil_color = filter_params[:veil_color] + @ring_number = filter_params[:ring_number] + @ring_type = filter_params[:ring_type] + @spore_print_color = filter_params[:spore_print_color] + @population = filter_params[:population] + @habitat = filter_params[:habitat] end def run @@ -56,6 +55,6 @@ def run mushroom_ids << Mushroom.where(population: @population).pluck(:id) if @population.present? mushroom_ids << Mushroom.where(habitat: @habitat).pluck(:id) if @habitat.present? - Mushroom.where(id: mushroom_ids.flatten).page(params[:page]) + Mushroom.where(id: mushroom_ids.flatten).page(page) end end From 3ebea23785cc44ed025db3d86ca0e4c5939b55a1 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 21:50:20 +0100 Subject: [PATCH 23/24] Refactor query --- app/services/filter_mushroom.rb | 50 ++++++++++++++++----------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/app/services/filter_mushroom.rb b/app/services/filter_mushroom.rb index 527daf4..8944911 100644 --- a/app/services/filter_mushroom.rb +++ b/app/services/filter_mushroom.rb @@ -29,32 +29,32 @@ def initialize(filter_params, page) end def run - mushroom_ids = [] + query = Mushroom.all - mushroom_ids << Mushroom.where(mushroom_class: @mushroom_class).pluck(:id) if @mushroom_class.present? - mushroom_ids << Mushroom.where(cap_shape: @cap_shape).pluck(:id) if @cap_shape.present? - mushroom_ids << Mushroom.where(cap_surface: @cap_surface).pluck(:id) if @cap_surface.present? - mushroom_ids << Mushroom.where(cap_color: @cap_color).pluck(:id) if @cap_color.present? - mushroom_ids << Mushroom.where(bruises: @bruises).pluck(:id) if @bruises.present? - mushroom_ids << Mushroom.where(odor: @odor).pluck(:id) if @odor.present? - mushroom_ids << Mushroom.where(gill_attachment: @gill_attachment).pluck(:id) if @gill_attachment.present? - mushroom_ids << Mushroom.where(gill_spacing: @gill_spacing).pluck(:id) if @gill_spacing.present? - mushroom_ids << Mushroom.where(gill_size: @gill_size).pluck(:id) if @gill_size.present? - mushroom_ids << Mushroom.where(gill_color: @gill_color).pluck(:id) if @gill_color.present? - mushroom_ids << Mushroom.where(stalk_shape: @stalk_shape).pluck(:id) if @stalk_shape.present? - mushroom_ids << Mushroom.where(stalk_root: @stalk_root).pluck(:id) if @stalk_root.present? - mushroom_ids << Mushroom.where(stalk_surface_above_ring: @stalk_surface_above_ring).pluck(:id) if @stalk_surface_above_ring.present? - mushroom_ids << Mushroom.where(stalk_surface_below_ring: @stalk_surface_below_ring).pluck(:id) if @stalk_surface_below_ring.present? - mushroom_ids << Mushroom.where(stalk_color_above_ring: @stalk_color_above_ring).pluck(:id) if @stalk_color_above_ring.present? - mushroom_ids << Mushroom.where(stalk_color_below_ring: @stalk_color_below_ring).pluck(:id) if @stalk_color_below_ring.present? - mushroom_ids << Mushroom.where(veil_type: @veil_type).pluck(:id) if @veil_type.present? - mushroom_ids << Mushroom.where(veil_color: @veil_color).pluck(:id) if @veil_color.present? - mushroom_ids << Mushroom.where(ring_number: @ring_number).pluck(:id) if @ring_number.present? - mushroom_ids << Mushroom.where(ring_type: @ring_type).pluck(:id) if @ring_type.present? - mushroom_ids << Mushroom.where(spore_print_color: @spore_print_color).pluck(:id) if @spore_print_color.present? - mushroom_ids << Mushroom.where(population: @population).pluck(:id) if @population.present? - mushroom_ids << Mushroom.where(habitat: @habitat).pluck(:id) if @habitat.present? + query = query.where(mushroom_class: @mushroom_class) if @mushroom_class.present? + query = query.where(cap_shape: @cap_shape) if @cap_shape.present? + query = query.where(cap_surface: @cap_surface) if @cap_surface.present? + query = query.where(cap_color: @cap_color) if @cap_color.present? + query = query.where(bruises: @bruises) if @bruises.present? + query = query.where(odor: @odor) if @odor.present? + query = query.where(gill_attachment: @gill_attachment) if @gill_attachment.present? + query = query.where(gill_spacing: @gill_spacing) if @gill_spacing.present? + query = query.where(gill_size: @gill_size) if @gill_size.present? + query = query.where(gill_color: @gill_color) if @gill_color.present? + query = query.where(stalk_shape: @stalk_shape) if @stalk_shape.present? + query = query.where(stalk_root: @stalk_root) if @stalk_root.present? + query = query.where(stalk_surface_above_ring: @stalk_surface_above_ring) if @stalk_surface_above_ring.present? + query = query.where(stalk_surface_below_ring: @stalk_surface_below_ring) if @stalk_surface_below_ring.present? + query = query.where(stalk_color_above_ring: @stalk_color_above_ring) if @stalk_color_above_ring.present? + query = query.where(stalk_color_below_ring: @stalk_color_below_ring) if @stalk_color_below_ring.present? + query = query.where(veil_type: @veil_type) if @veil_type.present? + query = query.where(veil_color: @veil_color) if @veil_color.present? + query = query.where(ring_number: @ring_number) if @ring_number.present? + query = query.where(ring_type: @ring_type) if @ring_type.present? + query = query.where(spore_print_color: @spore_print_color) if @spore_print_color.present? + query = query.where(population: @population) if @population.present? + query = query.where(habitat: @habitat) if @habitat.present? - Mushroom.where(id: mushroom_ids.flatten).page(page) + query.page(page) end end From fec7f557888c4c8ec1e03fbab4065a47e2d4b2d8 Mon Sep 17 00:00:00 2001 From: Halimat Mercy Oseni Date: Sat, 27 Jul 2019 21:50:54 +0100 Subject: [PATCH 24/24] Compute row count based on pagination details --- app/views/mushrooms/_table.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/mushrooms/_table.html.erb b/app/views/mushrooms/_table.html.erb index a48a752..4413f73 100644 --- a/app/views/mushrooms/_table.html.erb +++ b/app/views/mushrooms/_table.html.erb @@ -29,7 +29,7 @@
- <% row_count = 0 %> + <% row_count = @mushrooms.limit_value * (@mushrooms.current_page - 1)%> <% @mushrooms.each do |mushroom| %> <% row_count += 1 %>
# Class Cap Shape Cap Surface
<%= row_count %> <%= mushroom.mushroom_class %> <%= mushroom.cap_shape %> <%= mushroom.cap_surface %>