1
0
This repository has been archived on 2023-12-07. You can view files and clone it, but cannot push or open issues or pull requests.
body_tracking/app/models/measurement.rb
2019-11-16 19:07:16 +01:00

30 lines
881 B
Ruby

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_unit_pair)
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