Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

multiple_of for Numericality #7213

Closed
wants to merge 1 commit into from

5 participants

@bcardarella

Adds support for validating multiples of a value to the numericality validator

@nashby nashby commented on the diff
activemodel/lib/active_model/validations/numericality.rb
@@ -37,6 +37,12 @@ def validate_each(record, attr_name, value)
end
end
+ if options[:multiple_of]
+ unless value % options[:multiple_of] == 0
@nashby
nashby added a note

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bcardarella

If #7216 is accepted I will update this PR to use #multiple_of instead so other numeric types can be used. Currently #multiple_of is limited to Integer only

@frodsan frodsan commented on the diff
...est/cases/validations/numericality_validation_test.rb
@@ -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
@frodsan
frodsan added a note

Please, use 1.9 hash syntax. Thanks :)

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

@steveklabnik Collaborator

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

@steveklabnik Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@rafaelfranca

I don't think is worth to add this to the framework. You can archive it easily using custom validations.

@rafaelfranca

BTW, Thank you for the pull request

@bcardarella

@rafaelfranca can I ask the reason why you don't think it is worth it? Knowing the multiple of a number is fairly important. For example, one app we built required groups to have members in multiples of 4, I had to write a custom validator for this but IMO this should be covered by the numericality validator

@rafaelfranca

@bcardarella sure. Like I said you can archive this using custom validations. Also I don't think this should be inside the Rails framework because it doesn't work with all kind of numeric values.

@bcardarella

you mean all kind, right?

@rafaelfranca

yes, sorry.

@bcardarella

ok, fair enough. Thank you for the feedback :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 31, 2012
  1. @bcardarella
This page is out of date. Refresh to see the latest.
View
2  activemodel/CHANGELOG.md
@@ -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.
View
1  activemodel/lib/active_model/locale/en.yml
@@ -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}"
View
6 activemodel/lib/active_model/validations/numericality.rb
@@ -37,6 +37,12 @@ def validate_each(record, attr_name, value)
end
end
+ if options[:multiple_of]
+ unless value % options[:multiple_of] == 0
@nashby
nashby added a note

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
View
7 activemodel/test/cases/validations/numericality_validation_test.rb
@@ -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
@frodsan
frodsan added a note

Please, use 1.9 hash syntax. Thanks :)

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

@steveklabnik Collaborator

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

@steveklabnik Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ 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 }
Something went wrong with that request. Please try again.