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' %> + +
<%= l(:field_name) %> | + <% @primary_quantities.each do |q| %> +<%= q.name %> | + <% end %> +
---|---|
+ <%= i.name %> + | + <% @primary_quantities.each do |q| %> ++ <%= values[q.id] || '-' %> + | + <% end %> +
<%= 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 @@ + + ++ <%= link_to l(:button_hide), + toggle_nutrient_column_project_ingredients_path(@project, id: q.id), + {class: "icon icon-close", method: :post, remote: true} %> + | + <% end %> +
<%= l(:field_name) %> | - <% @primary_quantities.each do |q| %> -<%= q.name %> | - <% end %> -
---|---|
- <%= i.name %> - | - <% @primary_quantities.each do |q| %> -- <%= values[q.id] || '-' %> - | - <% end %> -
<%= l(:label_no_data) %>
-<% end %> +