multiple_of for Numericality #7213

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+16 −0
Diff settings

Always

Just for now

View
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##
+* Added `:multiple_of` options to `NumericalityValidator` *Brian Cardarella*
+
* Changed `AM::Serializers::JSON.include_root_in_json' default value to false.
Now, AM Serializers and AR objects have the same default behaviour. Fixes #6578.
@@ -26,3 +26,4 @@ en:
other_than: "must be other than %{count}"
odd: "must be odd"
even: "must be even"
+ multiple_of: "must be a multiple of %{multiple_of}"
@@ -37,6 +37,12 @@ def validate_each(record, attr_name, value)
end
end
+ if options[:multiple_of]
+ unless value % options[:multiple_of] == 0

This comment has been minimized.

@nashby

nashby Jul 31, 2012

Contributor

not sure but maybe it make sense to use multiple_of method from ActiveSupport? https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/integer/multiple.rb

@nashby

nashby Jul 31, 2012

Contributor

not sure but maybe it make sense to use multiple_of method from ActiveSupport? https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/integer/multiple.rb

This comment has been minimized.

@bcardarella

bcardarella Jul 31, 2012

Contributor

I would be fine with that by #multiple_of is only supported on Integer right now. Modulo works with floats as well.

@bcardarella

bcardarella Jul 31, 2012

Contributor

I would be fine with that by #multiple_of is only supported on Integer right now. Modulo works with floats as well.

+ record.errors.add(attr_name, :multiple_of, filtered_options(raw_value))
+ end
+ end
+
options.slice(*CHECKS.keys).each do |option, option_value|
case option
when :odd, :even
@@ -113,6 +113,13 @@ def test_validates_numericality_with_other_than
valid!([-1, 42])
end
+ def test_validates_numericality_with_multiple_of
+ Topic.validates_numericality_of :approved, :multiple_of => 2

This comment has been minimized.

@frodsan

frodsan Oct 26, 2012

Contributor

Please, use 1.9 hash syntax. Thanks :)

@frodsan

frodsan Oct 26, 2012

Contributor

Please, use 1.9 hash syntax. Thanks :)

This comment has been minimized.

@bcardarella

bcardarella Oct 26, 2012

Contributor

Is this PR still under consideration? I'll update if it has a chance to be pulled in.

@bcardarella

bcardarella Oct 26, 2012

Contributor

Is this PR still under consideration? I'll update if it has a chance to be pulled in.

This comment has been minimized.

@steveklabnik

steveklabnik Oct 26, 2012

Member

If it's not closed, it's under consideration.

@steveklabnik

steveklabnik Oct 26, 2012

Member

If it's not closed, it's under consideration.

This comment has been minimized.

@steveklabnik

steveklabnik Oct 26, 2012

Member

Apparently it got closed right after @frodsan asked about this.

@steveklabnik

steveklabnik Oct 26, 2012

Member

Apparently it got closed right after @frodsan asked about this.

+
+ invalid!([1, 1.1])
+ valid!([4, 8.0])
+ end
+
def test_validates_numericality_with_proc
Topic.send(:define_method, :min_approved, lambda { 5 })
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved }