Class: ActiveGraph::Node::Validations::UniquenessValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveGraph::Node::Validations::UniquenessValidator
- Defined in:
- lib/active_graph/node/validations.rb
Instance Method Summary collapse
- #found(record, attribute, value) ⇒ Object
-
#initialize(options) ⇒ UniquenessValidator
constructor
A new instance of UniquenessValidator.
- #message(instance) ⇒ Object
- #scope_conditions(instance, attributes) ⇒ Object
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ UniquenessValidator
Returns a new instance of UniquenessValidator.
22 23 24 |
# File 'lib/active_graph/node/validations.rb', line 22 def initialize() super(.reverse_merge(case_sensitive: true)) end |
Instance Method Details
#found(record, attribute, value) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_graph/node/validations.rb', line 32 def found(record, attribute, value) scopes, attributes = Array([:scope] || []).partition { |s| s.is_a?(Proc) } conditions = scope_conditions(record, attributes) # TODO: Added as find(:name => nil) throws error value = '' if value.nil? conditions[attribute] = [:case_sensitive] ? value : /#{Regexp.escape(value.to_s)}/i found = if scopes.empty? record.class.as(:result) else scopes.reduce(record) { |proxy, scope| proxy.instance_eval(&scope) } end.where(conditions) found = found.where_not(neo_id: record.neo_id) if record._persisted_obj found end |
#message(instance) ⇒ Object
50 51 52 |
# File 'lib/active_graph/node/validations.rb', line 50 def (instance) super || 'has already been taken' end |
#scope_conditions(instance, attributes) ⇒ Object
54 55 56 57 58 |
# File 'lib/active_graph/node/validations.rb', line 54 def scope_conditions(instance, attributes) attributes.inject({}) do |conditions, key| conditions.merge(key => instance[key]) end end |
#validate_each(record, attribute, value) ⇒ Object
26 27 28 29 30 |
# File 'lib/active_graph/node/validations.rb', line 26 def validate_each(record, attribute, value) return unless found(record, attribute, value).exists? record.errors.add(attribute, :taken, **.except(:case_sensitive, :scope).merge(value: value)) end |