diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index efcd0b2..e02fb2b 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -9,7 +9,7 @@ class IngredientsController < ApplicationController @ingredient = @project.ingredients.new # passing attr for Nutrient after_initialize @ingredient.nutrients.new(ingredient: @ingredient) - @ingredients = @project.ingredients.includes(:ref_unit) + @ingredients = @project.ingredients.includes(:ref_unit, :source) @ingredients << @ingredient end @@ -19,7 +19,7 @@ class IngredientsController < ApplicationController flash[:notice] = 'Created new ingredient' redirect_to project_ingredients_url(@project) else - @ingredients = @project.ingredients.includes(:ref_unit) + @ingredients = @project.ingredients.includes(:ref_unit, :source) @ingredient.nutrients.new(ingredient: @ingredient) if @ingredient.nutrients.empty? render :index end @@ -39,6 +39,7 @@ class IngredientsController < ApplicationController if params.has_key?(:file) quantities = @project.quantities.map { |q| [q.name, q] }.to_h units = @project.units.map { |u| [u.shortname, u] }.to_h + sources = @project.sources.map { |s| [s.name, s] }.to_h ingredients_params = [] column_units = {} @@ -47,11 +48,17 @@ class IngredientsController < ApplicationController unless r.has_key?('Name') warnings << "Line 1: required 'Name' column is missing" if line == 2 end + if r['Source'].present? && sources[r['Source']].blank? + warnings << "Line #{line}: unknown source name #{r['Source']}" + end + i_params = { name: r.delete('Name'), ref_amount: 100.0, ref_unit: units['g'], group: r.delete('Group') || :other, + source: sources[r['Source']], + source_ident: r.delete('SourceIdent'), nutrients_attributes: [] } @@ -134,6 +141,8 @@ class IngredientsController < ApplicationController :ref_amount, :ref_unit_id, :group, + :source_id, + :source_ident, nutrients_attributes: [ :id, diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb index 0ea8f87..06bac6d 100644 --- a/app/helpers/ingredients_helper.rb +++ b/app/helpers/ingredients_helper.rb @@ -11,6 +11,12 @@ module IngredientsHelper end end + def source_options + @project.sources.map do |s| + [s.name, s.id] + end + end + def group_options translations = t('.groups') Ingredient.groups.map do |k,v| diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 0a25f40..038823b 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -6,6 +6,7 @@ class Ingredient < ActiveRecord::Base belongs_to :project, required: true belongs_to :ref_unit, class_name: 'Unit', required: true + belongs_to :source, required: false has_many :nutrients, inverse_of: :ingredient, dependent: :destroy, validate: true validates :nutrients, presence: true diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index b5624ff..d49ee4a 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -7,6 +7,14 @@ <%= 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| %>

diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index 1a966da..6c0dd9f 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -64,7 +64,10 @@ <%= i.name %> <%= i.ref_amount %> [<%= i.ref_unit.shortname %>] <%= i.group %> - <%#= i.source %> + + <%= i.source.name if i.source.present? %> + <%= ", #{i.source_ident}" if i.source_ident.present? %> + <%= delete_link ingredient_path(i), data: {} %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index c8f533d..9af04bb 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,7 +4,8 @@ en: field_action: 'Action' field_reference: 'Reference' field_group: 'Group' - field_source: 'Source' + field_source: 'Data source' + field_source_ident: 'Source identifier' field_nutrients: 'Nutrients:' field_domain: 'Domain' field_parent_quantity: 'Parent' @@ -44,6 +45,7 @@ en: names are: (1) ingredient attributes, case sensitive: "Name" - required, "Reference" - defaults to 100[g], "Group" - defaults to "other", + "Source" - optional, "SourceIdent" - optional, (2) quantities'' names with unit short name in square brackets. Sample header: "Name,Reference,Group,Proteins[g],Fats[g],Carbohydrates[g]". Sample data row: "Brussels,100[g],other,3.4,300[mg],9". diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb index c7d9d7a..acae5c7 100644 --- a/db/migrate/001_create_units.rb +++ b/db/migrate/001_create_units.rb @@ -164,8 +164,8 @@ class CreateUnits < ActiveRecord::Migration v21 = Quantity.create project: nil, domain: :diet, parent: v1, name: "Vitamin K", description: "" - Source.create project: nil, name: "Nutrition label", - description: "From package nutrition label" + Source.create project: nil, name: "nutrition label", + description: "nutrition facts taken from package nutrition label" end dir.down do Unit.where(project: nil).delete_all