Class: DatabaseValidations::DbUniquenessValidator
- Inherits:
-
ActiveRecord::Validations::UniquenessValidator
- Object
- ActiveRecord::Validations::UniquenessValidator
- DatabaseValidations::DbUniquenessValidator
- Defined in:
- lib/database_validations/lib/validators/db_uniqueness_validator.rb
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.
- #validate(record) ⇒ Object
Constructor Details
#initialize(options) ⇒ DbUniquenessValidator
Returns a new instance of DbUniquenessValidator.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 13 def initialize() [:allow_nil] = true [:allow_blank] = false if .key?(:where) condition = [:where] [:conditions] = -> { where(condition) } end @index_name = .delete(:index_name) if .key?(:index_name) @where = .delete(:where) if .key?(:where) super Injector.inject(klass) Checkers::DbUniquenessValidator.validate!(self) end |
Instance Attribute Details
#index_name ⇒ Object (readonly)
Returns the value of attribute index_name.
3 4 5 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 3 def index_name @index_name end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 3 def klass @klass end |
#where ⇒ Object (readonly)
Returns the value of attribute where.
3 4 5 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 3 def where @where end |
Class Method Details
.kind ⇒ Symbol
Used to make 3rd party libraries work correctly
8 9 10 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 8 def self.kind :uniqueness end |
Instance Method Details
#apply_error(instance, attribute) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 35 def apply_error(instance, attribute) = .except(:case_sensitive, :scope, :conditions) [:value] = instance.public_send(attribute) instance.errors.add(attribute, :taken, ) end |
#validate(record) ⇒ Object
31 32 33 |
# File 'lib/database_validations/lib/validators/db_uniqueness_validator.rb', line 31 def validate(record) super if record._database_validations_fallback end |