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 %> +
> +

<%= t ".heading_new_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 %>

-
-
- <% @ingredient.nutrients.each_with_index do |n, index| %> - <%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %> -

- <%= 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 %>

+
+
+ <% @ingredient.nutrients.each_with_index do |n, index| %> + <%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %> +

+ <%= 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;' %> +

+
+ <%= submit_tag l(:button_create) %> + <%= link_to l(:button_cancel), "#", :onclick => '$("#add-ingredient").hide()' %> <% end %> -

<%= link_to t(".button_add_nutrient"), '#', :class => 'icon icon-add', - :onclick => 'addNutrient(); return false;' %>

+
<%= javascript_tag do %> diff --git a/app/views/ingredients/_import.html.erb b/app/views/ingredients/_import.html.erb new file mode 100644 index 0000000..b911317 --- /dev/null +++ b/app/views/ingredients/_import.html.erb @@ -0,0 +1,20 @@ + diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index 01b6261..8e6d44f 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -5,47 +5,12 @@
<%= link_to t(".heading_nutrient_view"), nutrients_project_ingredients_path(@project), :class => 'icon icon-stats' %> - <% 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 %> + <%= render :partial => 'ingredients/contextual' %>
- - -
> -

<%= t ".heading_new_ingredient" %>

- - <%= labelled_form_for @ingredient, - :url => project_ingredients_path(@project), - :html => {:id => 'ingredient-form'} do |f| %> - <%= render :partial => 'ingredients/form', :locals => { :f => f } %> - <%= submit_tag l(:button_create) %> - <%= link_to l(:button_cancel), "#", :onclick => '$("#add-ingredient").hide()' %> - <% end %> -
-
+<%= render :partial => 'ingredients/form' %>

<%= t ".heading" %>

diff --git a/app/views/ingredients/nutrients.html.erb b/app/views/ingredients/nutrients.html.erb index 8caba41..514a70b 100644 --- a/app/views/ingredients/nutrients.html.erb +++ b/app/views/ingredients/nutrients.html.erb @@ -1,12 +1,17 @@ <% content_for :sidebar do %> - <%#= render :partial => 'body_trackers/sidebar' %> + <%= render :partial => 'body_trackers/sidebar' %> <% end %>
<%= link_to t(".heading_ingredient_list"), project_ingredients_path(@project), :class => 'icon icon-list' %> + <%= render :partial => 'ingredients/contextual' %>
+<%= render :partial => 'ingredients/import' %> + +<%= render :partial => 'ingredients/form' %> +

<%= t ".heading" %>

<% if @nutrients.any? %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 40c2969..c819e9d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -39,11 +39,11 @@ en: link_defaults: 'Load defaults' confirm_defaults: 'This will load default quantities and units. Continue?' ingredients: - index: - heading: 'Ingredients' - heading_nutrient_view: 'Nutrient view' + contextual: heading_import_ingredients: 'Import' heading_new_ingredient: 'New ingredient' + import: + heading_import_ingredients: 'Import' label_import_select_csv_file: 'Select CSV file' import_hints: 'CSV file has to include header with column names. Recognized column names are: @@ -54,15 +54,19 @@ en: Sample header: "Name,Reference,Group,Proteins[g],Fats[g],Carbohydrates[g]". Sample data row: "Brussels,100[g],other,3.4,300[mg],9". Unit given in data row has precedence over that specified in header.' - nutrients: - heading: 'Nutrients' - heading_ingredient_list: 'Ingredient list' form: + heading_new_ingredient: 'New ingredient' button_add_nutrient: 'Add nutrient' button_delete_nutrient: 'Delete' groups: other: 'other' meat: 'meat' + index: + heading: 'Ingredients' + heading_nutrient_view: 'Nutrient view' + nutrients: + heading: 'Nutrients' + heading_ingredient_list: 'Ingredient list' sources: index: heading: 'Data sources'