Module: ValidatorsReflectionHelper

Defined in:
app/helpers/validators_reflection_helper.rb

Instance Method Summary collapse

Instance Method Details

#validated_value(record:, attr:, type: nil, options: []) ⇒ Integer, ...

Examples:

validated_value(record: @review, attr: :comment, type: :length, options: [:maximm]) #=> 255

Parameters:

  • record (ActiveRecord::Base)
  • attr (String, Symbol)

Returns:

  • (Integer, NilClass, Boolean)

Raises:

  • (TypeError)
  • (TypeError)


46
47
48
49
# File 'app/helpers/validators_reflection_helper.rb', line 46

def validated_value(record:, attr:, type: nil, options: [])
	fail TypeError, "`options` must be an Array, you pass a #{options.class}" unless options.is_a? Array
	validators_for(record: record, attr: attr, type: type, options: options).map { first_value(_1, options) }.compact.first
end

#validator_type_to_class(type) ⇒ Object

Examples:

validator_type_to_class(:presence)     #=> ActiveRecord::Validations::PresenceValidator
validator_type_to_class(:numericality) #=> ActiveRecord::Validations::NumericalityValidator

Parameters:

  • type (String, Symbol, NilClass)


56
57
58
59
# File 'app/helpers/validators_reflection_helper.rb', line 56

def validator_type_to_class(type)
	fail TypeError, "`type` must be a Symbol or String, you pass a #{type.class}" unless [Symbol, String, NilClass].include? type.class
	["ActiveRecord::Validations::", type.to_s.capitalize, "Validator"].join.constantize if type.present?
end

#validators_for(record:, attr:, type: nil, options: []) ⇒ Object

Parameters:

  • record (ActiveRecord::Base)
  • attr (String, Symbol)
  • options (Array<Symbol>) (defaults to: [])

Raises:

  • (TypeError)


31
32
33
34
35
36
# File 'app/helpers/validators_reflection_helper.rb', line 31

def validators_for(record:, attr:, type: nil, options: [])
	fail TypeError, "`options` must be an Array, you pass a #{options.class}" unless options.is_a? Array
	result = validators_for_attr(record, attr).select { _1.options.keys & options }
	result = result.select { _1.is_a? validator_type_to_class(type) } if type.present?
	result
end

#validators_for_attr(record, attr) ⇒ Object

Examples:

validators_for_attr(User.new, :email) #=>
[
  <ActiveRecord::Validations::PresenceValidator:0x @attributes=[:email], @options={}>,
  <EmailValidator:0x @attributes=[:email], @options={}>,
  <ActiveRecord::Validations::UniquenessValidator:0x @attributes=[:email], @options={}>,
  <ActiveRecord::Validations::PresenceValidator:0x @attributes=[:email], @options={:if=>:email_required?}>,
  <ActiveRecord::Validations::UniquenessValidator:0x @attributes=[:email], @options={:allow_blank=>true, :case_sensitive=>true, :if=>:will_save_change_to_email?}>,
  <ActiveModel::Validations::FormatValidator:0x @attributes=[:email], @options= {:allow_blank=>true, :if=>:will_save_change_to_email?}>
]

Parameters:

  • record (ActiveRecord::Base)
  • attr (String, Symbol)

Raises:

  • (TypeError)


19
20
21
22
23
24
# File 'app/helpers/validators_reflection_helper.rb', line 19

def validators_for_attr(record, attr)
	fail TypeError, "`attr` must be a Symbol or String, you pass a #{attr.class}" unless [Symbol, String].include? attr.class
	# TODO: fail TypeError if record.class.ancestors.include?()

	record.class.validators.select { _1.attributes.include? attr }
end