From 0dcb13a0650e22dbd617e7bc8ec68d8407f4d94a Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sat, 14 Sep 2019 23:00:31 +0200 Subject: [PATCH] Started Ingredient#create --- app/controllers/ingredients_controller.rb | 27 +++++++++++++++++++++++ app/models/ingredient.rb | 6 ++++- app/models/nutrient.rb | 6 +++-- app/views/ingredients/_form.html.erb | 4 ++-- app/views/ingredients/index.html.erb | 5 ++--- config/locales/en.yml | 1 + 6 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/controllers/ingredients_controller.rb b/app/controllers/ingredients_controller.rb index 51ecddc..27f4f06 100644 --- a/app/controllers/ingredients_controller.rb +++ b/app/controllers/ingredients_controller.rb @@ -9,8 +9,35 @@ class IngredientsController < ApplicationController end def create + @ingredient = Ingredient.new(ingredient_params.update(project: @project)) + if @ingredient.save + flash[:notice] = 'Created new ingredient' + redirect_to project_ingredients_url(@project) + else + @ingredients = @project.ingredients + render :index + end end def destroy end + + private + + def ingredient_params + params.require(:ingredient).permit( + :name, + :ref_amount, + :ref_unit_id, + :group, + nutrients_attributes: + [ + :id, + :quantity_id, + :amount, + :unit_id, + :_destroy + ] + ) + end end diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 3e41095..75cc9c2 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -4,8 +4,12 @@ class Ingredient < ActiveRecord::Base } belongs_to :project - has_many :nutrients + belongs_to :ref_unit, class_name: 'Unit' + has_many :nutrients, inverse_of: :ingredient accepts_nested_attributes_for :nutrients, allow_destroy: true + #reject_if: proc { |attrs| + # attrs['quantity_id'].blank? && attrs['amount'].blank? + #} 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 c56c570..d445c17 100644 --- a/app/models/nutrient.rb +++ b/app/models/nutrient.rb @@ -1,8 +1,10 @@ class Nutrient < ActiveRecord::Base - belongs_to :ingredient + belongs_to :ingredient, inverse_of: :nutrients + belongs_to :quantity + belongs_to :unit validates :ingredient, presence: true, associated: true - validates :quantity, presence: true, associated: true + validates :quantity, presence: true, associated: true, uniqueness: {scope: :ingredient_id} 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 83695d3..7d861c5 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -8,11 +8,11 @@

<%= f.select :group, group_options, required: true %>

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

<%= ff.select :quantity_id, quantity_options, label: (index > 0 ? '' : :field_nutrients) %> - <%= ff.number_field :amount, {size: 8, label: ''} %> + <%= 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"), '#', diff --git a/app/views/ingredients/index.html.erb b/app/views/ingredients/index.html.erb index b31b83e..d7d0ba1 100644 --- a/app/views/ingredients/index.html.erb +++ b/app/views/ingredients/index.html.erb @@ -29,7 +29,6 @@ <%= l(:field_name) %> <%= l(:field_ref_amount) %> - <%= l(:field_ref_unit) %> <%= l(:field_group) %> <%= l(:field_source) %> <%= l(:field_action) %> @@ -39,9 +38,9 @@ <% @ingredients.each do |i| %> <%= i.name %> - <%= i.ref_amount %> [<%= i.ref_unit %>] + <%= i.ref_amount %> [<%= i.ref_unit.shortname %>] <%= i.group %> - <%= i.source %> + <%#= i.source %> <%= delete_link ingredient_path(i), data: {} %> <% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 7b827c8..f765e36 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,6 +4,7 @@ en: field_action: 'Action' field_ref_amount: 'Reference amount' field_group: 'Group' + field_source: 'Source' field_nutrients: 'Nutrients:' field_domain: 'Domain' field_parent_quantity: 'Parent'