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.
cryptogopher 0b9401b089 Proper nested records uniqueness validation
Test pass: test_create_duplicate_for_persisted_target_should_fail
2021-04-19 00:38:28 +02:00

34 lines
1.2 KiB
Ruby

class Goal < ActiveRecord::Base
belongs_to :project, required: true
has_many :targets, -> { order effective_from: :desc }, inverse_of: :goal,
dependent: :destroy, extend: BodyTracking::ItemsWithQuantities
has_many :exposures, as: :view, dependent: :destroy,
class_name: 'Exposure', extend: BodyTracking::TogglableExposures
has_many :quantities, -> { order "lft" }, through: :exposures
accepts_nested_attributes_for :targets, allow_destroy: true
include Validations::NestedUniqueness
validates_nested_uniqueness_for :targets,
:effective_from, :quantity_id, :item_type, :item_id, :scope,
scope: [:effective_from]
validates :is_binding, uniqueness: {scope: :project_id}, if: :is_binding?
validates :name, presence: true, uniqueness: {scope: :project_id},
exclusion: {in: [I18n.t('goals.binding.name')], unless: :is_binding?}
after_initialize do
if new_record?
self.is_binding = false if self.is_binding.nil?
self.targets.new if !self.is_binding && self.targets.empty?
end
end
before_save do
quantities << targets.map(&:quantity)[0..5] if exposures.empty?
end
before_destroy prepend: true do
!is_binding?
end
end