Class: HC::Validators::ClassValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- HC::Validators::ClassValidator
- Defined in:
- lib/hc/validators/class.rb
Overview
Allows for ActiveRecord validation of class types
Instance Method Summary collapse
- #error(record, attribute) ⇒ Object
- #generate_message ⇒ Object
- #validate_each(record, attribute = nil, value = nil) ⇒ Object
Instance Method Details
#error(record, attribute) ⇒ Object
23 24 25 |
# File 'lib/hc/validators/class.rb', line 23 def error(record, attribute) record.errors.add(attribute, [:message] || ) end |
#generate_message ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/hc/validators/class.rb', line 27 def if [:is_a] "must be a #{options[:is_a]}" elsif [:in] "must be one of: #{options[:in]}" elsif [:array_of] "must be an array of #{options[:array_of]}" else :invalid end end |
#validate_each(record, attribute = nil, value = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/hc/validators/class.rb', line 10 def validate_each(record, attribute = nil, value = nil) = .merge(self.) if [:is_a] validate_one(, record, attribute, value) elsif [:in] validate_in(, record, attribute, value) elsif [:array_of] validate_array(, record, attribute, value) else raise 'Invalid validation options' end end |