module Validateable
[:save, :save!, :update_attribute].each{|attr| define_method(attr){}}
def method_missing(symbol, *params)
if(symbol.to_s =~ /(.*)_before_type_cast$/)
send($1)
end
end
def self.append_features(base)
super
base.send(:include, ActiveRecord::Validations)
end
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods
# Define class methods here.
def self_and_descendents_from_active_record#nodoc:
klass = self
classes = [klass]
while klass != klass.base_class
classes << klass = klass.superclass
end
classes
rescue
[self]
end
def human_name
""
end
def human_attribute_name(attribute_key_name, options = {})
""
end
end
end
module ActiveRecord
module Validations
module ClassMethods
# Validates whether the value of the specified xml attribute/element is the expected one.
#
# Example 1:
#
#
#
# class Leftwing < Adaptation::Message
# has_one :attribute, :side
#
# validates_value_of :side, "left"
# end
#
# Example 2:
#
#
# class Bird < Adaptation::Message
# has_many :wings
#
# validates_value_of :side, "left", :in => :wings
# end
#
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
# Validates whether the value of the specified xml attribute/element has a valid email format.
#
# Example:
# ...
#
# class Contact < Adaptation::Message
# has_one :attribute, :email
#
# validates_as_email :email
# end
#
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
end
end
end