class ActiveGraph::Node::Validations::UniquenessValidator
Public Class Methods
new(options)
click to toggle source
Calls superclass method
# File lib/active_graph/node/validations.rb 24 def initialize(options) 25 super(options.reverse_merge(case_sensitive: true)) 26 end
Public Instance Methods
found(record, attribute, value)
click to toggle source
# File lib/active_graph/node/validations.rb 34 def found(record, attribute, value) 35 conditions = scope_conditions(record) 36 37 # TODO: Added as find(:name => nil) throws error 38 value = '' if value.nil? 39 40 conditions[attribute] = options[:case_sensitive] ? value : /#{Regexp.escape(value.to_s)}/i 41 42 found = record.class.as(:result).where(conditions) 43 found = found.where_not(neo_id: record.neo_id) if record._persisted_obj 44 found 45 end
message(instance)
click to toggle source
Calls superclass method
# File lib/active_graph/node/validations.rb 47 def message(instance) 48 super || 'has already been taken' 49 end
scope_conditions(instance)
click to toggle source
# File lib/active_graph/node/validations.rb 51 def scope_conditions(instance) 52 Array(options[:scope] || []).inject({}) do |conditions, key| 53 conditions.merge(key => instance[key]) 54 end 55 end
validate_each(record, attribute, value)
click to toggle source
# File lib/active_graph/node/validations.rb 28 def validate_each(record, attribute, value) 29 return unless found(record, attribute, value).exists? 30 31 record.errors.add(attribute, :taken, **options.except(:case_sensitive, :scope).merge(value: value)) 32 end