Module: ActiveRecord::Validations::ClassMethods
- Defined in:
- lib/adaptation/validateable.rb
Instance Method Summary collapse
-
#validates_as_email(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element has a valid email format.
-
#validates_value_of(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element is the expected one.
Instance Method Details
#validates_as_email(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element has a valid email format.
Example:
<contact email="[email protected]">...</contact>
class Contact < Adaptation::Message
has_one :attribute, :email
validates_as_email :email
end
110 111 112 113 114 115 116 117 118 |
# File 'lib/adaptation/validateable.rb', line 110 def validates_as_email(*attr_names) configuration = { :message => 'is an invalid email', :with => RFC822::EmailAddress, :allow_nil => true } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) validates_format_of attr_names, configuration end |
#validates_value_of(*attr_names) ⇒ Object
Validates whether the value of the specified xml attribute/element is the expected one.
Example 1:
<leftwing side="left"/>
class Leftwing < Adaptation::Message
has_one :attribute, :side
validates_value_of :side, "left"
end
Example 2:
<bird><wings><wing side="left"/><wing side="right"/></wings></bird>
class Bird < Adaptation::Message
has_many :wings
validates_value_of :side, "left", :in => :wings
end
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/adaptation/validateable.rb', line 73 def validates_value_of(*attr_names) configuration = { :message => 'value doesn\'t exist' } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) if configuration[:in].nil? validates_each(attr_names, configuration) do |record, attr_name, value| if (attr_names[1].to_s != record.send(attr_names[0].to_sym)) record.errors.add(attr_name, configuration[:message]) end end else validates_each(attr_names, configuration) do |record, attr_name, value| found = false record.send(configuration[:in]).each do |s| if (attr_names[1].to_s == s.send(attr_names[0].to_sym)) found = true break end end record.errors.add(attr_name, configuration[:message]) unless found end end end |