1
0

WIP Targets #edit/#update

NestedUniqueness properly restores collection.target
This commit is contained in:
cryptogopher
2021-04-23 19:19:44 +02:00
parent 37e3b98788
commit bea0a8371d
9 changed files with 93 additions and 40 deletions

View File

@@ -17,6 +17,7 @@ class Target < ActiveRecord::Base
errors.add(:thresholds, :quantity_mismatch) unless quantities == ancestors
end
#validates :scope, inclusion: {in: [:ingredient, :meal, :day], if: -> { quantity&.diet? }}
# FIXME: does not seem to work
validates :effective_from, presence: {if: :is_binding?}, absence: {unless: :is_binding?}
after_initialize do

View File

@@ -44,10 +44,10 @@ module Validations::NestedUniqueness
def before_validation_nested_uniqueness
nested_uniqueness_options.each do |association_name, options|
collection = send(association_name)
was_loaded = collection.loaded?
records = collection.target.select(&:changed?)
records = collection.target.dup unless collection.loaded?
preserved_records = records.reject(&:marked_for_destruction?)
preserved_records = records.select(&:changed?).reject(&:marked_for_destruction?)
# TODO: do scoping on options[:attributes] and remove options[:scope]
scope = options[:scope]&.map { |attr| [attr, preserved_records.map(&attr).uniq] }.to_h
seen = {}
@@ -62,7 +62,7 @@ module Validations::NestedUniqueness
end
end
end
unless was_loaded
if records
collection.proxy_association.reset
records.each { |r| collection.proxy_association.add_to_target(r) }
end