Sha256: 1ce7db8044c95f295260891520606731eb2d2c7135312b41355c5967cc6e3fae
Contents?: true
Size: 912 Bytes
Versions: 6
Compression:
Stored size: 912 Bytes
Contents
# lib/apache_age/validators/unique_node.rb # Usage: # validates_with( # ApacheAge::Validators::UniqueNode, # attributes: [:first_name, :last_name, :gender] # ) module ApacheAge module Validators class UniqueNode < 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
6 entries across 6 versions & 1 rubygems