Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

multiple_of for Numericality #7213

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants
Contributor

bcardarella commented Jul 31, 2012

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

@nashby nashby commented on the diff Jul 31, 2012

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 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

@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.

Contributor

bcardarella commented Jul 31, 2012

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 Oct 26, 2012

...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 Oct 26, 2012

Contributor

Please, use 1.9 hash syntax. Thanks :)

@bcardarella

bcardarella Oct 26, 2012

Contributor

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

@steveklabnik

steveklabnik Oct 26, 2012

Member

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

@steveklabnik

steveklabnik Oct 26, 2012

Member

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

Owner

rafaelfranca commented Oct 26, 2012

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

Owner

rafaelfranca commented Oct 26, 2012

BTW, Thank you for the pull request

Contributor

bcardarella commented Oct 26, 2012

@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

Owner

rafaelfranca commented Oct 26, 2012

@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.

Contributor

bcardarella commented Oct 26, 2012

you mean all kind, right?

Owner

rafaelfranca commented Oct 26, 2012

yes, sorry.

Contributor

bcardarella commented Oct 26, 2012

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