Sha256: 3c10163363b6bbdb929f70a5a6acdced891c6c020e5fa48b46997d8bc970a148

Contents?: true

Size: 993 Bytes

Versions: 5

Compression:

Stored size: 993 Bytes

Contents

# # lib/apache_age/validators/unique_vertex.rb

# # Usage (within an Age Model)
# # validates_with(
# #   ApacheAge::Validators::UniqueVertex,
# #   attributes: [:first_name, :last_name, :gender]
# # )

# module ApacheAge
#   module Validators
#     class UniqueVertex < ActiveModel::Validator
#       def validate(record)
#         allowed_keys = record.age_properties.keys
#         attributes = options[:attributes]
#         return if attributes.blank?

#         record_attribs =
#           attributes
#           .map { |attr| [attr, record.send(attr)] }
#           .to_h.symbolize_keys
#           .slice(*allowed_keys)
#         query = record.class.find_by(record_attribs)

#         # if no match is found or if it finds itself, it's valid
#         return if query.blank?  || (query.id == record.id)

#         record.errors.add(:base, 'record not unique')
#         attributes.each { record.errors.add(_1, 'property combination not unique') }
#       end
#     end
#   end
# end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_age-0.6.4 lib/apache_age/validators/unique_vertex.rb
rails_age-0.6.3 lib/apache_age/validators/unique_vertex.rb
rails_age-0.6.2 lib/apache_age/validators/unique_vertex.rb
rails_age-0.6.1 lib/apache_age/validators/unique_vertex.rb
rails_age-0.6.0 lib/apache_age/validators/unique_vertex.rb