Skip to content

Commit

Permalink
Making use of Paperclip's most_appropriate_url method, instead of ove…
Browse files Browse the repository at this point in the history
…rwriting the url method.
  • Loading branch information
Bertg committed Nov 12, 2011
1 parent 3c0ff49 commit 78b7c48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 14 additions & 17 deletions lib/delayed_paperclip/attachment.rb
Expand Up @@ -7,7 +7,7 @@ def self.included(base)
base.alias_method_chain :post_processing, :delay
base.alias_method_chain :post_processing=, :delay
base.alias_method_chain :save, :prepare_enqueueing
base.alias_method_chain :url, :processed
base.alias_method_chain :most_appropriate_url, :processed
base.alias_method_chain :post_process_styles, :processing
end

Expand All @@ -21,9 +21,13 @@ def post_processing_with_delay=(value)
@post_processing_with_delay = value
end

def delayed_options
@instance.class.attachment_definitions[@name][:delayed]
end

def delay_processing?
if @post_processing_with_delay.nil?
!!@instance.class.attachment_definitions[@name][:delayed]
!!delayed_options
else
!@post_processing_with_delay
end
Expand Down Expand Up @@ -53,25 +57,18 @@ def save_with_prepare_enqueueing
end
end

def url_with_processed style = default_style, include_updated_timestamp = @options.use_timestamp
return url_without_processed style, include_updated_timestamp if !@instance.class.attachment_definitions[@name][:delayed].try(:[], :url_with_processing) || job_is_processing

if !@instance.respond_to?(:"#{name}_processing?")
url_without_processed style, include_updated_timestamp
def most_appropriate_url_with_processed
if original_filename.nil? || delayed_default_url?
default_url
else
if !processing?
url_without_processed style, include_updated_timestamp
else
if dirty?
url_without_processed style, include_updated_timestamp
else
default_url = @options.default_url.is_a?(Proc) ? @options.default_url.call(self) : @options.default_url
interpolate(default_url, style)
end
end
@options.url
end
end

def delayed_default_url?
!(job_is_processing || dirty? || !delayed_options.try(:[], :url_with_processing) || !(@instance.respond_to?(:"#{name}_processing?") && processing?))
end

end
end
end
4 changes: 2 additions & 2 deletions test/base_delayed_paperclip_test.rb
Expand Up @@ -73,7 +73,7 @@ def test_processed_true_when_delayed_jobs_completed
def test_unprocessed_image_returns_missing_url
dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
dummy.save!
assert_equal "/images/original/missing.png", dummy.image.url
assert_equal "/images/original/missing.png", dummy.image.url(:original, :timestamp => false)
process_jobs
dummy.reload
assert_match /\/system\/images\/1\/original\/12k.png/, dummy.image.url
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_original_url_if_image_changed
dummy.save!
dummy.image = File.open("#{RAILS_ROOT}/test/fixtures/12k.png")
dummy.save!
assert_equal '/images/original/missing.png', dummy.image.url
assert_equal '/images/original/missing.png', dummy.image.url(:original, :timestamp => false)
process_jobs
assert_match(/system\/images\/.*original.*/, dummy.reload.image.url)
end
Expand Down

0 comments on commit 78b7c48

Please sign in to comment.