From f039917d0e82eff83c2d31d1599879c24f460473 Mon Sep 17 00:00:00 2001
From: cryptogopher
Date: Fri, 13 Sep 2019 19:21:28 +0200
Subject: [PATCH] Added nested nutrients in ingredient form
---
app/controllers/ingredients_controller.rb | 1 +
app/helpers/ingredients_helper.rb | 6 ++++++
app/models/ingredient.rb | 2 ++
app/models/nutrient.rb | 6 ++++++
app/views/ingredients/_form.html.erb | 7 +++++++
config/locales/en.yml | 2 +-
db/migrate/001_create_units.rb | 2 +-
7 files changed, 24 insertions(+), 2 deletions(-)
diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb
index 7da1b49..046fc04 100644
--- a/app/controllers/ingredients_controller.rb
+++ b/app/controllers/ingredients_controller.rb
@@ -4,6 +4,7 @@ class IngredientsController < ApplicationController
def index
@ingredient = Ingredient.new
+ @nutrient = Nutrient.new
@ingredients = @project.ingredients
end
diff --git a/app/helpers/ingredients_helper.rb b/app/helpers/ingredients_helper.rb
index 8ddcfa0..0ea8f87 100644
--- a/app/helpers/ingredients_helper.rb
+++ b/app/helpers/ingredients_helper.rb
@@ -1,4 +1,10 @@
module IngredientsHelper
+ def quantity_options
+ nested_set_options(@project.quantities.diet) do |q|
+ raw("#{' ' * q.level}#{q.name}")
+ end
+ end
+
def unit_options
@project.units.map do |u|
[u.shortname, u.id]
diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb
index 8fda20d..5c8311b 100644
--- a/app/models/ingredient.rb
+++ b/app/models/ingredient.rb
@@ -4,6 +4,8 @@ class Ingredient < ActiveRecord::Base
}
belongs_to :project
+ has_many :nutrients
+ accepts_nested_attributes_for :nutrients
validates :project, associated: true
validates :name, presence: true, uniqueness: {scope: :project_id}
diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb
index b146427..c56c570 100644
--- a/app/models/nutrient.rb
+++ b/app/models/nutrient.rb
@@ -1,2 +1,8 @@
class Nutrient < ActiveRecord::Base
+ belongs_to :ingredient
+
+ validates :ingredient, presence: true, associated: true
+ validates :quantity, presence: true, associated: true
+ validates :amount, numericality: {greater_than: 0}
+ validates :unit, presence: true, associated: true
end
diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb
index 63dd3cd..f871d2a 100644
--- a/app/views/ingredients/_form.html.erb
+++ b/app/views/ingredients/_form.html.erb
@@ -7,4 +7,11 @@
<%= f.select :ref_unit_id, unit_options, {label: '', required: true} %>
<%= f.select :group, group_options, required: true %>
+ <%= f.fields_for :nutrients, @nutrient do |ff| %>
+
+ <%= ff.select :quantity_id, quantity_options, {label: '', required: true} %>
+ <%= ff.number_field :amount, {size: 8, label: '', required: true} %>
+ <%= ff.select :unit_id, unit_options, {label: '', required: true} %>
+
+ <% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 79b8dc7..52a65dd 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -19,7 +19,7 @@ en:
heading: 'Summary'
sidebar:
heading_body_trackers: 'Body trackers'
- heding_diet: 'Diet'
+ heading_diet: 'Diet'
heading_common: 'Common'
link_summary: 'Summary'
link_ingredients: 'Ingredients'
diff --git a/db/migrate/001_create_units.rb b/db/migrate/001_create_units.rb
index 8bd7cca..34d22e5 100644
--- a/db/migrate/001_create_units.rb
+++ b/db/migrate/001_create_units.rb
@@ -30,8 +30,8 @@ class CreateUnits < ActiveRecord::Migration
create_table :nutrients do |t|
t.references :ingredient
t.references :quantity
- t.references :unit
t.decimal :amount
+ t.references :unit
end
reversible do |dir|