Module: ValidatorsReflectionHelper
- Defined in:
- app/helpers/validators_reflection_helper.rb
Instance Method Summary collapse
- #validated_value(record:, attr:, type: nil, options: []) ⇒ Integer, ...
- #validator_type_to_class(type) ⇒ Object
- #validators_for(record:, attr:, type: nil, options: []) ⇒ Object
- #validators_for_attr(record, attr) ⇒ Object
Instance Method Details
#validated_value(record:, attr:, type: nil, options: []) ⇒ Integer, ...
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 #{.class}" unless .is_a? Array validators_for(record: record, attr: attr, type: type, options: ).map { first_value(_1, ) }.compact.first end |
#validator_type_to_class(type) ⇒ Object
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
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 #{.class}" unless .is_a? Array result = validators_for_attr(record, attr).select { _1..keys & } result = result.select { _1.is_a? validator_type_to_class(type) } if type.present? result end |
#validators_for_attr(record, attr) ⇒ Object
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 |