Finished Ingredient#create validations
This commit is contained in:
parent
0dcb13a065
commit
893e2646d0
@ -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}
|
||||
|
@ -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
|
||||
|
@ -11,13 +11,13 @@
|
||||
<%= f.fields_for 'nutrients_attributes', n, index: '' do |ff| %>
|
||||
<p class="nutrient">
|
||||
<%= 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;" %>
|
||||
</p>
|
||||
<% 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();
|
||||
|
@ -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:
|
||||
|
Reference in New Issue
Block a user