Class: DatabaseValidations::Validations::BelongsToPresenceValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/database_validations/validations/belongs_to_presence_validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BelongsToPresenceValidator

Returns a new instance of BelongsToPresenceValidator.



13
14
15
16
# File 'lib/database_validations/validations/belongs_to_presence_validator.rb', line 13

def initialize(options = {})
  @attributes = [options[:relation]]
  super
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/database_validations/validations/belongs_to_presence_validator.rb', line 4

def attributes
  @attributes
end

Class Method Details

.kindObject

This is a hack to simulate presence validator It’s used for cases when some 3rd parties are relies on the validators For example, required option from simple_form checks the validator



9
10
11
# File 'lib/database_validations/validations/belongs_to_presence_validator.rb', line 9

def self.kind
  :presence
end

Instance Method Details

#validate(record) ⇒ Object



18
19
20
21
22
# File 'lib/database_validations/validations/belongs_to_presence_validator.rb', line 18

def validate(record)
  return unless record.public_send(options[:column]).blank? && record.public_send(options[:relation]).blank?

  record.errors.add(options[:relation], :blank, message: :required)
end