Commit 5046dfcb by George Brocklehurst Committed by Mike Burns

Move after_save callback to HasAttachedFile

parent 32548249
...@@ -175,7 +175,6 @@ module Paperclip ...@@ -175,7 +175,6 @@ module Paperclip
def has_attached_file(name, options = {}) def has_attached_file(name, options = {})
HasAttachedFile.define_on(self, name, options) HasAttachedFile.define_on(self, name, options)
after_save { send(name).send(:save) }
before_destroy { send(name).send(:queue_all_for_delete) } before_destroy { send(name).send(:queue_all_for_delete) }
after_destroy { send(name).send(:flush_deletes) } after_destroy { send(name).send(:flush_deletes) }
......
...@@ -17,6 +17,7 @@ module Paperclip ...@@ -17,6 +17,7 @@ module Paperclip
define_query define_query
check_for_path_clash check_for_path_clash
register_with_rake_tasks register_with_rake_tasks
add_active_record_callbacks
end end
private private
...@@ -72,5 +73,10 @@ module Paperclip ...@@ -72,5 +73,10 @@ module Paperclip
def register_with_rake_tasks def register_with_rake_tasks
Paperclip::Tasks::Attachments.add(@klass, @name, @options) Paperclip::Tasks::Attachments.add(@klass, @name, @options)
end end
def add_active_record_callbacks
name = @name
@klass.send(:after_save) { send(name).send(:save) }
end
end end
end end
...@@ -26,6 +26,10 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -26,6 +26,10 @@ class HasAttachedFileTest < Test::Unit::TestCase
should 'register the attachment with Paperclip::Tasks' do should 'register the attachment with Paperclip::Tasks' do
assert_adding_attachment('avatar').registers_with_tasks assert_adding_attachment('avatar').registers_with_tasks
end end
should 'define an after_save callback' do
assert_adding_attachment('avatar').defines_callback('after_save')
end
end end
private private
...@@ -84,12 +88,23 @@ class HasAttachedFileTest < Test::Unit::TestCase ...@@ -84,12 +88,23 @@ class HasAttachedFileTest < Test::Unit::TestCase
end end
end end
def defines_callback(callback_name)
a_class = stub_class
Paperclip::HasAttachedFile.define_on(a_class, @attachment_name, {})
assert_received(a_class, callback_name.to_sym)
end
private private
def stub_class def stub_class
stub('class', stub('class',
validates_each: nil, validates_each: nil,
define_method: nil, define_method: nil,
after_save: nil,
before_destroy: nil,
after_destroy: nil,
name: 'Billy') name: 'Billy')
end end
end end
......
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