Updated Measurement to reflect ReadoutValue removal
This commit is contained in:
parent
d74f290a4f
commit
fc2db76a3e
@ -49,6 +49,7 @@ class MeasurementsController < ApplicationController
|
||||
[
|
||||
:id,
|
||||
:quantity_id,
|
||||
:value,
|
||||
:unit_id,
|
||||
:_destroy
|
||||
]
|
||||
|
@ -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?
|
||||
|
@ -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?
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -1,46 +1,35 @@
|
||||
<div id="add-measurement" <%= 'style=display:none;' if @measurement.errors.empty? %>>
|
||||
<h2><%= t ".heading_new_measurement" %></h2>
|
||||
<%= error_messages_for @measurement %>
|
||||
|
||||
<%= labelled_form_for @measurement,
|
||||
url: project_measurements_path(@project),
|
||||
html: {id: 'measurement-add-form', name: 'measurement-add-form'} do |f| %>
|
||||
|
||||
<%= error_messages_for @measurement %>
|
||||
|
||||
<div class="box tabular">
|
||||
<p><%= f.text_field :name, size: 40, required: true %></p>
|
||||
<p><%= f.select :source_id, source_options, required: false, include_blank: true %></p>
|
||||
<% @measurement.readouts.each_with_index do |r, index| %>
|
||||
<%= f.fields_for 'readouts_attributes', r, index: '' do |ff| %>
|
||||
<p class="readout">
|
||||
<%= 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;" %>
|
||||
</p>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<p>
|
||||
<%= link_to t(".button_add_readout"), '#', class: 'icon icon-add',
|
||||
onclick: 'addReadout(); return false;' %>
|
||||
<div class="box tabular">
|
||||
<p><%= f.select :source_id, source_options,
|
||||
{required: false, include_blank: t('.null_source')} %></p>
|
||||
<p><%= f.text_field :name, size: 40, required: true %></p>
|
||||
<% @measurement.readouts.each_with_index do |r, index| %>
|
||||
<%= f.fields_for 'readouts_attributes', r, index: '' do |ff| %>
|
||||
<p class="readout">
|
||||
<%= 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;" %>
|
||||
</p>
|
||||
</div>
|
||||
<%= submit_tag l(:button_create) %>
|
||||
<%= link_to l(:button_cancel), "#",
|
||||
onclick: '$("#add-measurement").hide(); return false;' %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<hr>
|
||||
<p>
|
||||
<%= link_to t(".button_add_readout"), '#', class: 'icon icon-add',
|
||||
onclick: 'addReadout(); return false;' %>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<%= javascript_tag do %>
|
||||
function addReadout() {
|
||||
var row = $('p.readout:visible:last');
|
||||
var new_row = row.clone().insertAfter(row);
|
||||
new_row.find('select[id$=_quantity_id]').val('');
|
||||
new_row.find('input[id$=_value], select[id$=_quantity_id]').val('');
|
||||
new_row.find('select[id$=_unit_id]').val(row.find('select[id$=_unit_id]').val());
|
||||
new_row.find('label:first').text('');
|
||||
if ($('p.readout:visible').length > 1) {
|
||||
|
@ -9,9 +9,27 @@
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
<%= render partial: 'measurements/form' %>
|
||||
<div id="add-measurement" <%= 'style=display:none;' if @measurement.errors.empty? %>>
|
||||
<h2><%= t ".heading_new_measurement" %></h2>
|
||||
|
||||
<%= labelled_form_for @measurement,
|
||||
url: project_measurements_path(@project),
|
||||
html: {id: 'measurement-add-form', name: 'measurement-add-form'} do |f| %>
|
||||
|
||||
<%= render partial: 'measurements/form', locals: {f: f} %>
|
||||
|
||||
<div class="tabular">
|
||||
<p>
|
||||
<%= submit_tag l(:button_create) %>
|
||||
<%= link_to l(:button_cancel), "#",
|
||||
onclick: '$("#add-measurement").hide(); return false;' %>
|
||||
</p>
|
||||
</div>
|
||||
<% end %>
|
||||
<hr>
|
||||
</div>
|
||||
|
||||
<h2><%= t ".heading" %></h2>
|
||||
<div id='measurements'>
|
||||
<%= render partial: 'measurements/list' %>
|
||||
<%= render partial: 'measurements/index' %>
|
||||
</div>
|
||||
|
@ -60,6 +60,7 @@ en:
|
||||
heading_new_measurement: 'New measurement'
|
||||
button_add_readout: 'Add readout'
|
||||
button_delete_readout: 'Delete'
|
||||
null_source: '- unspecified -'
|
||||
index:
|
||||
heading: 'Measurements'
|
||||
link_new_measurement: 'New measurement'
|
||||
|
Reference in New Issue
Block a user