Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/delayed/jobs/delayed_paperclip_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ class DelayedPaperclipJob < Struct.new(:instance_klass, :instance_id, :attachmen
def perform
instance = instance_klass.constantize.find(instance_id)

instance.send(attachment_name).reprocess!
instance.send(attachment_name).reprocess!
end
end
4 changes: 2 additions & 2 deletions lib/delayed/jobs/resque_paperclip_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class ResquePaperclipJob
@queue = :paperclip

def self.perform(instance_klass, instance_id, attachment_name)
instance = instance_klass.constantize.find(instance_id)

instance.send(attachment_name).reprocess!
instance.send(attachment_name).reprocess!
end
end
18 changes: 9 additions & 9 deletions lib/delayed/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ module Paperclip
def self.included(base) #:nodoc:
base.extend(ClassMethods)
end

module ClassMethods
def process_in_background(name)
include InstanceMethods

define_method "#{name}_changed?" do
attachment_changed?(name)
end

define_method "halt_processing_for_#{name}" do
if self.send("#{name}_changed?")
false # halts processing
end
end

define_method "enqueue_job_for_#{name}" do
if self.send("#{name}_changed?")
if delayed_job?
Expand All @@ -27,30 +27,30 @@ def process_in_background(name)
end
end
end

self.send("before_#{name}_post_process", :"halt_processing_for_#{name}")
after_save :"enqueue_job_for_#{name}"
end
end

module InstanceMethods
PAPERCLIP_ATTRIBUTES = ['_file_size', '_file_name', '_content_type', '_updated_at']

def attachment_changed?(name)
PAPERCLIP_ATTRIBUTES.each do |attribute|
full_attribute = "#{name}#{attribute}_changed?".to_sym

next unless self.respond_to?(full_attribute)
return true if self.send("#{name}#{attribute}_changed?")
end

false
end

def delayed_job?
defined? Delayed::Job
end

def resque?
defined? Resque
end
Expand Down
4 changes: 2 additions & 2 deletions lib/delayed/resque_paperclip_job.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class ResquePaperclipJob
@queue = :paperclip

def self.perform(instance_klass, instance_id, attachment_name)
instance = instance_klass.constantize.find(instance_id)

instance.send(attachment_name).reprocess!
instance.send(attachment_name).reprocess!
end
end
18 changes: 9 additions & 9 deletions test/delayed_paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ def setup
build_delayed_jobs
reset_dummy
end

def test_attachment_changed
@dummy.stubs(:image_file_size_changed?).returns(false)
@dummy.stubs(:image_file_name_changed?).returns(false)

assert !@dummy.image_changed?
end

def test_attachment_changed_when_image_changes
@dummy.stubs(:image_file_size_changed?).returns(true)

assert @dummy.image_changed?
end

def test_before_post_process
Dummy.expects(:before_image_post_process)
@dummy_class.process_in_background :image
end

def test_halt_processing_if_source_changed
@dummy.stubs(:image_changed?).returns(true)
assert !@dummy.halt_processing_for_image
end

def test_halt_processing_if_source_has_not_changed
@dummy.stubs(:image_changed?).returns(false)
assert_not_equal false, @dummy.halt_processing_for_image
end

def test_after_save
Dummy.expects(:after_save)
@dummy_class.process_in_background :image
end

def test_enqueue_job_if_source_changed
@dummy.stubs(:image_changed?).returns(true)

Expand All @@ -59,7 +59,7 @@ def test_perform_job

def test_after_callback_is_functional
@dummy_class.send(:define_method, :done_processing) { puts 'done' }
@dummy_class.after_image_post_process :done_processing
@dummy_class.after_image_post_process :done_processing
Dummy.any_instance.expects(:done_processing)

@dummy.save!
Expand Down
2 changes: 1 addition & 1 deletion test/resque_paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_perform_job

def test_after_callback_is_functional
@dummy_class.send(:define_method, :done_processing) { puts 'done' }
@dummy_class.after_image_post_process :done_processing
@dummy_class.after_image_post_process :done_processing
Dummy.any_instance.expects(:done_processing)

@dummy.save!
Expand Down
8 changes: 4 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
gem 'paperclip'
require 'paperclip'

FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['test'])
Expand All @@ -20,8 +20,8 @@
RAILS_ENV = "test"

$LOAD_PATH << File.join(ROOT, 'lib')
$LOAD_PATH << File.join(ROOT, 'lib', 'delayed', 'paperclip')
$LOAD_PATH << File.join(ROOT, 'test')
$LOAD_PATH << File.join(ROOT, 'lib', 'delayed', 'paperclip')
$LOAD_PATH << File.join(ROOT, 'test')

require File.join(ROOT, 'lib', 'delayed_paperclip.rb')

Expand Down Expand Up @@ -71,7 +71,7 @@ def reset_dummy
@dummy_class = reset_class "Dummy"
@dummy_class.has_attached_file :image
@dummy_class.process_in_background :image

@dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
end

Expand Down