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