15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/pakyow/validations/data/unique.rb', line 15
def self.valid?(value, source:, **options)
query = options[:context].app.data.public_send(source).public_send(:"by_#{options[:key]}", value)
if updating = options[:updating]
if updating.is_a?(Pakyow::Data::Result)
query.count == 0 || query.any? { |result|
result[updating.__proxy.source.class.primary_key_field] == updating[updating.__proxy.source.class.primary_key_field]
}
else
raise ArgumentError, "Expected `#{updating.class}' to be a `Pakyow::Data::Result'"
end
else
query.count == 0
end
end
|