Class: ActiveEntity::Validations::UniquenessOnActiveRecordValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- ActiveEntity::Validations::UniquenessOnActiveRecordValidator
- Defined in:
- lib/active_entity/validations/uniqueness_on_active_record.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#initialize(options) ⇒ UniquenessOnActiveRecordValidator
constructor
A new instance of UniquenessOnActiveRecordValidator.
- #validate_each(record, attribute, value) ⇒ Object
Constructor Details
#initialize(options) ⇒ UniquenessOnActiveRecordValidator
Returns a new instance of UniquenessOnActiveRecordValidator.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_entity/validations/uniqueness_on_active_record.rb', line 6 def initialize() if [:conditions] && ![:conditions].respond_to?(:call) raise ArgumentError, "#{[:conditions]} was passed as :conditions but is not callable. " \ "Pass a callable instead: `conditions: -> { where(approved: true) }`" end unless Array([:scope]).all? { |scope| scope.respond_to?(:to_sym) } raise ArgumentError, "#{[:scope]} is not supported format for :scope option. " \ "Pass a symbol or an array of symbols instead: `scope: :user_id`" end super @klass = if [:class].present? [:class] elsif [:class_name].present? [:class_name].safe_constantize else nil end unless @klass raise ArgumentError, "Must provide one of option :class_name or :class." end unless @klass < ActiveRecord::Base raise ArgumentError, "Class must be an Active Entity model, but got #{@klass}." end if @klass.abstract_class? raise ArgumentError, "Class can't be an abstract class." end end |
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_entity/validations/uniqueness_on_active_record.rb', line 37 def validate_each(record, attribute, value) finder_class = find_finder_class_for(record) value = map_enum_attribute(finder_class, attribute, value) relation = build_relation(finder_class, attribute, value) relation = scope_relation(record, relation) relation = relation.merge([:conditions]) if [:conditions] if relation.exists? = .except(:case_sensitive, :scope, :conditions) [:value] = value record.errors.add(attribute, :taken, **) end end |