Custom validations aren't scary. Adding a class method like
def validates_foo(*attr_names)
options = attr_names.extract_options!
validates_each(attr_names, options) do |record, attr_name, value|
record.errors.add(attr_name, options[:message]) if ...
end
end
end
def self.validates_is_exact(*attr_names)
options = attr_names.extract_options!
validates_each(*(attr_names << options)) do |record, attr_name, value|
if record.send( options[:compare_field] ) == value
record.errors.add(attr_name, options[:message])
end
end
true
end
validates_is_exact :name, :compare_field => :controller_name, :if =>
:redirect?, :message => "must have the same name as the controller."
def self.validates_is_exact(*attr_names)
options = attr_names.extract_options!
validates_each(*(attr_names << options)) do |record, attr_name, value|
if record.send( options[:compare_field] ) != value
record.errors.add(attr_name, options[:message])
end
end
true
end
validates_is_exact :name, :compare_field => :controller_name, :if =>
:redirect?, :message => "must have the same name as the controller."
Basically, it compares the :name field value with the :compare_field
field.value and if they are the same, validation is successful. If they
are different, it throws my custom message.
Thanks again Fred. It was much easier than I anticipated.