1
0

Added ingredient source setting/display/import

This commit is contained in:
cryptogopher 2019-09-23 22:33:40 +02:00
parent 0e6bbd8c74
commit 9664df4888
7 changed files with 35 additions and 6 deletions

View File

@ -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,

View File

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

View File

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

View File

@ -7,6 +7,14 @@
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
</p>
<p><%= f.select :group, group_options, required: true %></p>
<div class="splitcontent">
<div class="splitcontentleft">
<p><%= f.select :source_id, source_options, required: false, include_blank: true %></p>
</div>
<div class="splitcontentright">
<p><%= f.text_field :source_ident, size: 25, required: false %></p>
</div>
</div>
<% @ingredient.nutrients.each_with_index do |n, index| %>
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
<p class="nutrient">

View File

@ -64,7 +64,10 @@
<td class="name"><%= i.name %></td>
<td class="reference"><%= i.ref_amount %> [<%= i.ref_unit.shortname %>]</td>
<td class="group"><%= i.group %></td>
<td class="source"><%#= i.source %></td>
<td class="source">
<%= i.source.name if i.source.present? %>
<%= ", #{i.source_ident}" if i.source_ident.present? %>
</td>
<td><%= delete_link ingredient_path(i), data: {} %></td>
</tr>
<% end %>

View File

@ -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".

View File

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