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(*args, &block) ⇒ Object
-
#validates_with_method(*fields) ⇒ Object
Possible formats:.
- #validates_within(*fields) ⇒ Object
Instance Method Details
#validates_belongs_to(*fields) ⇒ Object
40 41 42 43 |
# File 'lib/ardm/ar/validations.rb', line 40 def validates_belongs_to(*fields) fields, = Ardm::Ar::Validations.(fields) validates *fields, presence: end |
#validates_length_of(*fields) ⇒ Object
62 63 64 65 |
# File 'lib/ardm/ar/validations.rb', line 62 def validates_length_of(*fields) fields, = Ardm::Ar::Validations.(fields, :in, :within, :maximum, :minimum, :is) validates *fields, length: end |
#validates_presence_of(*fields) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/ardm/ar/validations.rb', line 45 def validates_presence_of(*fields) fields, = Ardm::Ar::Validations.(fields) boolean_fields, non_boolean_fields = fields.partition { |f| self.properties[f.to_sym].is_a?(Ardm::Property::Boolean) } if non_boolean_fields.any? validates(*non_boolean_fields, presence: ) end if boolean_fields.any? if .any? $stderr.puts "validates_presence_of options ignored: #{.inspect}" end validates(*boolean_fields, inclusion: {in: [true, false]}) end end |
#validates_uniqueness_of(*fields) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ardm/ar/validations.rb', line 67 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(*args, &block) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ardm/ar/validations.rb', line 116 def validates_with_block(*args, &block) = args. attribute = args.shift validate() do |_| is_valid, = instance_eval(&block) unless is_valid attribute ||= :base if attribute.to_sym == :base raise "message is blank #{args.inspect} #{self.inspect} #{block.inspect}" if .blank? end errors.add(attribute, ) 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”
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ardm/ar/validations.rb', line 99 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
89 90 91 92 |
# File 'lib/ardm/ar/validations.rb', line 89 def validates_within(*fields) fields, = Ardm::Ar::Validations.(fields, :in) validates *fields, inclusion: end |