1
0

Measurement/readouts WIP

This commit is contained in:
cryptogopher
2019-11-16 18:39:46 +01:00
parent eb379c7835
commit 22aeda720d
8 changed files with 97 additions and 2 deletions

View File

@@ -37,6 +37,10 @@ class Ingredient < ActiveRecord::Base
end
end
def toggle_hidden!
self.toggle!(:hidden)
end
def self.filter(project, filters = {}, requested_q = Quantity.none)
ingredients = all

View File

@@ -1,2 +1,29 @@
class Measurement < ActiveRecord::Base
belongs_to :project, required: true
belongs_to :source, required: false
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?
}
# Readout (quantity_id, unit_id) pair uniqueness check for nested attributes
validate do
quantities = self.readouts.map { |r| [r.quantity_id, r.unit_id] }
if quantities.length != quantities.uniq.length
errors.add(:readouts, :duplicated_quantity)
end
end
validates :name, presence: true, uniqueness: {scope: :project_id}
after_initialize do
if new_record?
self.hidden = false if self.hidden.nil?
end
end
def toggle_hidden!
self.toggle!(:hidden)
end
end

View File

@@ -1,2 +1,7 @@
class Readout < ActiveRecord::Base
belongs_to :measurement, inverse_of: :readouts, required: true
belongs_to :quantity, required: true
belongs_to :unit, required: true
validates :quantity, uniqueness: {scope: [:ingredient_id, :unit_id]}
end