Module: DatabaseValidations::BelongsToHandlers

Extended by:
ActiveSupport::Concern
Defined in:
lib/database_validations/validations/belongs_to_handlers.rb

Instance Method Summary collapse

Instance Method Details

#save(opts = {}) ⇒ Object



21
22
23
24
25
26
# File 'lib/database_validations/validations/belongs_to_handlers.rb', line 21

def save(opts = {})
  ActiveRecord::Base.connection.transaction(requires_new: true) { super }
rescue ActiveRecord::InvalidForeignKey => e
  Helpers.handle_foreign_key_error!(self, e)
  false
end

#save!(opts = {}) ⇒ Object



28
29
30
31
32
33
# File 'lib/database_validations/validations/belongs_to_handlers.rb', line 28

def save!(opts = {})
  ActiveRecord::Base.connection.transaction(requires_new: true) { super }
rescue ActiveRecord::InvalidForeignKey => e
  Helpers.handle_foreign_key_error!(self, e)
  raise ActiveRecord::RecordInvalid, self
end

#valid?(context = nil) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/database_validations/validations/belongs_to_handlers.rb', line 9

def valid?(context = nil)
  output = super(context)

  Helpers.each_belongs_to_presence_validator(self.class) do |validator|
    next if validator.column_and_relation_blank_for?(self)

    validates_with(ActiveRecord::Validations::PresenceValidator, validator.validates_presence_options)
  end

  errors.empty? && output
end