Class: DatabaseValidations::BelongsToOptions
- Inherits:
-
Object
- Object
- DatabaseValidations::BelongsToOptions
- Defined in:
- lib/database_validations/validations/belongs_to_options.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #column_and_relation_blank_for?(instance) ⇒ Boolean
- #handle_foreign_key_error(instance) ⇒ Object
-
#initialize(column, relation, adapter) ⇒ BelongsToOptions
constructor
A new instance of BelongsToOptions.
- #key ⇒ String
- #validates_presence_options ⇒ Object
Constructor Details
#initialize(column, relation, adapter) ⇒ BelongsToOptions
Returns a new instance of BelongsToOptions.
5 6 7 8 9 10 11 12 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 5 def initialize(column, relation, adapter) @column = column @relation = relation @adapter = adapter raise_if_unsupported_database! raise_if_foreign_key_missed! unless ENV['SKIP_DB_UNIQUENESS_VALIDATOR_INDEX_CHECK'] end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
3 4 5 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 3 def adapter @adapter end |
#column ⇒ Object (readonly)
Returns the value of attribute column.
3 4 5 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 3 def column @column end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
3 4 5 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 3 def relation @relation end |
Instance Method Details
#column_and_relation_blank_for?(instance) ⇒ Boolean
20 21 22 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 20 def column_and_relation_blank_for?(instance) instance.public_send(column).blank? && instance.public_send(relation).blank? end |
#handle_foreign_key_error(instance) ⇒ Object
24 25 26 27 28 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 24 def handle_foreign_key_error(instance) # Hack to not query the database because we know the result already instance.send("#{relation}=", nil) instance.errors.add(relation, :blank, message: :required) end |
#key ⇒ String
15 16 17 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 15 def key Helpers.generate_key_for_belongs_to(column) end |
#validates_presence_options ⇒ Object
30 31 32 |
# File 'lib/database_validations/validations/belongs_to_options.rb', line 30 def { attributes: relation, message: :required } end |