Class: DatabaseValidations::DbUniquenessValidator
- Inherits:
-
ActiveRecord::Validations::UniquenessValidator
- Object
- ActiveRecord::Validations::UniquenessValidator
- DatabaseValidations::DbUniquenessValidator
- Defined in:
- lib/database_validations/lib/validators/db_uniqueness_validator.rb
Constant Summary collapse
- DEFAULT_MODE =
:optimized
Instance Attribute Summary collapse
-
#index_name ⇒ Object
readonly
Returns the value of attribute index_name.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#where ⇒ Object
readonly
Returns the value of attribute where.
Class Method Summary collapse
-
.kind ⇒ Symbol
Used to make 3rd party libraries work correctly.
Instance Method Summary collapse
- #apply_error(instance, attribute) ⇒ Object
-
#initialize(options) ⇒ DbUniquenessValidator
constructor
A new instance of DbUniquenessValidator.
- #perform_db_validation? ⇒ Boolean
- #validate(record) ⇒ Object
Constructor Details
#initialize(options) ⇒ DbUniquenessValidator
Returns a new instance of DbUniquenessValidator.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 15 def initialize() [:allow_nil] = true [:allow_blank] = false if .key?(:where) condition = [:where] [:conditions] = -> { where(condition) } end () super Injector.inject(klass) Checkers::DbUniquenessValidator.validate!(self) end |
Instance Attribute Details
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
5 6 7 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 5 def index_name @index_name end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
5 6 7 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 5 def klass @klass end |
#where ⇒ Object (readonly)
Returns the value of attribute where.
5 6 7 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 5 def where @where end |
Class Method Details
.kind ⇒ Symbol
Used to make 3rd party libraries work correctly
10 11 12 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 10 def self.kind :uniqueness end |
Instance Method Details
#apply_error(instance, attribute) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 40 def apply_error(instance, attribute) = .except(:case_sensitive, :scope, :conditions) [:value] = instance.public_send(attribute) instance.errors.add(attribute, :taken, ) end |
#perform_db_validation? ⇒ Boolean
32 33 34 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 32 def perform_db_validation? @mode != :standard end |
#validate(record) ⇒ Object
36 37 38 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 36 def validate(record) super if perform_query? || record._database_validations_fallback end |