Permalink
Browse files

Fix :on option for validation callbacks

ActiveModel::Validations::Callbacks already defines the before_validation and after_validation methods, which add support for the :on option. Closes #277
  • Loading branch information...
bkeepers committed May 25, 2011
1 parent d8debe9 commit b738b067bf18ba39a843965e6055ae2348e56830
Showing with 28 additions and 1 deletion.
  1. +1 −1 lib/mongo_mapper/plugins/embedded_callbacks.rb
  2. +27 −0 test/functional/test_callbacks.rb
@@ -7,7 +7,7 @@ module EmbeddedCallbacks
included do
extend ::ActiveModel::Callbacks
- define_model_callbacks :validation, :save, :create, :update, :destroy, :only => [:before, :after]
+ define_model_callbacks :save, :create, :update, :destroy, :only => [:before, :after]
end
module InstanceMethods
@@ -155,4 +155,31 @@ class CallbacksTest < Test::Unit::TestCase
end
end
end
+
+ context "Running validation callbacks with conditional execution" do
+ setup do
+ @document = Doc do
+ include CallbacksSupport
+ key :message, String
+
+ before_validation :set_message, :on => 'create'
+ def set_message
+ self['message'] = 'Hi!'
+ end
+ end
+ end
+
+ should 'run callback on create' do
+ doc = @document.create
+ doc.history.should include(:before_validation)
+ doc.message.should == 'Hi!'
+ end
+
+ should 'skip callback on update' do
+ doc = @document.create
+ doc.message = 'Ho!'
+ doc.save
+ doc.message.should == 'Ho!'
+ end
+ end
end

0 comments on commit b738b06

Please sign in to comment.