Class: DatabaseValidations::BelongsToOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/database_validations/lib/db_belongs_to/belongs_to_options.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, relation, adapter) ⇒ BelongsToOptions

Returns a new instance of BelongsToOptions.



9
10
11
12
13
14
15
16
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 9

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

#adapterObject (readonly)

Returns the value of attribute adapter.



7
8
9
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 7

def adapter
  @adapter
end

#columnObject (readonly)

Returns the value of attribute column.



7
8
9
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 7

def column
  @column
end

#relationObject (readonly)

Returns the value of attribute relation.



7
8
9
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 7

def relation
  @relation
end

Class Method Details

.validator_options(association, foreign_key) ⇒ Object



3
4
5
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 3

def self.validator_options(association, foreign_key)
  { attributes: association, foreign_key: foreign_key, message: :required }
end

Instance Method Details

#handle_foreign_key_error(instance) ⇒ Object



23
24
25
26
27
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 23

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

#keyString

Returns:

  • (String)


19
20
21
# File 'lib/database_validations/lib/db_belongs_to/belongs_to_options.rb', line 19

def key
  @key ||= Helpers.generate_key_for_belongs_to(column)
end