-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from mercyoseni/feature-filter
Add functionality to filter mushrooms
- Loading branch information
Showing
18 changed files
with
337 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.