Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple_of for Numericality #7213

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ## ## 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. * 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. Now, AM Serializers and AR objects have the same default behaviour. Fixes #6578.


Expand Down
1 change: 1 addition & 0 deletions activemodel/lib/active_model/locale/en.yml
Expand Up @@ -26,3 +26,4 @@ en:
other_than: "must be other than %{count}" other_than: "must be other than %{count}"
odd: "must be odd" odd: "must be odd"
even: "must be even" even: "must be even"
multiple_of: "must be a multiple of %{multiple_of}"
6 changes: 6 additions & 0 deletions activemodel/lib/active_model/validations/numericality.rb
Expand Up @@ -37,6 +37,12 @@ def validate_each(record, attr_name, value)
end end
end end


if options[:multiple_of]
unless value % options[:multiple_of] == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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| options.slice(*CHECKS.keys).each do |option, option_value|
case option case option
when :odd, :even when :odd, :even
Expand Down
Expand Up @@ -113,6 +113,13 @@ def test_validates_numericality_with_other_than
valid!([-1, 42]) valid!([-1, 42])
end end


def test_validates_numericality_with_multiple_of
Topic.validates_numericality_of :approved, :multiple_of => 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use 1.9 hash syntax. Thanks :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 def test_validates_numericality_with_proc
Topic.send(:define_method, :min_approved, lambda { 5 }) Topic.send(:define_method, :min_approved, lambda { 5 })
Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved } Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved }
Expand Down