Commit 17ecc58a by Arjun Anand Committed by Jon Yurek

Fix exception that is raised if after_commit callback hooks are absent

parent 3b087ee8
...@@ -91,7 +91,11 @@ module Paperclip ...@@ -91,7 +91,11 @@ module Paperclip
name = @name name = @name
@klass.send(:after_save) { send(name).send(:save) } @klass.send(:after_save) { send(name).send(:save) }
@klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) } @klass.send(:before_destroy) { send(name).send(:queue_all_for_delete) }
@klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) } if @klass.respond_to?(:after_commit)
@klass.send(:after_commit, :on => :destroy) { send(name).send(:flush_deletes) }
else
@klass.send(:after_destroy) { send(name).send(:flush_deletes) }
end
end end
def add_paperclip_callbacks def add_paperclip_callbacks
......
...@@ -38,6 +38,14 @@ describe Paperclip::HasAttachedFile do ...@@ -38,6 +38,14 @@ describe Paperclip::HasAttachedFile do
assert_adding_attachment('avatar').defines_callback('after_commit') assert_adding_attachment('avatar').defines_callback('after_commit')
end end
it 'defines an after_commit callback if the class allows after_commit callbacks' do
assert_adding_attachment('avatar').defines_callback('after_commit')
end
it 'defines an after_destroy callback if the class does not allow after_commit callbacks' do
assert_adding_attachment('avatar', :unstub_methods => [:after_commit]).defines_callback('after_destroy')
end
it 'defines the Paperclip-specific callbacks' do it 'defines the Paperclip-specific callbacks' do
assert_adding_attachment('avatar').defines_callback('define_paperclip_callbacks') assert_adding_attachment('avatar').defines_callback('define_paperclip_callbacks')
end end
...@@ -53,20 +61,26 @@ describe Paperclip::HasAttachedFile do ...@@ -53,20 +61,26 @@ describe Paperclip::HasAttachedFile do
private private
def assert_adding_attachment(attachment_name) def assert_adding_attachment(attachment_name, options={})
AttachmentAdder.new(attachment_name) AttachmentAdder.new(attachment_name, options)
end end
class AttachmentAdder class AttachmentAdder
include Mocha::API include Mocha::API
include RSpec::Matchers include RSpec::Matchers
def initialize(attachment_name) def initialize(attachment_name, options = {})
@attachment_name = attachment_name @attachment_name = attachment_name
@stubbed_class = stub_class
if options.present?
options[:unstub_methods].each do |method|
@stubbed_class.unstub(method)
end
end
end end
def defines_method(method_name) def defines_method(method_name)
a_class = stub_class a_class = @stubbed_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
...@@ -74,7 +88,7 @@ describe Paperclip::HasAttachedFile do ...@@ -74,7 +88,7 @@ describe Paperclip::HasAttachedFile do
end end
def defines_class_method(method_name) def defines_class_method(method_name)
a_class = stub_class a_class = @stubbed_class
a_class.class.stubs(:define_method) a_class.class.stubs(:define_method)
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
...@@ -83,7 +97,7 @@ describe Paperclip::HasAttachedFile do ...@@ -83,7 +97,7 @@ describe Paperclip::HasAttachedFile do
end end
def defines_validation def defines_validation
a_class = stub_class a_class = @stubbed_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
...@@ -91,7 +105,7 @@ describe Paperclip::HasAttachedFile do ...@@ -91,7 +105,7 @@ describe Paperclip::HasAttachedFile do
end end
def registers_attachment def registers_attachment
a_class = stub_class a_class = @stubbed_class
Paperclip::AttachmentRegistry.stubs(:register) Paperclip::AttachmentRegistry.stubs(:register)
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {size: 1})
...@@ -100,7 +114,7 @@ describe Paperclip::HasAttachedFile do ...@@ -100,7 +114,7 @@ describe Paperclip::HasAttachedFile do
end end
def defines_callback(callback_name) def defines_callback(callback_name)
a_class = stub_class a_class = @stubbed_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {}) Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
...@@ -132,6 +146,7 @@ describe Paperclip::HasAttachedFile do ...@@ -132,6 +146,7 @@ describe Paperclip::HasAttachedFile do
after_save: nil, after_save: nil,
before_destroy: nil, before_destroy: nil,
after_commit: nil, after_commit: nil,
after_destroy: nil,
define_paperclip_callbacks: nil, define_paperclip_callbacks: nil,
extend: nil, extend: nil,
name: 'Billy', name: 'Billy',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment