diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 636b2e8..6d66ef1 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -9,10 +9,31 @@ class IngredientsController < ApplicationController @ingredient = @project.ingredients.new # passing attr for Nutrient after_initialize @ingredient.nutrients.new(ingredient: @ingredient) + @ingredients = @project.ingredients.includes(:ref_unit, :source) @ingredients << @ingredient end + def nutrients + @ingredient = @project.ingredients.new + @ingredient.nutrients.new(ingredient: @ingredient) + + ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) + @header = @project.quantities.where(primary: true) + @nutrients = Hash.new { |h,k| h[k] = {} } + @descriptions = Hash.new { |h,k| h[k] = [] } + ingredients.each do |i| + i.nutrients.sort_by { |n| n.quantity.lft }.each do |n| + if @header.include?(n.quantity) + @nutrients[i.name][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]" + else + @descriptions[i.name] << "#{n.quantity.name}: #{n.amount} [#{n.unit.shortname}]" + end + end + end + @descriptions.each { |k, v| @descriptions[k] = v.join(", ") } + end + def create @ingredient = @project.ingredients.new(ingredient_params) if @ingredient.save @@ -138,23 +159,6 @@ class IngredientsController < ApplicationController redirect_to project_ingredients_url(@project) end - def nutrients - ingredients = @project.ingredients.includes(:ref_unit, nutrients: [:quantity, :unit]) - @header = @project.quantities.where(primary: true) - @nutrients = Hash.new { |h,k| h[k] = {} } - @descriptions = Hash.new { |h,k| h[k] = [] } - ingredients.each do |i| - i.nutrients.sort_by { |n| n.quantity.lft }.each do |n| - if @header.include?(n.quantity) - @nutrients[i.name][n.quantity_id] = "#{n.amount} [#{n.unit.shortname}]" - else - @descriptions[i.name] << "#{n.quantity.name}: #{n.amount} [#{n.unit.shortname}]" - end - end - end - @descriptions.each { |k, v| @descriptions[k] = v.join(", ") } - end - private def ingredient_params diff --git a/app/views/ingredients/_contextual.html.erb b/app/views/ingredients/_contextual.html.erb new file mode 100644 index 0000000..08dd2ae --- /dev/null +++ b/app/views/ingredients/_contextual.html.erb @@ -0,0 +1,6 @@ +<% if User.current.allowed_to?(:manage_common, @project) %> + <%= link_to t(".heading_import_ingredients"), '#', :class => 'icon icon-multiple', + :onclick => 'showAndScrollTo("import-ingredients", "filename"); return false;' %> + <%= link_to t(".heading_new_ingredient"), '#', :class => 'icon icon-add', + :onclick => 'showAndScrollTo("add-ingredient", "ingredient_name"); return false;' %> +<% end %> diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index d49ee4a..009f66b 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -1,37 +1,53 @@ -<%= error_messages_for @ingredient %> +
<%= f.text_field :name, size: 40, required: true %>
-- <%= f.number_field :ref_amount, size: 8, required: true, min: 0, label: :field_reference %> - <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %> -
-<%= f.select :group, group_options, required: true %>
-<%= f.select :source_id, source_options, required: false, include_blank: true %>
-<%= f.text_field :source_ident, size: 25, required: false %>
-- <%= ff.select :quantity_id, quantity_options, - {include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %> - <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> - <%= ff.select :unit_id, unit_options, {label: ''} %> - <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> - <%= link_to t(".button_delete_nutrient"), '#', - :class => 'icon icon-del', - :style => (@ingredient.nutrients.length > 1 ? "" : "display:none"), - :onclick => "deleteNutrient(); return false;" %> + <%= labelled_form_for @ingredient, + :url => project_ingredients_path(@project), + :html => {:id => 'ingredient-form'} do |f| %> + <%= error_messages_for @ingredient %> + +
<%= f.text_field :name, size: 40, required: true %>
++ <%= f.number_field :ref_amount, size: 8, required: true, min: 0, + label: :field_reference %> + <%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
- <% end %> +<%= f.select :group, group_options, required: true %>
++ <%= f.select :source_id, source_options, required: false, include_blank: true %> +
+<%= f.text_field :source_ident, size: 25, required: false %>
++ <%= ff.select :quantity_id, quantity_options, + {include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %> + <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> + <%= ff.select :unit_id, unit_options, {label: ''} %> + <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> + <%= link_to t(".button_delete_nutrient"), '#', + :class => 'icon icon-del', + :style => (@ingredient.nutrients.length > 1 ? "" : "display:none"), + :onclick => "deleteNutrient(); return false;" %> +
+ <% end %> + <% end %> ++ <%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add', + :onclick => 'addNutrient(); return false;' %> +
+<%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add', - :onclick => 'addNutrient(); return false;' %>
+