Permalink
Browse files

Move :with/:without check outside the method generated by validates_f…

…ormat_of
  • Loading branch information...
1 parent cccb0e6 commit e202c6c814886251d3c7a9b6a33ba6a8f1a2d448 @lifo lifo committed Aug 10, 2009
Showing with 6 additions and 6 deletions.
  1. +6 −6 activemodel/lib/active_model/validations/format.rb
@@ -47,13 +47,13 @@ def validates_format_of(*attr_names)
raise ArgumentError, "A regular expression must be supplied as the :without option of the configuration hash"
end
- validates_each(attr_names, configuration) do |record, attr_name, value|
- if configuration[:with] && value.to_s !~ configuration[:with]
- record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
+ if configuration[:with]
+ validates_each(attr_names, configuration) do |record, attr_name, value|
+ record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) if value.to_s !~ configuration[:with]
end
-
- if configuration[:without] && value.to_s =~ configuration[:without]
- record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
+ elsif configuration[:without]
+ validates_each(attr_names, configuration) do |record, attr_name, value|
+ record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value) if value.to_s =~ configuration[:without]
end
end
end

2 comments on commit e202c6c

Contributor

Great work Pratik!

Don't you think it needs some tests too?

Member
lifo replied Aug 11, 2009

This is simple refactoring that's not changing any behaviour or fixing any bug. So passing of existing tests is enough :)

Please sign in to comment.