From 2ebbe9a3065b76ef39fba2d4767eca7882fd604f Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Thu, 21 Nov 2019 01:28:03 +0100 Subject: [PATCH] Added quantity filtering by domain --- app/controllers/ingredients_controller.rb | 13 ++++++--- app/controllers/quantities_controller.rb | 33 ++++++++++++++++------- app/helpers/ingredients_helper.rb | 2 +- app/helpers/quantities_helper.rb | 6 ++++- app/models/ingredient.rb | 2 +- app/models/quantity.rb | 10 +++++++ app/views/ingredients/_filters.html.erb | 25 ++++++++--------- app/views/quantities/_filters.html.erb | 27 +++++++++++++++++++ app/views/quantities/_list.html.erb | 3 +++ config/locales/en.yml | 4 +++ config/routes.rb | 1 + init.rb | 2 +- 12 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 app/views/quantities/_filters.html.erb diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index abb03e5..3e02008 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,6 +1,7 @@ class IngredientsController < ApplicationController require 'csv' + before_action :init_session_filters before_action :find_project_by_project_id, only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients] before_action :find_quantity, only: [:toggle_nutrient_column] @@ -54,13 +55,13 @@ class IngredientsController < ApplicationController end def filter - session[:filters] = params[:filters] + session[:i_filters] = params[:filters] prepare_ingredients render :toggle end def filter_nutrients - session[:filters] = params[:filters] + session[:i_filters] = params[:filters] prepare_nutrients render :toggle_nutrient_column end @@ -167,6 +168,10 @@ class IngredientsController < ApplicationController private + def init_session_filters + session[:i_filters] ||= {} + end + def ingredient_params params.require(:ingredient).permit( :name, @@ -197,13 +202,13 @@ class IngredientsController < ApplicationController def prepare_ingredients @ingredients, @formula_q = @project.ingredients.includes(:ref_unit, :source) - .filter(@project, session[:filters]) + .filter(@project, session[:i_filters]) end def prepare_nutrients @quantities = @project.quantities.where(primary: true) ingredients, requested_n, extra_n, @formula_q = @project.ingredients - .filter(@project, session[:filters], @quantities) + .filter(@project, session[:i_filters], @quantities) @nutrients = {} @extra_nutrients = {} diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index 03db0fb..238a931 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -1,11 +1,12 @@ class QuantitiesController < ApplicationController - before_action :find_project_by_project_id, only: [:index, :create] + before_action :init_session_filters + before_action :find_project_by_project_id, only: [:index, :create, :filter] before_action :find_quantity, only: [:destroy, :toggle, :up, :down, :left, :right] before_action :authorize def index @quantity = @project.quantities.new - @quantities = @project.quantities + prepare_quantities end def create @@ -14,7 +15,7 @@ class QuantitiesController < ApplicationController flash[:notice] = 'Created new quantity' redirect_to project_quantities_url(@project) else - @quantities = @project.quantities + prepare_quantities render :index end end @@ -23,41 +24,51 @@ class QuantitiesController < ApplicationController if @quantity.destroy flash[:notice] = 'Deleted quantity' end - @quantities = @project.quantities + prepare_quantities render :toggle end def toggle @quantity.toggle_primary! - @quantities = @project.quantities + prepare_quantities + end + + def filter + session[:q_filters] = params[:filters] + prepare_quantities + render :toggle end def up @quantity.move_left if @quantity.left_sibling.present? - @quantities = @project.quantities + prepare_quantities render :toggle end def down @quantity.move_right if @quantity.right_sibling.present? - @quantities = @project.quantities + prepare_quantities render :toggle end def left @quantity.move_to_right_of(@quantity.parent) if @quantity.parent.present? - @quantities = @project.quantities + prepare_quantities render :toggle end def right @quantity.move_to_child_of(@quantity.left_sibling) if @quantity.left_sibling.present? - @quantities = @project.quantities + prepare_quantities render :toggle end private + def init_session_filters + session[:q_filters] ||= {} + end + def quantity_params params[:quantity].delete(:formula) if params[:quantity][:formula].blank? params.require(:quantity).permit( @@ -69,4 +80,8 @@ class QuantitiesController < ApplicationController :primary ) end + + def prepare_quantities + @quantities = @project.quantities.filter(@project, session[:q_filters]) + end end diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index 94cdc17..b9b0933 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -15,7 +15,7 @@ module IngredientsHelper end def visibility_options(selected) - options = [["all", nil], ["visible", 1], ["hidden", 0]] + options = [["visible", 1], ["hidden", 0]] options_for_select(options, selected) end diff --git a/app/helpers/quantities_helper.rb b/app/helpers/quantities_helper.rb index 89c9934..918e2e0 100644 --- a/app/helpers/quantities_helper.rb +++ b/app/helpers/quantities_helper.rb @@ -1,11 +1,15 @@ module QuantitiesHelper def domain_options - translations = t('.domains') + translations = t('quantities.form.domains') Quantity.domains.map do |k,v| [translations[k.to_sym], k] end end + def domain_options_tag(selected) + options_for_select(domain_options, selected) + end + def parent_options(domain) options = nested_set_options(@quantities.send(domain), @quantity) do |i| raw("#{' ' * i.level}#{i.name}") diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 70da695..f22ce0f 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -41,7 +41,7 @@ class Ingredient < ActiveRecord::Base self.toggle!(:hidden) end - def self.filter(project, filters = {}, requested_q = Quantity.none) + def self.filter(project, filters, requested_q = Quantity.none) ingredients = all if filters[:name].present? diff --git a/app/models/quantity.rb b/app/models/quantity.rb index b261da8..5f49750 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -34,4 +34,14 @@ class Quantity < ActiveRecord::Base def calculate(inputs) Formula.new(self.project, self.formula).calculate(inputs) end + + def self.filter(project, filters) + quantities = all + + if filters[:domain].present? + quantities = quantities.where(domain: domains[filters[:domain]]) + end + + quantities + end end diff --git a/app/views/ingredients/_filters.html.erb b/app/views/ingredients/_filters.html.erb index 95bf2e0..ede4210 100644 --- a/app/views/ingredients/_filters.html.erb +++ b/app/views/ingredients/_filters.html.erb @@ -7,28 +7,29 @@
- <%= text_field_tag 'filters[name]', session[:filters][:name], placeholder: 'name', - :onblur => '$("#filters_form").submit(); return false;' %> + <%= text_field_tag 'filters[name]', session[:i_filters][:name], placeholder: 'name', + onblur: '$("#filters_form").submit(); return false;' %> <%= select_tag 'filters[visibility]', - visibility_options(session[:filters][:visibility]), - :onchange => '$("#filters_form").submit(); return false;' %> + visibility_options(session[:i_filters][:visibility]), + prompt: t('.visibility_prompt'), + onchange: '$("#filters_form").submit(); return false;' %> - <%= text_field_tag 'filters[nutrients]', session[:filters][:nutrients], + <%= text_field_tag 'filters[nutrients]', session[:i_filters][:nutrients], placeholder: 'conditional expression including nutrients', size: 40, - :style => 'box-sizing:border-box; width:100%;', - :onblur => '$("#filters_form").submit(); return false;' %> + style: 'box-sizing:border-box; width:100%;', + onblur: '$("#filters_form").submit(); return false;' %> - <%= link_to l(:button_apply), '#', :class => "icon icon-checked", - :onclick => '$("#filters_form").submit(); return false;' %> + <%= link_to l(:button_apply), '#', class: "icon icon-checked", + onclick: '$("#filters_form").submit(); return false;' %> - <%= link_to l(:button_clear), '#', :class => "icon icon-reload", - :onclick => '$("#filters_form input, #filters_form select").val(""); - $("#filters_form").submit(); return false;' %> + <%= link_to l(:button_clear), '#', class: "icon icon-reload", + onclick: '$("#filters_form input, #filters_form select").val(""); + $("#filters_form").submit(); return false;' %>
diff --git a/app/views/quantities/_filters.html.erb b/app/views/quantities/_filters.html.erb new file mode 100644 index 0000000..baf39c5 --- /dev/null +++ b/app/views/quantities/_filters.html.erb @@ -0,0 +1,27 @@ +
+ <%= l(:label_filter_plural) %> +
+ <%= form_tag url, id: 'filters_form', method: :get, remote: true do %> + + + + + + + +
+ + <%= select_tag 'filters[domain]', domain_options_tag(session[:q_filters][:domain]), + prompt: t('.domain_prompt'), + onchange: '$("#filters_form").submit(); return false;' %> + + <%= link_to l(:button_apply), '#', class: "icon icon-checked", + onclick: '$("#filters_form").submit(); return false;' %> + + <%= link_to l(:button_clear), '#', class: "icon icon-reload", + onclick: '$("#filters_form input, #filters_form select").val(""); + $("#filters_form").submit(); return false;' %> +
+ <% end %> +
+
diff --git a/app/views/quantities/_list.html.erb b/app/views/quantities/_list.html.erb index 6061275..a546dba 100644 --- a/app/views/quantities/_list.html.erb +++ b/app/views/quantities/_list.html.erb @@ -1,3 +1,6 @@ +<%= render :partial => 'quantities/filters', + :locals => {:url => filter_project_quantities_path(@project)} %> + <% if @quantities.any? { |q| q.persisted? } %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 21b1033..90fa891 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -67,6 +67,8 @@ en: contextual: link_import_ingredients: 'Import' link_new_ingredient: 'New ingredient' + filters: + visibility_prompt: 'all' import: heading_import_ingredients: 'Import' label_import_select_csv_file: 'Select CSV file' @@ -98,6 +100,8 @@ en: heading_new_source: 'New source' link_new_source: 'New source' quantities: + filters: + domain_prompt: 'all' index: heading: 'Quantities' heading_new_quantity: 'New quantity' diff --git a/config/routes.rb b/config/routes.rb index d6e999e..990fd12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,6 +21,7 @@ resources :projects do resources :quantities, :only => [:index, :create, :destroy] do post 'toggle', on: :member post 'up', 'down', 'left', 'right', on: :member + get 'filter', on: :collection end resources :units, :only => [:index, :create, :destroy] end diff --git a/init.rb b/init.rb index e849639..d16047f 100644 --- a/init.rb +++ b/init.rb @@ -19,7 +19,7 @@ Redmine::Plugin.register :body_tracking do :measurements => [:index], :ingredients => [:index, :nutrients, :filter, :filter_nutrients], :sources => [:index], - :quantities => [:index], + :quantities => [:index, :filter], :units => [:index], }, read: true permission :manage_common, {