From cf9c9138979a1098e6d166d39e94b5d0a43ff832 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Fri, 1 Nov 2019 21:43:56 +0100 Subject: [PATCH] Toggling quantity primariness from nutrients view --- app/controllers/ingredients_controller.rb | 39 +++++---- app/controllers/quantities_controller.rb | 11 +-- app/helpers/ingredients_helper.rb | 9 +++ app/models/quantity.rb | 4 + .../ingredients/_list_nutrients.html.erb | 78 ++++++++++++++++++ app/views/ingredients/_options.html.erb | 31 ++++++++ app/views/ingredients/nutrients.html.erb | 79 +------------------ .../ingredients/toggle_nutrient_column.js.erb | 3 + config/locales/en.yml | 2 + config/routes.rb | 1 + init.rb | 3 +- .../application_controller_patch.rb | 16 ++++ 12 files changed, 174 insertions(+), 102 deletions(-) create mode 100644 app/views/ingredients/_list_nutrients.html.erb create mode 100644 app/views/ingredients/_options.html.erb create mode 100644 app/views/ingredients/toggle_nutrient_column.js.erb create mode 100644 lib/body_tracking/application_controller_patch.rb diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index eadc642..a22be1a 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -3,6 +3,7 @@ class IngredientsController < ApplicationController before_action :find_project_by_project_id, only: [:index, :create, :import, :nutrients] before_action :find_ingredient, only: [:destroy, :toggle] + before_action :find_quantity, only: [:toggle_nutrient_column] before_action :authorize def index @@ -17,22 +18,12 @@ class IngredientsController < ApplicationController def nutrients @ingredient = @project.ingredients.new @ingredient.nutrients.new(ingredient: @ingredient) + prepare_nutrients + end - ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) - @primary_quantities = @project.quantities.where(primary: true) - @primary_nutrients = {} - @extra_nutrients = {} - ingredients.each do |i| - @primary_nutrients[i] = {} - @extra_nutrients[i] = {} - i.nutrients.sort_by { |n| n.quantity.lft }.each do |n| - if @primary_quantities.include?(n.quantity) - @primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]" - else - @extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]" - end - end - end + def toggle_nutrient_column + @quantity.toggle_primary! + prepare_nutrients end def create @@ -189,4 +180,22 @@ class IngredientsController < ApplicationController rescue ActiveRecord::RecordNotFound render_404 end + + def prepare_nutrients + ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) + @primary_quantities = @project.quantities.where(primary: true) + @primary_nutrients = {} + @extra_nutrients = {} + ingredients.each do |i| + @primary_nutrients[i] = {} + @extra_nutrients[i] = {} + i.nutrients.sort_by { |n| n.quantity.lft }.each do |n| + if @primary_quantities.include?(n.quantity) + @primary_nutrients[i][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]" + else + @extra_nutrients[i][n.quantity.name] = "#{n.amount} [#{n.unit.shortname}]" + end + end + end + end end diff --git a/app/controllers/quantities_controller.rb b/app/controllers/quantities_controller.rb index 34cfe3f..1f1e64a 100644 --- a/app/controllers/quantities_controller.rb +++ b/app/controllers/quantities_controller.rb @@ -28,7 +28,7 @@ class QuantitiesController < ApplicationController end def toggle - @quantity.update(primary: !@quantity.primary) + @quantity.toggle_primary! @quantities = @project.quantities end @@ -67,13 +67,4 @@ class QuantitiesController < ApplicationController :primary ) end - - # :find_* methods are called before :authorize, - # @project is required for :authorize to succeed - def find_quantity - @quantity = Quantity.find(params[:id]) - @project = @quantity.project - rescue ActiveRecord::RecordNotFound - render_404 - end end diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index 06bac6d..7bd404b 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -5,6 +5,15 @@ module IngredientsHelper end end + def nutrient_column_options + disabled = [] + options = nested_set_options(@project.quantities.diet) do |q| + disabled << q.id if q.primary + raw("#{' ' * q.level}#{q.name}") + end + options_for_select(options, disabled: disabled) + end + def unit_options @project.units.map do |u| [u.shortname, u.id] diff --git a/app/models/quantity.rb b/app/models/quantity.rb index 748a3c6..cc610f0 100644 --- a/app/models/quantity.rb +++ b/app/models/quantity.rb @@ -19,4 +19,8 @@ class Quantity < ActiveRecord::Base self.primary = false if self.primary.nil? end end + + def toggle_primary! + self.toggle!(:primary) + end end diff --git a/app/views/ingredients/_list_nutrients.html.erb b/app/views/ingredients/_list_nutrients.html.erb new file mode 100644 index 0000000..b865cf4 --- /dev/null +++ b/app/views/ingredients/_list_nutrients.html.erb @@ -0,0 +1,78 @@ +<% if @primary_nutrients.any? %> + <%= render :partial => 'ingredients/options' %> + + + + + <% total_width = 3 + @primary_quantities.length %> + + <% @primary_quantities.each do |q| %> + + <% end %> + + + + <% @primary_nutrients.each do |i, values| %> + <% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %> + + + <% @primary_quantities.each do |q| %> + + <% end %> + + + + + <% @primary_quantities.each do |q| %> + + <% end %> + + + + <% @primary_quantities.each do |q| %> + + <% end %> + + + <% extras = @extra_nutrients[i].keys %> + <% extras.each_slice(@primary_quantities.length).with_index do |names, index| %> + + + <% names.each do |name| %> + + <% end %> + <% if @primary_quantities.length > names.length %> + + <% end %> + + + + <% names.each do |name| %> + + <% end %> + <% if @primary_quantities.length > names.length %> + + <% end %> + + <% end %> + + <% end %> + +
<%= l(:field_name) %><%= q.name %>
+ <%= i.name %> + + <%= values[q.id] || '-' %> +
+<% else %> +

<%= l(:label_no_data) %>

+<% end %> diff --git a/app/views/ingredients/_options.html.erb b/app/views/ingredients/_options.html.erb new file mode 100644 index 0000000..173ca1b --- /dev/null +++ b/app/views/ingredients/_options.html.erb @@ -0,0 +1,31 @@ +
+ <%= l(:label_options) %> + <%= form_tag toggle_nutrient_column_project_ingredients_path(@project), + id: 'add_nutrient_column', method: :post, remote: true do %> + + + + + + +
+ <%= select_tag 'id', nutrient_column_options %> + + <%= submit_tag l(:button_add) %> +
+ <% end %> +
+ + + + <% total_width = 3 + @primary_quantities.length %> + + <% @primary_quantities.each do |q| %> + + <% end %> + +
+ <%= link_to l(:button_hide), + toggle_nutrient_column_project_ingredients_path(@project, id: q.id), + {class: "icon icon-close", method: :post, remote: true} %> +
diff --git a/app/views/ingredients/nutrients.html.erb b/app/views/ingredients/nutrients.html.erb index c8f1597..2096bd4 100644 --- a/app/views/ingredients/nutrients.html.erb +++ b/app/views/ingredients/nutrients.html.erb @@ -13,79 +13,6 @@ <%= render :partial => 'ingredients/form' %>

<%= t ".heading" %>

-<% if @primary_nutrients.any? %> - - - - - <% @primary_quantities.each do |q| %> - - <% end %> - - - - <% @primary_nutrients.each do |i, values| %> - - <% row_class = "ingredient#{' hidden' if i.hidden} #{cycle('odd', 'even')}" %> - - - <% @primary_quantities.each do |q| %> - - <% end %> - - - - - <% @primary_quantities.each do |q| %> - - <% end %> - - - - <% @primary_quantities.each do |q| %> - - <% end %> - - - <% extras = @extra_nutrients[i].keys %> - <% extras.each_slice(@primary_quantities.length).with_index do |names, index| %> - - - <% names.each do |name| %> - - <% end %> - <% if @primary_quantities.length > names.length %> - - <% end %> - - - - <% names.each do |name| %> - - <% end %> - <% if @primary_quantities.length > names.length %> - - <% end %> - - <% end %> - - <% end %> - -
<%= l(:field_name) %><%= q.name %>
- <%= i.name %> - - <%= values[q.id] || '-' %> -
-<% else %> -

<%= l(:label_no_data) %>

-<% end %> +
+ <%= render :partial => 'ingredients/list_nutrients' %> +
diff --git a/app/views/ingredients/toggle_nutrient_column.js.erb b/app/views/ingredients/toggle_nutrient_column.js.erb new file mode 100644 index 0000000..55cd8f1 --- /dev/null +++ b/app/views/ingredients/toggle_nutrient_column.js.erb @@ -0,0 +1,3 @@ +$('div[id^=flash_]').remove(); +$('#content').prepend('<%= escape_javascript(render_flash_messages) %>'); +$('#nutrients').html('<%= escape_javascript(render :partial => 'ingredients/list_nutrients') %>'); diff --git a/config/locales/en.yml b/config/locales/en.yml index c819e9d..1a33d5c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -61,6 +61,8 @@ 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 730031d..913f8cb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,7 @@ resources :projects do post 'toggle', on: :member post 'import', on: :collection get 'nutrients', on: :collection + post 'toggle_nutrient_column', 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 f16870c..764e3f4 100644 --- a/init.rb +++ b/init.rb @@ -1,6 +1,7 @@ require_dependency 'body_tracking/body_trackers_view_listener' (Rails::VERSION::MAJOR < 5 ? ActionDispatch : ActiveSupport)::Reloader.to_prepare do + ApplicationController.include BodyTracking::ApplicationControllerPatch Project.include BodyTracking::ProjectPatch end @@ -22,7 +23,7 @@ Redmine::Plugin.register :body_tracking do }, read: true permission :manage_common, { :body_trackers => [:defaults], - :ingredients => [:create, :destroy, :toggle, :import], + :ingredients => [:create, :destroy, :toggle, :import, :toggle_nutrient_column], :sources => [:create, :destroy], :quantities => [:create, :destroy, :toggle, :up, :down, :left, :right], :units => [:create, :destroy], diff --git a/lib/body_tracking/application_controller_patch.rb b/lib/body_tracking/application_controller_patch.rb new file mode 100644 index 0000000..853a0e8 --- /dev/null +++ b/lib/body_tracking/application_controller_patch.rb @@ -0,0 +1,16 @@ +module BodyTracking + module ApplicationControllerPatch + ApplicationController.class_eval do + private + + # :find_* methods are called before :authorize, + # @project is required for :authorize to succeed + def find_quantity + @quantity = Quantity.find(params[:id]) + @project = @quantity.project + rescue ActiveRecord::RecordNotFound + render_404 + end + end + end +end