1
0

Finished Ingredient#create validations

This commit is contained in:
cryptogopher 2019-09-15 18:53:00 +02:00
parent 0dcb13a065
commit 893e2646d0
4 changed files with 23 additions and 9 deletions

View File

@ -5,11 +5,19 @@ class Ingredient < ActiveRecord::Base
belongs_to :project belongs_to :project
belongs_to :ref_unit, class_name: 'Unit' belongs_to :ref_unit, class_name: 'Unit'
has_many :nutrients, inverse_of: :ingredient has_many :nutrients, inverse_of: :ingredient
accepts_nested_attributes_for :nutrients, allow_destroy: true accepts_nested_attributes_for :nutrients, allow_destroy: true, reject_if: proc { |attrs|
#reject_if: proc { |attrs| attrs['quantity_id'].blank? && attrs['amount'].blank?
# 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 :project, associated: true
validates :name, presence: true, uniqueness: {scope: :project_id} validates :name, presence: true, uniqueness: {scope: :project_id}

View File

@ -3,7 +3,8 @@ class Nutrient < ActiveRecord::Base
belongs_to :quantity belongs_to :quantity
belongs_to :unit 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 :quantity, presence: true, associated: true, uniqueness: {scope: :ingredient_id}
validates :amount, numericality: {greater_than: 0} validates :amount, numericality: {greater_than: 0}
validates :unit, presence: true, associated: true validates :unit, presence: true, associated: true

View File

@ -11,13 +11,13 @@
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %> <%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
<p class="nutrient"> <p class="nutrient">
<%= ff.select :quantity_id, quantity_options, <%= 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.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: ''} %> <%= ff.check_box :_destroy, {style: "display:none", label: ''} %>
<%= link_to t(".button_delete_nutrient"), '#', <%= link_to t(".button_delete_nutrient"), '#',
:class => 'icon icon-del', :class => 'icon icon-del',
:style => "display:none", :style => (@ingredient.nutrients.length > 1 ? "" : "display:none"),
:onclick => "deleteNutrient(); return false;" %> :onclick => "deleteNutrient(); return false;" %>
</p> </p>
<% end %> <% end %>
@ -41,7 +41,8 @@
function deleteNutrient() { function deleteNutrient() {
var row = $(event.target).closest('p.nutrient'); var row = $(event.target).closest('p.nutrient');
row.find('[id$=_destroy]').val(1); 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" %>'); $('p.nutrient:visible:first label:first').text('<%= t "field_nutrients" %>');
if ($('p.nutrient:visible').length <= 1) { if ($('p.nutrient:visible').length <= 1) {
$('p.nutrient a.icon-del').hide(); $('p.nutrient a.icon-del').hide();

View File

@ -12,6 +12,10 @@ en:
activerecord: activerecord:
errors: errors:
models: models:
ingredient:
attributes:
nutrients:
duplicated_quantity: 'you can define each quantity only once per ingredient'
quantity: quantity:
attributes: attributes:
parent: parent: