12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/apache_age/validators/unique_edge.rb', line 12
def validate(record)
allowed_keys = record.age_properties.keys
attributes = options[:attributes] || []
edge_attribs =
attributes
.map { |attr| [attr, record.send(attr)] }.to_h
.symbolize_keys
.slice(*allowed_keys)
possible_end_keys = [:end_id, 'end_id', :end_node, 'end_node']
end_query =
if possible_end_keys.any? { |key| attributes.include?(key) }
end_query = query_node(record.end_node)
edge_attribs[:end_id] = end_query&.id
end_query
end
possible_start_keys = [:start_id, 'start_id', :start_node, 'start_node']
start_query =
if possible_start_keys.any? { |key| attributes.include?(key) }
start_query = query_node(record.start_node)
edge_attribs[:start_id] = start_query&.id
start_query
end
return if attributes.blank? && (end_query.blank? || start_query.blank?)
query = record.class.find_by(edge_attribs.compact)
return if query.blank? || (query.id == record.id)
record.errors.add(:base, 'record not unique')
record.errors.add(:end_node, 'node combination not unique')
record.errors.add(:start_node, 'node combination not unique')
attributes.each { record.errors.add(_1, 'prpoerty combination not unique') }
end
|