delayed_paperclip 0.6.5 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rvmrc +1 -0
- data/CONTRIBUTING +16 -0
- data/Gemfile +3 -0
- data/README.textile +5 -1
- data/Rakefile +2 -38
- data/delayed_paperclip.gemspec +12 -52
- data/lib/delayed_paperclip/jobs/delayed_job.rb +17 -0
- data/lib/delayed_paperclip/jobs/resque.rb +15 -0
- data/lib/delayed_paperclip.rb +152 -5
- data/test/delayed_paperclip_test.rb +44 -55
- data/test/resque_paperclip_test.rb +16 -15
- data/test/test_helper.rb +15 -13
- metadata +88 -56
- data/VERSION +0 -1
- data/lib/delayed/jobs/delayed_paperclip_job.rb +0 -19
- data/lib/delayed/jobs/resque_paperclip_job.rb +0 -19
- data/lib/delayed/paperclip.rb +0 -108
data/.gitignore
CHANGED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm gemset use delayed_paperclip --create
|
data/CONTRIBUTING
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
Contributor Policy
|
2
|
+
=================
|
3
|
+
|
4
|
+
Commit bit. If you have a commit accepted into the project then you get full git access to the repo. If I don't give you this in a timely manner just send me a message.
|
5
|
+
|
6
|
+
Testing
|
7
|
+
======
|
8
|
+
|
9
|
+
Please don't commit code without tests. You can bootstrap the development environment by running `bundle install`. After that, running `rake test` should just work. If it doesn't then file a bug.
|
10
|
+
|
11
|
+
Versioning
|
12
|
+
=========
|
13
|
+
|
14
|
+
Don't bump the version in any changes you make or pull in to the project. I'll maintain rights to push the gem to rubygems.org and make releases when appropriate.
|
15
|
+
|
16
|
+
And please keep the README up to date. Thank you!
|
data/Gemfile
ADDED
data/README.textile
CHANGED
data/Rakefile
CHANGED
@@ -1,51 +1,15 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
|
-
require 'rake/rdoctask'
|
4
3
|
|
5
4
|
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
|
6
|
-
require 'paperclip'
|
7
5
|
|
8
6
|
desc 'Default: run unit tests.'
|
9
|
-
task :default =>
|
7
|
+
task :default => :test
|
10
8
|
|
11
9
|
desc 'Test the paperclip plugin.'
|
12
10
|
Rake::TestTask.new(:test) do |t|
|
13
|
-
t.libs << 'lib'
|
11
|
+
t.libs << 'lib:test'
|
14
12
|
t.pattern = 'test/**/*_test.rb'
|
15
13
|
t.verbose = true
|
16
14
|
end
|
17
15
|
|
18
|
-
desc 'Generate documentation for the paperclip plugin.'
|
19
|
-
Rake::RDocTask.new(:rdoc) do |rdoc|
|
20
|
-
rdoc.rdoc_dir = 'doc'
|
21
|
-
rdoc.title = 'Delayed::Paperclip'
|
22
|
-
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
-
rdoc.rdoc_files.include('README*')
|
24
|
-
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
-
end
|
26
|
-
|
27
|
-
desc 'Clean up files.'
|
28
|
-
task :clean do |t|
|
29
|
-
FileUtils.rm_rf "doc"
|
30
|
-
FileUtils.rm_rf "tmp"
|
31
|
-
FileUtils.rm_rf "pkg"
|
32
|
-
FileUtils.rm "test/debug.log" rescue nil
|
33
|
-
FileUtils.rm "test/paperclip.db" rescue nil
|
34
|
-
Dir.glob("paperclip-*.gem").each{|f| FileUtils.rm f }
|
35
|
-
end
|
36
|
-
|
37
|
-
begin
|
38
|
-
require 'jeweler'
|
39
|
-
Jeweler::Tasks.new do |gemspec|
|
40
|
-
gemspec.name = "delayed_paperclip"
|
41
|
-
gemspec.summary = "Process your Paperclip attachments in the background with delayed_job."
|
42
|
-
gemspec.description = "Process your Paperclip attachments in the background with delayed_job."
|
43
|
-
gemspec.email = "jesse@jstorimer.com"
|
44
|
-
gemspec.homepage = "http://github.com/jstorimer/delayed_paperclip"
|
45
|
-
gemspec.authors = ["Jesse Storimer"]
|
46
|
-
gemspec.add_dependency('paperclip', '>= 2.3.0')
|
47
|
-
end
|
48
|
-
Jeweler::GemcutterTasks.new
|
49
|
-
rescue LoadError
|
50
|
-
puts "Jeweler not available. Install it with: sudo gem install jeweler -s http://gemcutter.org"
|
51
|
-
end
|
data/delayed_paperclip.gemspec
CHANGED
@@ -1,61 +1,21 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
1
|
Gem::Specification.new do |s|
|
7
2
|
s.name = %q{delayed_paperclip}
|
8
|
-
s.version = "0.
|
3
|
+
s.version = "0.7.1"
|
9
4
|
|
10
|
-
s.
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
s.description = %q{Process your Paperclip attachments in the background with delayed_job.}
|
5
|
+
s.authors = ["Jesse Storimer", "Bert Goethals"]
|
6
|
+
s.summary = %q{Process your Paperclip attachments in the background.}
|
7
|
+
s.description = %q{Process your Paperclip attachments in the background with delayed_job, Resque or your own processor.}
|
14
8
|
s.email = %q{jesse@jstorimer.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"LICENSE",
|
17
|
-
"README.textile"
|
18
|
-
]
|
19
|
-
s.files = [
|
20
|
-
".gitignore",
|
21
|
-
"LICENSE",
|
22
|
-
"README.textile",
|
23
|
-
"Rakefile",
|
24
|
-
"VERSION",
|
25
|
-
"delayed_paperclip.gemspec",
|
26
|
-
"lib/delayed/jobs/delayed_paperclip_job.rb",
|
27
|
-
"lib/delayed/jobs/resque_paperclip_job.rb",
|
28
|
-
"lib/delayed/paperclip.rb",
|
29
|
-
"lib/delayed_paperclip.rb",
|
30
|
-
"rails/init.rb",
|
31
|
-
"test/database.yml",
|
32
|
-
"test/delayed_paperclip_test.rb",
|
33
|
-
"test/fixtures/12k.png",
|
34
|
-
"test/resque_paperclip_test.rb",
|
35
|
-
"test/test_helper.rb"
|
36
|
-
]
|
37
9
|
s.homepage = %q{http://github.com/jstorimer/delayed_paperclip}
|
38
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
39
|
-
s.require_paths = ["lib"]
|
40
|
-
s.rubygems_version = %q{1.3.6}
|
41
|
-
s.summary = %q{Process your Paperclip attachments in the background with delayed_job.}
|
42
|
-
s.test_files = [
|
43
|
-
"test/delayed_paperclip_test.rb",
|
44
|
-
"test/resque_paperclip_test.rb",
|
45
|
-
"test/test_helper.rb"
|
46
|
-
]
|
47
10
|
|
48
|
-
|
49
|
-
|
50
|
-
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
|
14
|
+
s.add_runtime_dependency 'paperclip', ["~> 2.3.0"]
|
51
15
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
else
|
58
|
-
s.add_dependency(%q<paperclip>, [">= 2.3.0"])
|
59
|
-
end
|
16
|
+
s.add_development_dependency 'mocha'
|
17
|
+
s.add_development_dependency 'sqlite3-ruby'
|
18
|
+
s.add_development_dependency 'delayed_job'
|
19
|
+
s.add_development_dependency 'resque'
|
60
20
|
end
|
61
21
|
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module DelayedPaperclip
|
2
|
+
module Jobs
|
3
|
+
class DelayedJob < Struct.new(:instance_klass, :instance_id, :attachment_name)
|
4
|
+
|
5
|
+
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
|
6
|
+
::Delayed::Job.enqueue(
|
7
|
+
new(instance_klass, instance_id, attachment_name),
|
8
|
+
:priority => instance_klass.constantize.attachment_definitions[attachment_name][:delayed][:priority].to_i
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform
|
13
|
+
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module DelayedPaperclip
|
2
|
+
module Jobs
|
3
|
+
class Resque
|
4
|
+
@queue = :paperclip
|
5
|
+
|
6
|
+
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
|
7
|
+
::Resque.enqueue(self, instance_klass, instance_id, attachment_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.perform(instance_klass, instance_id, attachment_name)
|
11
|
+
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/delayed_paperclip.rb
CHANGED
@@ -1,9 +1,156 @@
|
|
1
1
|
require 'paperclip'
|
2
2
|
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require '
|
3
|
+
require 'delayed_paperclip'
|
4
|
+
require 'delayed_paperclip/jobs/delayed_job'
|
5
|
+
require 'delayed_paperclip/jobs/resque'
|
6
|
+
|
7
|
+
module DelayedPaperclip
|
8
|
+
|
9
|
+
class << self
|
10
|
+
|
11
|
+
def options
|
12
|
+
options ||= {
|
13
|
+
:background_job_class => detect_background_task
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
def detect_background_task
|
18
|
+
return DelayedPaperclip::Jobs::DelayedJob if defined? ::Delayed::Job
|
19
|
+
return DelayedPaperclip::Jobs::Resque if defined? ::Resque
|
20
|
+
end
|
21
|
+
|
22
|
+
def processor
|
23
|
+
options[:background_job_class]
|
24
|
+
end
|
25
|
+
|
26
|
+
def enqueue(instance_klass, instance_id, attachment_name)
|
27
|
+
processor.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
def process_job(instance_klass, instance_id, attachment_name)
|
31
|
+
instance_klass.constantize.find(instance_id).
|
32
|
+
send(attachment_name).
|
33
|
+
process_delayed!
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.included(base)
|
39
|
+
base.extend(ClassMethods)
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassMethods
|
43
|
+
|
44
|
+
def process_in_background(name, options = {})
|
45
|
+
include InstanceMethods
|
46
|
+
|
47
|
+
attachment_definitions[name][:delayed] = {}
|
48
|
+
attachment_definitions[name][:delayed][:priority] = options.key?(:priority) ? options[:priority] : 0
|
49
|
+
|
50
|
+
if respond_to?(:after_commit)
|
51
|
+
after_commit :enqueue_delayed_processing
|
52
|
+
else
|
53
|
+
after_save :enqueue_delayed_processing
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
module InstanceMethods
|
59
|
+
|
60
|
+
# setting each inididual NAME_processing to true, skipping the ActiveModel dirty setter
|
61
|
+
# Then immediately push the state to the database
|
62
|
+
def mark_enqueue_delayed_processing
|
63
|
+
processing = []
|
64
|
+
(@_enqued_for_processing || []).each do |name|
|
65
|
+
if self.attributes.has_key? "#{name}_processing"
|
66
|
+
write_attribute("#{name}_processing", true)
|
67
|
+
processing << name
|
68
|
+
end
|
69
|
+
end
|
70
|
+
self.class.update_all(processing.collect{|n| "#{n}_processing = 1" }.join(", "), "id = #{self.id}") unless processing.empty?
|
71
|
+
end
|
72
|
+
|
73
|
+
# First mark processing
|
74
|
+
# then create
|
75
|
+
def enqueue_delayed_processing
|
76
|
+
mark_enqueue_delayed_processing
|
77
|
+
(@_enqued_for_processing || []).each do |name|
|
78
|
+
enqueue_post_processing_for(name)
|
79
|
+
end
|
80
|
+
@_enqued_for_processing = []
|
81
|
+
end
|
82
|
+
|
83
|
+
def enqueue_post_processing_for name
|
84
|
+
DelayedPaperclip.enqueue(self.class.name, read_attribute(:id), name.to_sym)
|
85
|
+
end
|
86
|
+
|
87
|
+
def attachment_for name
|
88
|
+
@_paperclip_attachments ||= {}
|
89
|
+
@_paperclip_attachments[name] ||= ::Paperclip::Attachment.new(name, self, self.class.attachment_definitions[name]).tap do |a|
|
90
|
+
a.post_processing = false if self.class.attachment_definitions[name][:delayed]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def prepare_enqueueing_for name
|
95
|
+
@_enqued_for_processing ||= []
|
96
|
+
@_enqued_for_processing << name
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
module Paperclip
|
103
|
+
class Attachment
|
104
|
+
attr_accessor :job_is_processing
|
105
|
+
|
106
|
+
def save_with_prepare_enqueueing
|
107
|
+
was_dirty = @dirty
|
108
|
+
save_without_prepare_enqueueing.tap do
|
109
|
+
if delay_processing? && was_dirty
|
110
|
+
instance.prepare_enqueueing_for name
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
alias_method_chain :save, :prepare_enqueueing
|
115
|
+
|
116
|
+
def url_with_processed style = default_style, include_updated_timestamp = @use_timestamp
|
117
|
+
return url_without_processed style, include_updated_timestamp if job_is_processing
|
118
|
+
|
119
|
+
if !@instance.respond_to?(:"#{name}_processing?")
|
120
|
+
url_without_processed style, include_updated_timestamp
|
121
|
+
else
|
122
|
+
if !processing?
|
123
|
+
url_without_processed style, include_updated_timestamp
|
124
|
+
else
|
125
|
+
if dirty?
|
126
|
+
url_without_processed style, include_updated_timestamp
|
127
|
+
else
|
128
|
+
interpolate(@default_url, style)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
alias_method_chain :url, :processed
|
134
|
+
|
135
|
+
def delay_processing?
|
136
|
+
!!@instance.class.attachment_definitions[@name][:delayed]
|
137
|
+
end
|
138
|
+
|
139
|
+
def processing?
|
140
|
+
@instance.send(:"#{@name}_processing?")
|
141
|
+
end
|
142
|
+
|
143
|
+
def process_delayed!
|
144
|
+
job_is_processing = true
|
145
|
+
reprocess!
|
146
|
+
job_is_processing = false
|
147
|
+
instance.update_attribute("#{name}_processing", false) if instance.attributes.has_key? "#{name}_processing"
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
6
153
|
|
7
154
|
if Object.const_defined?("ActiveRecord")
|
8
|
-
ActiveRecord::Base.send(:include,
|
9
|
-
end
|
155
|
+
ActiveRecord::Base.send(:include, DelayedPaperclip)
|
156
|
+
end
|
@@ -1,74 +1,69 @@
|
|
1
|
-
require '
|
2
|
-
gem 'delayed_job'
|
1
|
+
require 'test_helper'
|
3
2
|
require 'delayed_job'
|
3
|
+
Delayed::Worker.backend = :active_record
|
4
4
|
|
5
5
|
class DelayedPaperclipTest < Test::Unit::TestCase
|
6
6
|
def setup
|
7
|
+
super
|
8
|
+
|
7
9
|
build_delayed_jobs
|
8
10
|
reset_dummy
|
9
11
|
end
|
10
12
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
@dummy.stubs(:image_content_type_changed?).returns(false)
|
15
|
-
@dummy.stubs(:image_updated_at_changed?).returns(false)
|
13
|
+
def test_normal_paperclip_functioning
|
14
|
+
build_dummy_table(false)
|
15
|
+
reset_class "Dummy", false
|
16
16
|
|
17
|
-
|
18
|
-
end
|
17
|
+
Paperclip::Attachment.any_instance.expects(:post_process)
|
19
18
|
|
20
|
-
|
21
|
-
@dummy.stubs(:image_file_size_changed?).returns(true)
|
19
|
+
dummy = Dummy.new(:image => File.open("#{ROOT}/test/fixtures/12k.png"))
|
22
20
|
|
23
|
-
assert
|
21
|
+
assert !dummy.image.delay_processing?
|
22
|
+
assert dummy.image.post_processing
|
23
|
+
assert dummy.save
|
24
|
+
assert File.exists?(dummy.image.path)
|
24
25
|
end
|
25
26
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
end
|
27
|
+
def test_delayed_paperclip_functioning
|
28
|
+
build_dummy_table(false)
|
29
|
+
reset_class "Dummy", true
|
30
30
|
|
31
|
-
|
32
|
-
@dummy.stubs(:image_changed?).returns(true)
|
33
|
-
assert !@dummy.halt_processing_for_image
|
34
|
-
end
|
31
|
+
Paperclip::Attachment.any_instance.expects(:post_process).never
|
35
32
|
|
36
|
-
|
37
|
-
@dummy.stubs(:image_changed?).returns(false)
|
38
|
-
assert_not_equal false, @dummy.halt_processing_for_image
|
39
|
-
end
|
33
|
+
dummy = Dummy.new(:image => File.open("#{ROOT}/test/fixtures/12k.png"))
|
40
34
|
|
41
|
-
|
42
|
-
|
43
|
-
|
35
|
+
assert dummy.image.delay_processing?
|
36
|
+
assert !dummy.image.post_processing
|
37
|
+
assert dummy.save
|
38
|
+
assert File.exists?(dummy.image.path)
|
44
39
|
end
|
45
40
|
|
41
|
+
|
46
42
|
def test_enqueue_job_if_source_changed
|
47
|
-
@dummy.
|
43
|
+
@dummy.image = File.open("#{RAILS_ROOT}/test/fixtures/12k.png")
|
48
44
|
|
49
45
|
original_job_count = Delayed::Job.count
|
50
|
-
@dummy.
|
46
|
+
@dummy.save
|
51
47
|
|
52
48
|
assert_equal original_job_count + 1, Delayed::Job.count
|
53
49
|
end
|
54
50
|
|
55
51
|
def test_perform_job
|
56
|
-
@dummy.
|
52
|
+
@dummy.image = File.open("#{RAILS_ROOT}/test/fixtures/12k.png")
|
57
53
|
Paperclip::Attachment.any_instance.expects(:reprocess!)
|
58
54
|
|
59
55
|
@dummy.save!
|
60
56
|
Delayed::Job.last.payload_object.perform
|
61
57
|
end
|
62
|
-
|
58
|
+
|
63
59
|
def test_processing_column_kept_intact
|
64
60
|
@dummy = reset_dummy(true)
|
65
|
-
|
66
|
-
@dummy.stubs(:image_changed?).returns(true)
|
61
|
+
|
67
62
|
Paperclip::Attachment.any_instance.stubs(:reprocess!).raises(StandardError.new('oops'))
|
68
63
|
|
69
64
|
@dummy.save!
|
70
65
|
assert @dummy.image_processing?
|
71
|
-
Delayed::
|
66
|
+
Delayed::Worker.new.work_off
|
72
67
|
assert @dummy.reload.image_processing?
|
73
68
|
end
|
74
69
|
|
@@ -78,11 +73,7 @@ class DelayedPaperclipTest < Test::Unit::TestCase
|
|
78
73
|
Dummy.any_instance.expects(:done_processing)
|
79
74
|
|
80
75
|
@dummy.save!
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_processed_method_returns_nil_if_column_does_not_exist
|
85
|
-
assert_equal nil, @dummy.image_processed!
|
76
|
+
DelayedPaperclip::Jobs::DelayedJob.new(@dummy.class.name, @dummy.id, :image).perform
|
86
77
|
end
|
87
78
|
|
88
79
|
def test_processing_true_when_new_image_added
|
@@ -95,17 +86,17 @@ class DelayedPaperclipTest < Test::Unit::TestCase
|
|
95
86
|
end
|
96
87
|
|
97
88
|
def test_processed_true_when_delayed_jobs_completed
|
98
|
-
@dummy = reset_dummy(true)
|
89
|
+
@dummy = reset_dummy(true)
|
99
90
|
@dummy.save!
|
100
91
|
|
101
|
-
Delayed::
|
92
|
+
Delayed::Worker.new.work_off
|
102
93
|
|
103
94
|
@dummy.reload
|
104
95
|
assert !@dummy.image_processing?
|
105
96
|
end
|
106
97
|
|
107
98
|
def test_unprocessed_image_returns_missing_url
|
108
|
-
@dummy = reset_dummy(true)
|
99
|
+
@dummy = reset_dummy(true)
|
109
100
|
@dummy.save!
|
110
101
|
|
111
102
|
assert_equal "/images/original/missing.png", @dummy.image.url
|
@@ -114,31 +105,29 @@ class DelayedPaperclipTest < Test::Unit::TestCase
|
|
114
105
|
|
115
106
|
@dummy.reload
|
116
107
|
assert_match(/\/system\/images\/1\/original\/12k.png/, @dummy.image.url)
|
117
|
-
end
|
118
|
-
|
108
|
+
end
|
109
|
+
|
119
110
|
def test_original_url_when_no_processing_column
|
120
|
-
@dummy = reset_dummy(false)
|
111
|
+
@dummy = reset_dummy(false)
|
121
112
|
@dummy.save!
|
122
113
|
|
123
114
|
assert_match(/\/system\/images\/1\/original\/12k.png/, @dummy.image.url)
|
124
|
-
end
|
125
|
-
|
115
|
+
end
|
116
|
+
|
126
117
|
def test_original_url_if_image_changed
|
127
|
-
@dummy =
|
128
|
-
@dummy.
|
129
|
-
@dummy.image_content_type_will_change!
|
118
|
+
@dummy.image = File.open("#{RAILS_ROOT}/test/fixtures/12k.png")
|
119
|
+
@dummy.save!
|
130
120
|
|
131
121
|
assert_match(/system\/images\/.*original.*/, @dummy.image.url)
|
132
122
|
end
|
133
|
-
|
123
|
+
|
134
124
|
def test_missing_url_if_image_hasnt_changed
|
135
|
-
@dummy = reset_dummy(true)
|
136
|
-
@dummy.
|
137
|
-
@dummy.stubs(:image_changed?).returns(false)
|
125
|
+
@dummy = reset_dummy(true)
|
126
|
+
@dummy.save!
|
138
127
|
|
139
128
|
assert_match(/images\/.*missing.*/, @dummy.image.url)
|
140
129
|
end
|
141
|
-
|
130
|
+
|
142
131
|
def test_should_not_blow_up_if_dsl_unused
|
143
132
|
reset_class "Dummy", false
|
144
133
|
@dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
@@ -1,9 +1,11 @@
|
|
1
|
-
require '
|
1
|
+
require 'test_helper'
|
2
2
|
gem 'resque'
|
3
3
|
require 'resque'
|
4
4
|
|
5
5
|
class ResquePaperclipTest < Test::Unit::TestCase
|
6
6
|
def setup
|
7
|
+
super
|
8
|
+
|
7
9
|
# Make sure that we just test Resque in here
|
8
10
|
Object.send(:remove_const, :Delayed) if defined? Delayed
|
9
11
|
|
@@ -12,42 +14,41 @@ class ResquePaperclipTest < Test::Unit::TestCase
|
|
12
14
|
end
|
13
15
|
|
14
16
|
def test_enqueue_job_if_source_changed
|
15
|
-
@dummy.
|
17
|
+
@dummy.image = File.open("#{RAILS_ROOT}/test/fixtures/12k.png")
|
16
18
|
|
17
19
|
original_job_count = Resque.size(:paperclip)
|
18
|
-
@dummy.
|
20
|
+
@dummy.save
|
19
21
|
|
20
22
|
assert_equal original_job_count + 1, Resque.size(:paperclip)
|
21
23
|
end
|
22
24
|
|
23
25
|
def test_perform_job
|
24
26
|
Paperclip::Attachment.any_instance.expects(:reprocess!)
|
25
|
-
|
26
27
|
@dummy.save!
|
27
|
-
|
28
|
+
DelayedPaperclip::Jobs::Resque.perform(@dummy.class.name, @dummy.id, :image)
|
28
29
|
end
|
29
30
|
|
30
31
|
def test_after_callback_is_functional
|
31
32
|
@dummy_class.send(:define_method, :done_processing) { puts 'done' }
|
32
|
-
@dummy_class.after_image_post_process :done_processing
|
33
|
+
@dummy_class.after_image_post_process :done_processing
|
33
34
|
Dummy.any_instance.expects(:done_processing)
|
34
35
|
|
35
36
|
@dummy.save!
|
36
|
-
|
37
|
+
DelayedPaperclip::Jobs::Resque.perform(@dummy.class.name, @dummy.id, :image)
|
37
38
|
end
|
38
|
-
|
39
|
+
|
39
40
|
def test_processing_column_kept_intact
|
40
41
|
@dummy = reset_dummy(true)
|
41
|
-
|
42
|
-
@dummy.
|
42
|
+
|
43
|
+
@dummy.image = File.open("#{RAILS_ROOT}/test/fixtures/12k.png")
|
43
44
|
Paperclip::Attachment.any_instance.stubs(:reprocess!).raises(StandardError.new('oops'))
|
44
45
|
|
45
46
|
@dummy.save!
|
46
|
-
assert @dummy.image_processing
|
47
|
-
|
47
|
+
assert @dummy.image_processing?, "image should be processing #{@dummy.inspect}"
|
48
|
+
|
48
49
|
worker = Resque::Worker.new(:paperclip)
|
49
50
|
worker.process
|
50
|
-
|
51
|
-
assert @dummy.reload.image_processing
|
51
|
+
|
52
|
+
assert @dummy.reload.image_processing?, "image should be processing"
|
52
53
|
end
|
53
|
-
end
|
54
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,23 +1,25 @@
|
|
1
|
-
require 'test/unit'
|
2
1
|
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
3
|
require 'mocha'
|
4
4
|
require 'active_record'
|
5
|
+
require 'logger'
|
6
|
+
require 'sqlite3'
|
7
|
+
require 'paperclip/railtie'
|
8
|
+
Paperclip::Railtie.insert
|
5
9
|
|
6
10
|
ROOT = File.join(File.dirname(__FILE__), '..')
|
7
11
|
RAILS_ROOT = ROOT
|
8
|
-
RAILS_ENV = "test"
|
9
|
-
|
10
12
|
$LOAD_PATH << File.join(ROOT, 'lib')
|
11
|
-
$LOAD_PATH << File.join(ROOT, 'lib', 'delayed', 'paperclip')
|
12
|
-
$LOAD_PATH << File.join(ROOT, 'test')
|
13
|
-
|
14
|
-
require File.join(ROOT, 'lib', 'delayed_paperclip.rb')
|
15
13
|
|
16
|
-
require '
|
17
|
-
gem 'sqlite3-ruby'
|
14
|
+
require 'delayed_paperclip'
|
18
15
|
|
19
|
-
|
20
|
-
|
16
|
+
class Test::Unit::TestCase
|
17
|
+
def setup
|
18
|
+
silence_warnings do
|
19
|
+
Object.const_set(:Rails, stub('Rails', :root => ROOT, :env => 'test'))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
21
23
|
|
22
24
|
FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
|
23
25
|
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
@@ -29,7 +31,7 @@ def reset_dummy(with_processed = false)
|
|
29
31
|
|
30
32
|
reset_class "Dummy"
|
31
33
|
|
32
|
-
@dummy = Dummy.new(:image => File.open("#{
|
34
|
+
@dummy = Dummy.new(:image => File.open("#{ROOT}/test/fixtures/12k.png"))
|
33
35
|
end
|
34
36
|
|
35
37
|
def reset_class class_name, include_process = true
|
@@ -65,4 +67,4 @@ def build_delayed_jobs
|
|
65
67
|
table.string :locked_by # Who is working on this object (if locked)
|
66
68
|
table.timestamps
|
67
69
|
end
|
68
|
-
end
|
70
|
+
end
|
metadata
CHANGED
@@ -1,56 +1,91 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed_paperclip
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 0
|
7
|
-
- 6
|
8
|
-
- 5
|
9
|
-
version: 0.6.5
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.7.1
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Jesse Storimer
|
9
|
+
- Bert Goethals
|
13
10
|
autorequire:
|
14
11
|
bindir: bin
|
15
12
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2010-03-13 00:00:00 -05:00
|
13
|
+
date: 2011-07-27 00:00:00.000000000 +02:00
|
18
14
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
- !ruby/object:Gem::Dependency
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
21
17
|
name: paperclip
|
22
|
-
|
23
|
-
|
24
|
-
requirements:
|
25
|
-
- -
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
segments:
|
28
|
-
- 2
|
29
|
-
- 3
|
30
|
-
- 0
|
18
|
+
requirement: &2152759240 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
31
23
|
version: 2.3.0
|
32
24
|
type: :runtime
|
33
|
-
|
34
|
-
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *2152759240
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mocha
|
29
|
+
requirement: &2152758840 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *2152758840
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: sqlite3-ruby
|
40
|
+
requirement: &2152758380 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *2152758380
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: delayed_job
|
51
|
+
requirement: &2152757960 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *2152757960
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: resque
|
62
|
+
requirement: &2152757540 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *2152757540
|
71
|
+
description: Process your Paperclip attachments in the background with delayed_job,
|
72
|
+
Resque or your own processor.
|
35
73
|
email: jesse@jstorimer.com
|
36
74
|
executables: []
|
37
|
-
|
38
75
|
extensions: []
|
39
|
-
|
40
|
-
|
41
|
-
- LICENSE
|
42
|
-
- README.textile
|
43
|
-
files:
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
44
78
|
- .gitignore
|
79
|
+
- .rvmrc
|
80
|
+
- CONTRIBUTING
|
81
|
+
- Gemfile
|
45
82
|
- LICENSE
|
46
83
|
- README.textile
|
47
84
|
- Rakefile
|
48
|
-
- VERSION
|
49
85
|
- delayed_paperclip.gemspec
|
50
|
-
- lib/delayed/jobs/delayed_paperclip_job.rb
|
51
|
-
- lib/delayed/jobs/resque_paperclip_job.rb
|
52
|
-
- lib/delayed/paperclip.rb
|
53
86
|
- lib/delayed_paperclip.rb
|
87
|
+
- lib/delayed_paperclip/jobs/delayed_job.rb
|
88
|
+
- lib/delayed_paperclip/jobs/resque.rb
|
54
89
|
- rails/init.rb
|
55
90
|
- test/database.yml
|
56
91
|
- test/delayed_paperclip_test.rb
|
@@ -60,34 +95,31 @@ files:
|
|
60
95
|
has_rdoc: true
|
61
96
|
homepage: http://github.com/jstorimer/delayed_paperclip
|
62
97
|
licenses: []
|
63
|
-
|
64
98
|
post_install_message:
|
65
|
-
rdoc_options:
|
66
|
-
|
67
|
-
require_paths:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
68
101
|
- lib
|
69
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
|
81
|
-
- 0
|
82
|
-
version: "0"
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
none: false
|
110
|
+
requirements:
|
111
|
+
- - ! '>='
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
83
114
|
requirements: []
|
84
|
-
|
85
115
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.
|
116
|
+
rubygems_version: 1.6.2
|
87
117
|
signing_key:
|
88
118
|
specification_version: 3
|
89
|
-
summary: Process your Paperclip attachments in the background
|
90
|
-
test_files:
|
119
|
+
summary: Process your Paperclip attachments in the background.
|
120
|
+
test_files:
|
121
|
+
- test/database.yml
|
91
122
|
- test/delayed_paperclip_test.rb
|
123
|
+
- test/fixtures/12k.png
|
92
124
|
- test/resque_paperclip_test.rb
|
93
125
|
- test/test_helper.rb
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.6.5
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class DelayedPaperclipJob < Struct.new(:instance_klass, :instance_id, :attachment_name)
|
2
|
-
def perform
|
3
|
-
process_job do
|
4
|
-
instance.send(attachment_name).reprocess!
|
5
|
-
instance.send("#{attachment_name}_processed!")
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
private
|
10
|
-
def instance
|
11
|
-
@instance ||= instance_klass.constantize.find(instance_id)
|
12
|
-
end
|
13
|
-
|
14
|
-
def process_job
|
15
|
-
instance.send(attachment_name).job_is_processing = true
|
16
|
-
yield
|
17
|
-
instance.send(attachment_name).job_is_processing = false
|
18
|
-
end
|
19
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
class ResquePaperclipJob
|
2
|
-
@queue = :paperclip
|
3
|
-
|
4
|
-
def self.perform(instance_klass, instance_id, attachment_name)
|
5
|
-
instance = instance_klass.constantize.find(instance_id)
|
6
|
-
|
7
|
-
process_job(instance, attachment_name) do
|
8
|
-
instance.send(attachment_name).reprocess!
|
9
|
-
instance.send("#{attachment_name}_processed!")
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
def self.process_job(instance, attachment_name)
|
15
|
-
instance.send(attachment_name).job_is_processing = true
|
16
|
-
yield
|
17
|
-
instance.send(attachment_name).job_is_processing = false
|
18
|
-
end
|
19
|
-
end
|
data/lib/delayed/paperclip.rb
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
module Delayed
|
2
|
-
module Paperclip
|
3
|
-
def self.included(base)
|
4
|
-
base.extend(ClassMethods)
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def process_in_background(name)
|
9
|
-
include InstanceMethods
|
10
|
-
|
11
|
-
define_method "#{name}_changed?" do
|
12
|
-
attachment_has_changed?(name)
|
13
|
-
end
|
14
|
-
|
15
|
-
define_method "halt_processing_for_#{name}" do
|
16
|
-
return unless self.send("#{name}_changed?")
|
17
|
-
|
18
|
-
false # halts processing
|
19
|
-
end
|
20
|
-
|
21
|
-
define_method "enqueue_job_for_#{name}" do
|
22
|
-
return unless self.send("#{name}_changed?")
|
23
|
-
|
24
|
-
if delayed_job?
|
25
|
-
Delayed::Job.enqueue DelayedPaperclipJob.new(self.class.name, read_attribute(:id), name.to_sym)
|
26
|
-
elsif resque?
|
27
|
-
Resque.enqueue(ResquePaperclipJob, self.class.name, read_attribute(:id), name.to_sym)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
define_method "#{name}_processed!" do
|
32
|
-
return unless column_exists?(:"#{name}_processing")
|
33
|
-
return unless self.send(:"#{name}_processing?")
|
34
|
-
|
35
|
-
self.send("#{name}_processing=", false)
|
36
|
-
self.save(false)
|
37
|
-
end
|
38
|
-
|
39
|
-
define_method "#{name}_processing!" do
|
40
|
-
return unless column_exists?(:"#{name}_processing")
|
41
|
-
return if self.send(:"#{name}_processing?")
|
42
|
-
return unless self.send(:"#{name}_changed?")
|
43
|
-
|
44
|
-
self.send("#{name}_processing=", true)
|
45
|
-
end
|
46
|
-
|
47
|
-
self.send("before_#{name}_post_process", :"halt_processing_for_#{name}")
|
48
|
-
|
49
|
-
before_save :"#{name}_processing!"
|
50
|
-
after_save :"enqueue_job_for_#{name}"
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
module InstanceMethods
|
55
|
-
PAPERCLIP_ATTRIBUTES = ['_file_size', '_file_name', '_content_type', '_updated_at']
|
56
|
-
|
57
|
-
def attachment_has_changed?(name)
|
58
|
-
PAPERCLIP_ATTRIBUTES.each do |attribute|
|
59
|
-
full_attribute = "#{name}#{attribute}_changed?".to_sym
|
60
|
-
|
61
|
-
next unless self.respond_to?(full_attribute)
|
62
|
-
return true if self.send("#{name}#{attribute}_changed?")
|
63
|
-
end
|
64
|
-
|
65
|
-
false
|
66
|
-
end
|
67
|
-
|
68
|
-
def delayed_job?
|
69
|
-
defined? Delayed::Job
|
70
|
-
end
|
71
|
-
|
72
|
-
def resque?
|
73
|
-
defined? Resque
|
74
|
-
end
|
75
|
-
|
76
|
-
def column_exists?(column)
|
77
|
-
self.class.columns_hash.has_key?(column.to_s)
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
module Paperclip
|
84
|
-
class Attachment
|
85
|
-
attr_accessor :job_is_processing
|
86
|
-
|
87
|
-
def url_with_processed style = default_style, include_updated_timestamp = true
|
88
|
-
return url_without_processed style, include_updated_timestamp unless @instance.respond_to?(:column_exists?)
|
89
|
-
return url_without_processed style, include_updated_timestamp if job_is_processing
|
90
|
-
|
91
|
-
if !@instance.column_exists?(:"#{@name}_processing")
|
92
|
-
url_without_processed style, include_updated_timestamp
|
93
|
-
else
|
94
|
-
if !@instance.send(:"#{@name}_processing?")
|
95
|
-
url_without_processed style, include_updated_timestamp
|
96
|
-
else
|
97
|
-
if @instance.send(:"#{@name}_changed?")
|
98
|
-
url_without_processed style, include_updated_timestamp
|
99
|
-
else
|
100
|
-
interpolate(@default_url, style)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
alias_method_chain :url, :processed
|
107
|
-
end
|
108
|
-
end
|