diff --git a/app/controllers/measurements_controller.rb b/app/controllers/measurements_controller.rb index 82facaf..6996eea 100644 --- a/app/controllers/measurements_controller.rb +++ b/app/controllers/measurements_controller.rb @@ -49,6 +49,7 @@ class MeasurementsController < ApplicationController [ :id, :quantity_id, + :value, :unit_id, :_destroy ] diff --git a/app/models/measurement.rb b/app/models/measurement.rb index 9c37012..cfc33a1 100644 --- a/app/models/measurement.rb +++ b/app/models/measurement.rb @@ -5,7 +5,7 @@ class Measurement < ActiveRecord::Base has_many :readouts, inverse_of: :measurement, dependent: :destroy, validate: true validates :readouts, presence: true accepts_nested_attributes_for :readouts, allow_destroy: true, reject_if: proc { |attrs| - attrs['quantity_id'].blank? + attrs['quantity_id'].blank? && attrs['value'].blank? } # Readout (quantity_id, unit_id) pair uniqueness check for nested attributes validate do @@ -15,7 +15,7 @@ class Measurement < ActiveRecord::Base end end - validates :name, presence: true, uniqueness: {scope: :project_id} + validates :name, presence: true after_initialize do if new_record? diff --git a/app/models/nutrient.rb b/app/models/nutrient.rb index 1abc0b1..ebbd1e6 100644 --- a/app/models/nutrient.rb +++ b/app/models/nutrient.rb @@ -4,7 +4,7 @@ class Nutrient < ActiveRecord::Base belongs_to :unit, required: true validates :quantity, uniqueness: {scope: :ingredient_id} - validates :amount, numericality: {greater_thani_or_equal_to: 0.0} + validates :amount, numericality: {greater_than_or_equal_to: 0.0} after_initialize do if new_record? diff --git a/app/models/readout.rb b/app/models/readout.rb index 506ba5e..96d67da 100644 --- a/app/models/readout.rb +++ b/app/models/readout.rb @@ -4,4 +4,5 @@ class Readout < ActiveRecord::Base belongs_to :unit, required: true validates :quantity, uniqueness: {scope: [:measurement_id, :unit_id]} + validates :value, numericality: true end diff --git a/app/views/ingredients/_form.html.erb b/app/views/ingredients/_form.html.erb index 1863df9..8ebdfec 100644 --- a/app/views/ingredients/_form.html.erb +++ b/app/views/ingredients/_form.html.erb @@ -67,7 +67,7 @@ function deleteNutrient() { var row = $(event.target).closest('p.nutrient'); row.find('[id$=_destroy]').val(1); - // FIXME: should only hide() row if record already saved (to send _destroy to backend) + // 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) { diff --git a/app/views/measurements/_form.html.erb b/app/views/measurements/_form.html.erb index 81b6fc2..47d6bfa 100644 --- a/app/views/measurements/_form.html.erb +++ b/app/views/measurements/_form.html.erb @@ -1,46 +1,35 @@ -
<%= f.text_field :name, size: 40, required: true %>
-<%= f.select :source_id, source_options, required: false, include_blank: true %>
- <% @measurement.readouts.each_with_index do |r, index| %> - <%= f.fields_for 'readouts_attributes', r, index: '' do |ff| %> -- <%= ff.select :quantity_id, quantity_options, - {include_blank: true, label: (index > 0 ? '' : :field_readouts)} %> - <%= ff.select :unit_id, unit_options, {label: ''} %> - <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> - <%= link_to t(".button_delete_readout"), '#', - class: 'icon icon-del', - style: (@measurement.readouts.length > 1 ? "" : "display:none"), - onclick: "deleteReadout(); return false;" %> -
- <% end %> - <% end %> -- <%= link_to t(".button_add_readout"), '#', class: 'icon icon-add', - onclick: 'addReadout(); return false;' %> +
<%= f.select :source_id, source_options, + {required: false, include_blank: t('.null_source')} %>
+<%= f.text_field :name, size: 40, required: true %>
+ <% @measurement.readouts.each_with_index do |r, index| %> + <%= f.fields_for 'readouts_attributes', r, index: '' do |ff| %> ++ <%= ff.select :quantity_id, quantity_options, + {include_blank: true, label: (index > 0 ? '' : :field_readouts)} %> + <%= ff.number_field :value, {size: 8, step: :any, label: ''} %> + <%= ff.select :unit_id, unit_options, {label: ''} %> + <%= ff.check_box :_destroy, {style: "display:none", label: ''} %> + <%= link_to t(".button_delete_readout"), '#', + class: 'icon icon-del', + style: (@measurement.readouts.length > 1 ? "" : "display:none"), + onclick: "deleteReadout(); return false;" %>
-+ <%= link_to t(".button_add_readout"), '#', class: 'icon icon-add', + onclick: 'addReadout(); return false;' %> +
+ <%= submit_tag l(:button_create) %> + <%= link_to l(:button_cancel), "#", + onclick: '$("#add-measurement").hide(); return false;' %> +
+