From 1acb72f33d25ad2cf50550e6ac8f63180c7717d1 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 2 Nov 2019 15:32:21 +0100 Subject: [PATCH] Filtering ingredients by name Nutrients link in sidebar --- app/controllers/ingredients_controller.rb | 39 ++++++++++++++++++++--- app/views/body_trackers/_sidebar.html.erb | 6 +++- app/views/ingredients/_filters.html.erb | 19 +++++++++++ app/views/ingredients/_list.html.erb | 2 +- app/views/ingredients/_options.html.erb | 2 +- app/views/ingredients/index.html.erb | 2 ++ app/views/ingredients/nutrients.html.erb | 2 ++ config/locales/en.yml | 3 +- config/routes.rb | 6 ++-- init.rb | 2 +- 10 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 app/views/ingredients/_filters.html.erb diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index a22be1a..6c43825 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -1,9 +1,10 @@ class IngredientsController < ApplicationController require 'csv' - before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients] - before_action :find_ingredient, only: [:destroy, :toggle] + before_action :find_project_by_project_id, + only: [:index, :nutrients, :create, :import, :filter, :filter_nutrients] before_action :find_quantity, only: [:toggle_nutrient_column] + before_action :find_ingredient, only: [:destroy, :toggle] before_action :authorize def index @@ -11,7 +12,7 @@ class IngredientsController < ApplicationController # passing attr for Nutrient after_initialize @ingredient.nutrients.new(ingredient: @ingredient) - @ingredients = @project.ingredients.includes(:ref_unit, :source) + prepare_ingredients @ingredients << @ingredient end @@ -27,18 +28,20 @@ class IngredientsController < ApplicationController end def create + # FIXME: redirect to :back? @ingredient = @project.ingredients.new(ingredient_params) if @ingredient.save flash[:notice] = 'Created new ingredient' redirect_to project_ingredients_url(@project) else - @ingredients = @project.ingredients.includes(:ref_unit, :source) + prepare_ingredients @ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty? render :index end end def destroy + # FIXME: redirect to :back? # FIXME: don't destroy if any meal depend on ingredient if @ingredient.destroy flash[:notice] = 'Deleted ingredient' @@ -48,10 +51,23 @@ class IngredientsController < ApplicationController def toggle @ingredient.update(hidden: !@ingredient.hidden) - @ingredients = @project.ingredients.includes(:ref_unit, :source) + prepare_ingredients + end + + def filter + session[:filters] = params[:filters] + prepare_ingredients + render :toggle + end + + def filter_nutrients + session[:filters] = params[:filters] + prepare_nutrients + render :toggle_nutrient_column end def import + # FIXME: redirect to :back? warnings = [] if params.has_key?(:file) @@ -181,8 +197,13 @@ class IngredientsController < ApplicationController render_404 end + def prepare_ingredients + @ingredients = filter_ingredients(@project.ingredients.includes(:ref_unit, :source)) + end + def prepare_nutrients ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) + ingredients = filter_ingredients(ingredients) @primary_quantities = @project.quantities.where(primary: true) @primary_nutrients = {} @extra_nutrients = {} @@ -198,4 +219,12 @@ class IngredientsController < ApplicationController end end end + + def filter_ingredients(ingredients) + filters = session[:filters] || {} + if filters[:name].present? + ingredients = ingredients.where("name LIKE ?", "%#{filters[:name]}%") + end + ingredients + end end diff --git a/app/views/body_trackers/_sidebar.html.erb b/app/views/body_trackers/_sidebar.html.erb index c81995b..f8579f0 100644 --- a/app/views/body_trackers/_sidebar.html.erb +++ b/app/views/body_trackers/_sidebar.html.erb @@ -5,7 +5,11 @@

<%= t ".heading_diet" %>

<%= t ".heading_common" %>

diff --git a/app/views/ingredients/_filters.html.erb b/app/views/ingredients/_filters.html.erb new file mode 100644 index 0000000..61b5bf8 --- /dev/null +++ b/app/views/ingredients/_filters.html.erb @@ -0,0 +1,19 @@ +
+ <%= l(:label_filter_plural) %> + <%= form_tag url, id: 'filters_form', method: :get, remote: true do %> + + + + + + +
+ <%= text_field_tag 'filters[name]', session[:filters][:name], + placeholder: 'name filter' %> + + <%= link_to l(:button_apply), '#', + :class => "icon icon-checked", + :onclick => '$("#filters_form").submit(); return false;' %> +
+ <% end %> +
diff --git a/app/views/ingredients/_list.html.erb b/app/views/ingredients/_list.html.erb index d486e0b..10c652e 100644 --- a/app/views/ingredients/_list.html.erb +++ b/app/views/ingredients/_list.html.erb @@ -6,7 +6,7 @@ <%= l(:field_reference) %> <%= l(:field_group) %> <%= l(:field_source) %> - <%= l(:field_action) %> + <%= l(:field_action) %> diff --git a/app/views/ingredients/_options.html.erb b/app/views/ingredients/_options.html.erb index 173ca1b..dd56085 100644 --- a/app/views/ingredients/_options.html.erb +++ b/app/views/ingredients/_options.html.erb @@ -4,7 +4,7 @@ id: 'add_nutrient_column', method: :post, remote: true do %> - + diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index 8e6d44f..c921d3a 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -13,6 +13,8 @@ <%= render :partial => 'ingredients/form' %>

<%= t ".heading" %>

+<%= render :partial => 'ingredients/filters', + :locals => {:url => filter_project_ingredients_path(@project)} %>
<%= render :partial => 'ingredients/list' %>
diff --git a/app/views/ingredients/nutrients.html.erb b/app/views/ingredients/nutrients.html.erb index 2096bd4..4fb8de3 100644 --- a/app/views/ingredients/nutrients.html.erb +++ b/app/views/ingredients/nutrients.html.erb @@ -13,6 +13,8 @@ <%= render :partial => 'ingredients/form' %>

<%= t ".heading" %>

+<%= render :partial => 'ingredients/filters', + :locals => {:url => filter_nutrients_project_ingredients_path(@project)} %>
<%= render :partial => 'ingredients/list_nutrients' %>
diff --git a/config/locales/en.yml b/config/locales/en.yml index 1a33d5c..6dda2c5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -33,6 +33,7 @@ en: heading_common: 'Common' link_summary: 'Summary' link_ingredients: 'Ingredients' + link_nutrients: 'Nutrients' link_sources: 'Data sources' link_quantities: 'Quantities' link_units: 'Units' @@ -61,8 +62,6 @@ en: groups: other: 'other' meat: 'meat' - options: - label_options: 'Options' index: heading: 'Ingredients' heading_nutrient_view: 'Nutrient view' diff --git a/config/routes.rb b/config/routes.rb index 913f8cb..2409f2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,10 +7,12 @@ resources :projects do post 'defaults', on: :collection end resources :ingredients, :only => [:index, :create, :destroy] do - post 'toggle', on: :member - post 'import', on: :collection get 'nutrients', on: :collection post 'toggle_nutrient_column', on: :collection + post 'toggle', on: :member + get 'filter', on: :collection + get 'filter_nutrients', on: :collection + post 'import', on: :collection end resources :sources, :only => [:index, :create, :destroy] resources :quantities, :only => [:index, :create, :destroy] do diff --git a/init.rb b/init.rb index 764e3f4..697f3e5 100644 --- a/init.rb +++ b/init.rb @@ -16,7 +16,7 @@ Redmine::Plugin.register :body_tracking do project_module :body_tracking do permission :view_body_trackers, { :body_trackers => [:index], - :ingredients => [:index, :nutrients], + :ingredients => [:index, :nutrients, :filter, :filter_nutrients], :sources => [:index], :quantities => [:index], :units => [:index],
<%= select_tag 'id', nutrient_column_options %>