Skip to content

Commit

Permalink
allow '1' or true for acceptance validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
xlgmokha committed Jan 11, 2015
1 parent 89470bb commit 140557e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,3 +1,9 @@
* Change validates_acceptance_of to accept true by default.

The default for validates_acceptance_of is now "1" and true.
In the past, only "1" was the default and you were required to add
accept: true.

* Remove deprecated `ActiveModel::Dirty#reset_#{attribute}` and
`ActiveModel::Dirty#reset_changes`.

Expand Down
8 changes: 6 additions & 2 deletions activemodel/lib/active_model/validations/acceptance.rb
Expand Up @@ -3,12 +3,12 @@ module ActiveModel
module Validations
class AcceptanceValidator < EachValidator # :nodoc:
def initialize(options)
super({ allow_nil: true, accept: "1" }.merge!(options))
super({ allow_nil: true, accept: ["1", true] }.merge!(options))
setup!(options[:class])
end

def validate_each(record, attribute, value)
unless value == options[:accept]
unless acceptable_option?(value)
record.errors.add(attribute, :accepted, options.except(:accept, :allow_nil))
end
end
Expand All @@ -20,6 +20,10 @@ def setup!(klass)
klass.send(:attr_reader, *attr_readers)
klass.send(:attr_writer, *attr_writers)
end

def acceptable_option?(value)
Array(options[:accept]).include?(value)
end
end

module HelperMethods
Expand Down
Expand Up @@ -65,4 +65,10 @@ def test_validates_acceptance_of_for_ruby_class
ensure
Person.clear_validators!
end

def test_validates_acceptance_of_true
Topic.validates_acceptance_of(:terms_of_service)

assert Topic.new(terms_of_service: true).valid?
end
end

0 comments on commit 140557e

Please sign in to comment.