Skip to content

Instantly share code, notes, and snippets.

@einarj
Created August 22, 2012 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarj/3424831 to your computer and use it in GitHub Desktop.
Save einarj/3424831 to your computer and use it in GitHub Desktop.
validate_acceptance_of behaviour
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
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
@einarj
Copy link
Author

einarj commented Aug 22, 2012

Solution found, I was referring to outdated documentation.

Huge thanks to elaptics on #rubyonrails

@einarj
Copy link
Author

einarj commented Aug 22, 2012

The solution is to omit the :on parameter.

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