Module: Ardm::Ar::Validations::ClassMethods
- Defined in:
- lib/ardm/ar/validations.rb
Instance Method Summary collapse
- #validates_belongs_to(*fields) ⇒ Object
- #validates_length_of(*fields) ⇒ Object
- #validates_presence_of(*fields) ⇒ Object
- #validates_uniqueness_of(*fields) ⇒ Object
- #validates_with_block(att = nil, &block) ⇒ Object
-
#validates_with_method(*fields) ⇒ Object
Possible formats:.
- #validates_within(*fields) ⇒ Object
Instance Method Details
#validates_belongs_to(*fields) ⇒ Object
36 37 38 39 |
# File 'lib/ardm/ar/validations.rb', line 36 def validates_belongs_to(*fields) fields, = Ardm::Ar::Validations.(fields) validates *fields, presence: end |
#validates_length_of(*fields) ⇒ Object
46 47 48 49 |
# File 'lib/ardm/ar/validations.rb', line 46 def validates_length_of(*fields) fields, = Ardm::Ar::Validations.(fields, :in, :within, :maximum, :minimum, :is) validates *fields, length: end |
#validates_presence_of(*fields) ⇒ Object
41 42 43 44 |
# File 'lib/ardm/ar/validations.rb', line 41 def validates_presence_of(*fields) fields, = Ardm::Ar::Validations.(fields) validates *fields, presence: end |
#validates_uniqueness_of(*fields) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/ardm/ar/validations.rb', line 51 def validates_uniqueness_of(*fields) fields, = Ardm::Ar::Validations.(fields, :scope) fields = fields.map do |field| if property = properties[field] property.field elsif assoc = reflect_on_association(field) assoc.foreign_key else field end end if [:scope] [:scope] = Array([:scope]).map do |scope| assoc = reflect_on_association(scope) assoc ? assoc.foreign_key : scope end end validates *fields, uniqueness: end |
#validates_with_block(att = nil, &block) ⇒ Object
100 101 102 103 104 105 106 107 |
# File 'lib/ardm/ar/validations.rb', line 100 def validates_with_block(att=nil, &block) validate do |record| val, = instance_eval(&block) unless val errors.add(att, ) end end end |
#validates_with_method(*fields) ⇒ Object
Possible formats:
validates_with_method :attr, :method_name validates_with_method :attr, method: :method_name validates_with_method :method_name, options: “here”
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ardm/ar/validations.rb', line 83 def validates_with_method(*fields) fields, = Ardm::Ar::Validations.(fields, :method) # validates_with_method :attr, :method_name att, meth = *fields # validates_with_method :attr, method: :method_name meth = [:method] if [:method] # validates_with_method :method_name att, meth = :base, att if !meth validates_with_block(att) do self.send(meth) end end |
#validates_within(*fields) ⇒ Object
73 74 75 76 |
# File 'lib/ardm/ar/validations.rb', line 73 def validates_within(*fields) fields, = Ardm::Ar::Validations.(fields, :in) validates *fields, inclusion: end |