Created
August 22, 2012 11:55
-
-
Save einarj/3424831 to your computer and use it in GitHub Desktop.
validate_acceptance_of behaviour
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Invitation | |
extend ActiveModel::Naming | |
include ActiveModel::Conversion | |
include ActiveModel::Validations | |
attr_accessor :terms | |
validates_acceptance_of :terms, :on => :save, :allow_nil => false | |
def initialize(attrs={}) | |
attrs.each do |k, v| send("#{k}=", v) end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test_helper' | |
class InvitedUserTest < ActiveSupport::TestCase | |
test "terms validation" do | |
iu = Invitation.new | |
assert iu.valid? == false | |
assert_nil iu.terms | |
# Fails when :on => :save in model, but works when :on is omitted | |
assert iu.errors.include?(:terms), "Terms MUST NOT be nil #{iu.errors.full_messages}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The solution is to omit the :on parameter.