Skip to content

Commit

Permalink
Merge pull request #1 from mercyoseni/feature-filter
Browse files Browse the repository at this point in the history
Add functionality to filter mushrooms
  • Loading branch information
mercyoseni authored Jul 27, 2019
2 parents 8c5bed9 + fec7f55 commit ed98305
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ 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'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'activerecord-import'
gem 'kaminari'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
18 changes: 18 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ 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)
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)
Expand Down Expand Up @@ -204,6 +220,8 @@ DEPENDENCIES
chromedriver-helper
coffee-rails (~> 4.2)
jbuilder (~> 2.5)
jquery-rails
kaminari
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/mushrooms.coffee
Original file line number Diff line number Diff line change
@@ -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/
35 changes: 35 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,38 @@
*= require_tree .
*= require_self
*/

.body-container {
padding: 20px;
padding-right: 50px;
}

.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;
}

label {
font-size: 1.5em !important;
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/mushrooms.scss
Original file line number Diff line number Diff line change
@@ -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/
10 changes: 10 additions & 0 deletions app/controllers/mushrooms_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class MushroomsController < ApplicationController

def index
@mushrooms = []

if params[:filter].present?
@mushrooms = FilterMushroom.new(params[:filter], params[:page]).run
end
end
end
16 changes: 16 additions & 0 deletions app/helpers/mushrooms_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +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
File renamed without changes.
60 changes: 60 additions & 0 deletions app/services/filter_mushroom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class FilterMushroom
attr_reader :page

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
query = Mushroom.all

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?

query.page(page)
end
end
11 changes: 9 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>Mushrooms</title>
<%= 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 %>
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css'>
<script src='https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js'></script>
</head>

<body>
<%= yield %>
<div class='body-container'>
<%= yield %>
</div>
</body>
</html>
79 changes: 79 additions & 0 deletions app/views/mushrooms/_filter_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<%= form_for :filter, url: root_path, method: 'GET', id: 'my-form' do |f| %>
<%# mushroom class %>
<%= render 'filter_option', field: :mushroom_class, placeholder: 'Select class' %>

<%# 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' %>

<%# 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' %>

<%# 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' %>

<%# 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' %>

<%# 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' %>

<%# 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' %>

<%# 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 %>

<script>
$(document).ready(function(){
$('select').formSelect();
});
</script>
12 changes: 12 additions & 0 deletions app/views/mushrooms/_filter_option.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class='input-field col s12'>
<select name='filter[<%= field.to_s %>][]' multiple>
<option value='' disabled><%= placeholder %></option>
<% mushroom_attributes_values(field).each do |value| %>
<option <%= filter_selected?(field, value) %> value=<%= value %>>
<%= value %>
</option>
<% end %>
</select>

<label><%= field.to_s.titleize %></label>
</div>
65 changes: 65 additions & 0 deletions app/views/mushrooms/_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<table>
<thead>
<tr>
<th>#</th>
<th>Class</th>
<th>Cap Shape</th>
<th>Cap Surface</th>
<th>Cap Color</th>
<th>Bruises</th>
<th>Odor</th>
<th>Gill Attachment</th>
<th>Gill Spacing</th>
<th>Gill Size</th>
<th>Gill Color</th>
<th>Stalk Shape</th>
<th>Stalk Root</th>
<th>Stalk Surface Above Ring</th>
<th>Stalk Surface Below Ring</th>
<th>Stalk Color Above Ring</th>
<th>Stalk Color Below Ring</th>
<th>Veil Type</th>
<th>Veil Color</th>
<th>Ring Number</th>
<th>Ring Type</th>
<th>Spore Print Color</th>
<th>Population</th>
<th>Habitat</th>
</tr>
</thead>

<tbody>
<% row_count = @mushrooms.limit_value * (@mushrooms.current_page - 1)%>

<% @mushrooms.each do |mushroom| %>
<% row_count += 1 %>

<tr>
<th><%= row_count %></th>
<td><%= mushroom.mushroom_class %></td>
<td><%= mushroom.cap_shape %></td>
<td><%= mushroom.cap_surface %></td>
<td><%= mushroom.cap_color %></td>
<td><%= mushroom.bruises %></td>
<td><%= mushroom.odor %></td>
<td><%= mushroom.gill_attachment %></td>
<td><%= mushroom.gill_spacing %></td>
<td><%= mushroom.gill_size %></td>
<td><%= mushroom.gill_color %></td>
<td><%= mushroom.stalk_shape %></td>
<td><%= mushroom.stalk_root %></td>
<td><%= mushroom.stalk_surface_above_ring %></td>
<td><%= mushroom.stalk_surface_below_ring %></td>
<td><%= mushroom.stalk_color_above_ring %></td>
<td><%= mushroom.stalk_color_below_ring %></td>
<td><%= mushroom.veil_type %></td>
<td><%= mushroom.veil_color %></td>
<td><%= mushroom.ring_number %></td>
<td><%= mushroom.ring_type %></td>
<td><%= mushroom.spore_print_color %></td>
<td><%= mushroom.population %></td>
<td><%= mushroom.habitat %></td>
</tr>
<% end %>
</tbody>
</table>
15 changes: 15 additions & 0 deletions app/views/mushrooms/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h3>Mushrooms</h3>

<div class='row'>
<div class='col s3 filters'>
<div class='title'><h4>Filters<h4></div><hr>
<%= render 'filter_form' %>
</div>

<% if @mushrooms.present? %>
<div class='table-container col s9'>
<%= render 'table' %>
<%= paginate @mushrooms %>
</div>
<% end %>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit ed98305

Please sign in to comment.