From 893e2646d0def5c8a92880783d9fdb7dd5d709c5 Mon Sep 17 00:00:00 2001 From: cryptogopher Date: Sun, 15 Sep 2019 18:53:00 +0200 Subject: [PATCH] Finished Ingredient#create validations --- app/models/ingredient.rb | 16 ++++++++++++---- app/models/nutrient.rb | 3 ++- app/views/ingredients/_form.html.erb | 9 +++++---- config/locales/en.yml | 4 ++++ 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/models/ingredient.rb b/app/models/ingredient.rb index 75cc9c2..cc93af1 100644 --- a/app/models/ingredient.rb +++ b/app/models/ingredient.rb @@ -5,11 +5,19 @@ class Ingredient < ActiveRecord::Base belongs_to :project 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? - #} + accepts_nested_attributes_for :nutrients, allow_destroy: true, reject_if: proc { |attrs| + attrs['quantity_id'].blank? && attrs['amount'].blank? + } + validates_associated :nutrients + # Nutrient quantity_id uniqueness check for nested attributes + validate on: :create do + quantities = self.nutrients.map { |n| n.quantity_id } + if quantities.length != quantities.uniq.length + errors.add(:nutrients, :duplicated_quantity) + end + end 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 d445c17..e8275b3 100644 --- a/app/models/nutrient.rb +++ b/app/models/nutrient.rb @@ -3,7 +3,8 @@ class Nutrient < ActiveRecord::Base belongs_to :quantity belongs_to :unit - validates :ingredient, presence: true, associated: true + # disabled to avoid loop with Ingredient 'validates_associated :nutrients' + #validates :ingredient, 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 diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index 7d861c5..ff61dd3 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -11,13 +11,13 @@ <%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>

<%= ff.select :quantity_id, quantity_options, - label: (index > 0 ? '' : :field_nutrients) %> + {include_blank: true, label: (index > 0 ? '' : :field_nutrients)} %> <%= ff.number_field :amount, {size: 8, min: 0, step: :any, label: ''} %> - <%= ff.select :unit_id, unit_options, label: '' %> + <%= ff.select :unit_id, unit_options, {label: ''} %> <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> <%= link_to t(".button_delete_nutrient"), '#', :class => 'icon icon-del', - :style => "display:none", + :style => (@ingredient.nutrients.length > 1 ? "" : "display:none"), :onclick => "deleteNutrient(); return false;" %>

<% end %> @@ -41,7 +41,8 @@ function deleteNutrient() { var row = $(event.target).closest('p.nutrient'); row.find('[id$=_destroy]').val(1); - row.hide(); + // FIXME: should only hide() row if record already saved (to send _destroy to backend) + row.remove(); $('p.nutrient:visible:first label:first').text('<%= t "field_nutrients" %>'); if ($('p.nutrient:visible').length <= 1) { $('p.nutrient a.icon-del').hide(); diff --git a/config/locales/en.yml b/config/locales/en.yml index f765e36..8b9d343 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -12,6 +12,10 @@ en: activerecord: errors: models: + ingredient: + attributes: + nutrients: + duplicated_quantity: 'you can define each quantity only once per ingredient' quantity: attributes: parent: