delayed_paperclip 2.5.1.0 → 2.6.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +29 -1
- data/lib/delayed_paperclip/attachment.rb +16 -3
- data/lib/delayed_paperclip/url_generator.rb +3 -3
- data/lib/delayed_paperclip.rb +7 -2
- data/test/base_delayed_paperclip_test.rb +76 -0
- data/test/delayed_paperclip_test.rb +1 -0
- data/test/test_helper.rb +6 -1
- metadata +28 -12
data/README.textile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
h1. Delayed::Paperclip !https://
|
1
|
+
h1. Delayed::Paperclip !https://travis-ci.org/jrgifford/delayed_paperclip.png?branch=master!:https://travis-ci.org/jrgifford/delayed_paperclip "!https://codeclimate.com/github/jrgifford/delayed_paperclip.png!":https://codeclimate.com/github/jrgifford/delayed_paperclip
|
2
2
|
|
3
3
|
Delayed_paperclip lets you process your "Paperclip":http://github.com/thoughtbot/paperclip attachments in a background task with "delayed_job":http://github.com/tobi/delayed_job or "Resque":http://github.com/defunkt/resque.
|
4
4
|
|
@@ -98,6 +98,34 @@ being processed.
|
|
98
98
|
|
99
99
|
</code></pre>
|
100
100
|
|
101
|
+
h4. Only process certain styles
|
102
|
+
|
103
|
+
This is useful if you don't want the background job to reprocess all styles.
|
104
|
+
|
105
|
+
<pre><code>
|
106
|
+
class User < ActiveRecord::Base
|
107
|
+
has_attached_file :avatar, :styles => { :small => "25x25#", :medium => "50x50x" }
|
108
|
+
|
109
|
+
process_in_background :avatar, :only_process => [:small]
|
110
|
+
end
|
111
|
+
</code></pre>
|
112
|
+
|
113
|
+
h4. Reprocess Without Delay
|
114
|
+
|
115
|
+
This is useful if you don't want the background job. It accepts individual styles to. Take note, normal `reprocess!` does not accept
|
116
|
+
styles as arguments anymore. It will delegate to DelayedPaperclip and reprocess all styles.
|
117
|
+
|
118
|
+
<pre><code>
|
119
|
+
class User < ActiveRecord::Base
|
120
|
+
has_attached_file :avatar, :styles => { :small => "25x25#", :medium => "50x50x" }
|
121
|
+
|
122
|
+
process_in_background :avatar
|
123
|
+
end
|
124
|
+
|
125
|
+
@user.avatar.url #=> "/system/images/3/original/IMG_2772.JPG?1267562148"
|
126
|
+
@user.avatar.reprocess_without_delay!(:medium)
|
127
|
+
</code></pre>
|
128
|
+
|
101
129
|
|
102
130
|
h2. What if I'm not using images?
|
103
131
|
|
@@ -39,24 +39,32 @@ module DelayedPaperclip
|
|
39
39
|
def process_delayed!
|
40
40
|
self.job_is_processing = true
|
41
41
|
self.post_processing = true
|
42
|
-
reprocess!
|
42
|
+
reprocess!(*delayed_options[:only_process])
|
43
43
|
self.job_is_processing = false
|
44
44
|
end
|
45
45
|
|
46
|
+
def processing_image_url
|
47
|
+
processing_image_url = @options[:delayed][:processing_image_url]
|
48
|
+
processing_image_url = processing_image_url.call(self) if processing_image_url.respond_to?(:call)
|
49
|
+
processing_image_url
|
50
|
+
end
|
51
|
+
|
52
|
+
|
46
53
|
def after_flush_writes_with_processing(*args)
|
47
54
|
after_flush_writes_without_processing(*args)
|
48
55
|
|
49
56
|
# update_column is available in rails 3.1 instead we can do this to update the attribute without callbacks
|
50
57
|
|
51
|
-
#instance.update_column("#{name}_processing", false) if instance.respond_to?(:"#{name}_processing?")
|
58
|
+
# instance.update_column("#{name}_processing", false) if instance.respond_to?(:"#{name}_processing?")
|
52
59
|
if instance.respond_to?(:"#{name}_processing?")
|
53
60
|
instance.send("#{name}_processing=", false)
|
54
|
-
instance.class.update_all({ "#{name}_processing" => false }
|
61
|
+
instance.class.where(instance.class.primary_key => instance.id).update_all({ "#{name}_processing" => false })
|
55
62
|
end
|
56
63
|
end
|
57
64
|
|
58
65
|
def save_with_prepare_enqueueing
|
59
66
|
was_dirty = @dirty
|
67
|
+
|
60
68
|
save_without_prepare_enqueueing.tap do
|
61
69
|
if delay_processing? && was_dirty
|
62
70
|
instance.prepare_enqueueing_for name
|
@@ -64,6 +72,11 @@ module DelayedPaperclip
|
|
64
72
|
end
|
65
73
|
end
|
66
74
|
|
75
|
+
def reprocess_without_delay!(*style_args)
|
76
|
+
@post_processing_with_delay = true
|
77
|
+
reprocess!(*style_args)
|
78
|
+
end
|
79
|
+
|
67
80
|
end
|
68
81
|
end
|
69
82
|
end
|
@@ -9,16 +9,16 @@ module DelayedPaperclip
|
|
9
9
|
|
10
10
|
def most_appropriate_url_with_processed
|
11
11
|
if @attachment.original_filename.nil? || delayed_default_url?
|
12
|
-
if @attachment.delayed_options.nil? || @attachment.
|
12
|
+
if @attachment.delayed_options.nil? || @attachment.processing_image_url.nil? || !@attachment.processing?
|
13
13
|
default_url
|
14
14
|
else
|
15
|
-
@attachment.
|
15
|
+
@attachment.processing_image_url
|
16
16
|
end
|
17
17
|
else
|
18
18
|
@attachment_options[:url]
|
19
19
|
end
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
def timestamp_possible_with_processed?
|
23
23
|
if delayed_default_url?
|
24
24
|
false
|
data/lib/delayed_paperclip.rb
CHANGED
@@ -51,10 +51,13 @@ module DelayedPaperclip
|
|
51
51
|
attachment_definitions[name][:delayed] = {}
|
52
52
|
{
|
53
53
|
:priority => 0,
|
54
|
+
:only_process => attachment_definitions[name][:only_process],
|
54
55
|
:url_with_processing => DelayedPaperclip.options[:url_with_processing],
|
55
56
|
:processing_image_url => options[:processing_image_url]
|
56
57
|
}.each do |option, default|
|
58
|
+
|
57
59
|
attachment_definitions[name][:delayed][option] = options.key?(option) ? options[option] : default
|
60
|
+
|
58
61
|
end
|
59
62
|
|
60
63
|
if respond_to?(:after_commit)
|
@@ -70,10 +73,10 @@ module DelayedPaperclip
|
|
70
73
|
# setting each inididual NAME_processing to true, skipping the ActiveModel dirty setter
|
71
74
|
# Then immediately push the state to the database
|
72
75
|
def mark_enqueue_delayed_processing
|
73
|
-
unless @_enqued_for_processing_with_processing.blank? # catches nil and
|
76
|
+
unless @_enqued_for_processing_with_processing.blank? # catches nil and empty arrays
|
74
77
|
updates = @_enqued_for_processing_with_processing.collect{|n| "#{n}_processing = :true" }.join(", ")
|
75
78
|
updates = ActiveRecord::Base.send(:sanitize_sql_array, [updates, {:true => true}])
|
76
|
-
self.class.
|
79
|
+
self.class.where(:id => self.id).update_all(updates)
|
77
80
|
end
|
78
81
|
end
|
79
82
|
|
@@ -81,6 +84,7 @@ module DelayedPaperclip
|
|
81
84
|
# then create
|
82
85
|
def enqueue_delayed_processing
|
83
86
|
mark_enqueue_delayed_processing
|
87
|
+
|
84
88
|
(@_enqued_for_processing || []).each do |name|
|
85
89
|
enqueue_post_processing_for(name)
|
86
90
|
end
|
@@ -98,6 +102,7 @@ module DelayedPaperclip
|
|
98
102
|
@_enqued_for_processing_with_processing ||= []
|
99
103
|
@_enqued_for_processing_with_processing << name
|
100
104
|
end
|
105
|
+
|
101
106
|
@_enqued_for_processing ||= []
|
102
107
|
@_enqued_for_processing << name
|
103
108
|
end
|
@@ -110,6 +110,20 @@ module BaseDelayedPaperclipTest
|
|
110
110
|
assert dummy.image.url.starts_with?("/system/dummies/images/000/000/001/original/12k.png")
|
111
111
|
end
|
112
112
|
|
113
|
+
def test_unprocessed_processing_url_when_file
|
114
|
+
reset_dummy :with_processed => true, :processing_image_url => "processing"
|
115
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
116
|
+
dummy.save!
|
117
|
+
assert dummy.reload.image.url.starts_with?("processing")
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_processed_default_url_when_no_file
|
121
|
+
reset_dummy :with_processed => true, :processing_image_url => "processing"
|
122
|
+
dummy = Dummy.new()
|
123
|
+
dummy.save!
|
124
|
+
assert dummy.reload.image.url.starts_with?("/images/original/missing.png")
|
125
|
+
end
|
126
|
+
|
113
127
|
def test_original_url_when_no_processing_column
|
114
128
|
reset_dummy :with_processed => false
|
115
129
|
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
@@ -157,4 +171,66 @@ module BaseDelayedPaperclipTest
|
|
157
171
|
dummy.name = "foobar123"
|
158
172
|
end
|
159
173
|
|
174
|
+
def test_delayed_paperclip_functioning_with_only_process_option
|
175
|
+
reset_class "Dummy", :with_processed => true, :only_process => [:thumbnail]
|
176
|
+
Paperclip::Attachment.any_instance.expects(:reprocess!).with(:thumbnail)
|
177
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
178
|
+
dummy.save!
|
179
|
+
process_jobs
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_delayed_paperclip_functioning_with_paperclip_only_process_option
|
183
|
+
reset_class "Dummy", :with_processed => true, :paperclip => { :only_process => [:thumbnail] }
|
184
|
+
Paperclip::Attachment.any_instance.expects(:reprocess!).with(:thumbnail)
|
185
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
186
|
+
dummy.save!
|
187
|
+
process_jobs
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_delayed_paperclip_should_convert_image_formats
|
191
|
+
reset_class "Dummy", :with_processed => true, :paperclip => { :styles => {:thumbnail => ['12x12', :jpg]} }
|
192
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
193
|
+
dummy.save!
|
194
|
+
process_jobs
|
195
|
+
assert dummy.reload.image.url(:thumbnail).starts_with?("/system/dummies/images/000/000/001/thumbnail/12k.jpg")
|
196
|
+
assert File.exists?("#{RAILS_ROOT}/public/system/dummies/images/000/000/001/thumbnail/12k.jpg")
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_delayed_paperclip_without_delay
|
200
|
+
reset_class "Dummy", :with_processed => true, :paperclip => { :styles => {:thumbnail => ['12x12', :jpg]} }
|
201
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
202
|
+
dummy.save!
|
203
|
+
existing_jobs_count = jobs_count
|
204
|
+
dummy.update_attribute(:image_processing, false)
|
205
|
+
dummy.image.reprocess_without_delay!(:thumbnail)
|
206
|
+
Paperclip::Attachment.any_instance.expects(:reprocess!).never
|
207
|
+
assert_equal existing_jobs_count, jobs_count
|
208
|
+
assert_equal false, dummy.image_processing?
|
209
|
+
assert File.exists?("#{RAILS_ROOT}/public/system/dummies/images/000/000/001/thumbnail/12k.jpg")
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_unprocess_image_interpolates_reprocessing_url
|
213
|
+
reset_class "Dummy", :paperclip => { :styles => {:thumbnail => '12x12'} }
|
214
|
+
reset_dummy :processing_image_url => "/images/:style/processing.png"
|
215
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
216
|
+
dummy.save!
|
217
|
+
assert dummy.image.url.starts_with?("/images/original/processing.png")
|
218
|
+
assert dummy.image.url(:thumbnail).starts_with?("/images/thumbnail/processing.png")
|
219
|
+
process_jobs
|
220
|
+
dummy.reload
|
221
|
+
assert dummy.image.url.starts_with?("/system/dummies/images/000/000/001/original/12k.png")
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_unprocess_image_accepts_proc_for_reprocessing_url
|
225
|
+
reset_class "Dummy", :paperclip => { :styles => {:thumbnail => '12x12'} }
|
226
|
+
reset_dummy :processing_image_url => lambda { |attachment| attachment.instance.reprocessing_url }
|
227
|
+
Dummy.send(:define_method, :reprocessing_url) { 'done' }
|
228
|
+
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
|
229
|
+
dummy.save!
|
230
|
+
assert dummy.image.url.starts_with?("done")
|
231
|
+
process_jobs
|
232
|
+
dummy.reload
|
233
|
+
assert dummy.image.url.starts_with?("/system/dummies/images/000/000/001/original/12k.png")
|
234
|
+
end
|
235
|
+
|
160
236
|
end
|
@@ -39,6 +39,7 @@ class DelayedPaperclipTest < Test::Unit::TestCase
|
|
39
39
|
table.datetime :locked_at # Set when a client is working on this object
|
40
40
|
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
|
41
41
|
table.string :locked_by # Who is working on this object (if locked)
|
42
|
+
table.string :queue
|
42
43
|
table.timestamps
|
43
44
|
end
|
44
45
|
end
|
data/test/test_helper.rb
CHANGED
@@ -5,6 +5,8 @@ require 'active_record'
|
|
5
5
|
require 'logger'
|
6
6
|
require 'sqlite3'
|
7
7
|
require 'paperclip/railtie'
|
8
|
+
require 'debugger'
|
9
|
+
|
8
10
|
Paperclip::Railtie.insert
|
9
11
|
|
10
12
|
ROOT = File.join(File.dirname(__FILE__), '..')
|
@@ -51,19 +53,22 @@ def build_dummy_table(with_processed)
|
|
51
53
|
end
|
52
54
|
|
53
55
|
def reset_class(class_name, options)
|
56
|
+
options[:paperclip] = {} if options[:paperclip].nil?
|
54
57
|
ActiveRecord::Base.send(:include, Paperclip::Glue)
|
55
58
|
Object.send(:remove_const, class_name) rescue nil
|
56
59
|
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
57
60
|
klass.class_eval do
|
58
61
|
include Paperclip::Glue
|
59
|
-
has_attached_file
|
62
|
+
has_attached_file :image, options[:paperclip]
|
60
63
|
|
64
|
+
options.delete(:paperclip)
|
61
65
|
process_in_background :image, options if options[:with_processed]
|
62
66
|
after_update :reprocess if options[:with_after_update_callback]
|
63
67
|
|
64
68
|
def reprocess
|
65
69
|
image.reprocess!
|
66
70
|
end
|
71
|
+
|
67
72
|
end
|
68
73
|
klass.reset_column_information
|
69
74
|
klass
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed_paperclip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.6.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2013-02
|
14
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: paperclip
|
@@ -109,6 +109,22 @@ dependencies:
|
|
109
109
|
- - ! '>='
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: debugger
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
type: :development
|
121
|
+
prerelease: false
|
122
|
+
version_requirements: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
112
128
|
description: Process your Paperclip attachments in the background with delayed_job,
|
113
129
|
Resque or your own processor.
|
114
130
|
email: james@jamesrgifford.com
|
@@ -120,21 +136,21 @@ files:
|
|
120
136
|
- LICENSE
|
121
137
|
- Rakefile
|
122
138
|
- init.rb
|
123
|
-
- lib/delayed_paperclip
|
124
|
-
- lib/delayed_paperclip/
|
139
|
+
- lib/delayed_paperclip.rb
|
140
|
+
- lib/delayed_paperclip/url_generator.rb
|
141
|
+
- lib/delayed_paperclip/attachment.rb
|
125
142
|
- lib/delayed_paperclip/jobs/resque.rb
|
143
|
+
- lib/delayed_paperclip/jobs/delayed_job.rb
|
126
144
|
- lib/delayed_paperclip/jobs/sidekiq.rb
|
127
|
-
- lib/delayed_paperclip/
|
145
|
+
- lib/delayed_paperclip/railtie.rb
|
128
146
|
- lib/delayed_paperclip/jobs.rb
|
129
|
-
- lib/delayed_paperclip/url_generator.rb
|
130
|
-
- lib/delayed_paperclip.rb
|
131
|
-
- test/database.yml
|
132
|
-
- test/sidekiq_paperclip.rb
|
133
|
-
- test/delayed_paperclip_test.rb
|
134
147
|
- test/resque_paperclip_test.rb
|
148
|
+
- test/delayed_paperclip_test.rb
|
149
|
+
- test/database.yml
|
150
|
+
- test/fixtures/12k.png
|
135
151
|
- test/base_delayed_paperclip_test.rb
|
152
|
+
- test/sidekiq_paperclip.rb
|
136
153
|
- test/test_helper.rb
|
137
|
-
- test/fixtures/12k.png
|
138
154
|
- rails/init.rb
|
139
155
|
homepage: http://github.com/jrgifford/delayed_paperclip
|
140
156
|
licenses: []
|
@@ -156,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
172
|
version: '0'
|
157
173
|
requirements: []
|
158
174
|
rubyforge_project:
|
159
|
-
rubygems_version: 1.8.
|
175
|
+
rubygems_version: 1.8.23
|
160
176
|
signing_key:
|
161
177
|
specification_version: 3
|
162
178
|
summary: Process your Paperclip attachments in the background.
|