Skip to content
This repository has been archived by the owner on Oct 5, 2018. It is now read-only.

Commit

Permalink
fix default urs for split processing
Browse files Browse the repository at this point in the history
When doing split processing, we have some styles already available, even
though the background processing is still ongoing. We must return those
instead of the missing picture url
  • Loading branch information
Giorgos Avramidis committed Feb 7, 2014
1 parent 1fdded7 commit b653d91
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 21 deletions.
17 changes: 12 additions & 5 deletions lib/delayed_paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,22 @@ def delay_processing?
!@post_processing_with_delay
end
end

def split_processing?
@instance.class.paperclip_definitions[@name][:only_process] &&
@instance.class.paperclip_definitions[@name][:only_process] !=
@instance.class.paperclip_definitions[@name][:delayed][:only_process]
@instance.class.paperclip_definitions[@name][:only_process] &&
@instance.class.paperclip_definitions[@name][:only_process] !=
delayed_options[:only_process]
end

def processing?
@instance.send(:"#{@name}_processing?")
column_name = :"#{@name}_processing?"
@instance.respond_to?(column_name) && @instance.send(column_name)
end

def processing_style?(style)
return false if !processing?

!split_processing? || delayed_options[:only_process].include?(style)
end

def process_delayed!
Expand Down
26 changes: 22 additions & 4 deletions lib/delayed_paperclip/url_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,21 @@ module UrlGenerator
def self.included(base)
base.alias_method_chain :most_appropriate_url, :processed
base.alias_method_chain :timestamp_possible?, :processed
base.alias_method_chain :for, :processed
end

def most_appropriate_url_with_processed
if @attachment.original_filename.nil? || delayed_default_url?
def for_with_processed(style_name, options)
most_appropriate_url = most_appropriate_url(style_name)

escape_url_as_needed(
timestamp_as_needed(
@attachment_options[:interpolator].interpolate(most_appropriate_url, @attachment, style_name),
options
), options)
end

def most_appropriate_url_with_processed(style = nil)
if @attachment.original_filename.nil? || delayed_default_url?(style)
if @attachment.delayed_options.nil? || @attachment.processing_image_url.nil? || !@attachment.processing?
default_url
else
Expand All @@ -27,11 +38,11 @@ def timestamp_possible_with_processed?
end
end

def delayed_default_url?
def delayed_default_url?(style = nil)
return false if @attachment.job_is_processing
return false if @attachment.dirty?
return false if not @attachment.delayed_options.try(:[], :url_with_processing)
return false if not (@attachment.instance.respond_to?(:"#{@attachment.name}_processing?") && @attachment.processing?)
return false if not processing?(style)
true

# OLD CRAZY CONDITIONAL
Expand All @@ -43,6 +54,13 @@ def delayed_default_url?
# !(@attachment.instance.respond_to?(:"#{@attachment.name}_processing?") && @attachment.processing?)
# )
end

private
def processing?(style)
return true if @attachment.processing?

return @attachment.processing_style?(style) if style
end
end

end
61 changes: 57 additions & 4 deletions spec/delayed_paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

describe DelayedPaperclip::Attachment do

before :all do
before :each do
DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque
reset_dummy
reset_dummy(dummy_options)
end

let(:dummy_options) { {} }
let(:dummy) { Dummy.create }

describe "#delayed_options" do
Expand All @@ -20,7 +21,6 @@
:queue => nil
}
end

end

describe "#post_processing_with_delay" do
Expand Down Expand Up @@ -52,6 +52,59 @@
dummy.expects(:image_processing?)
dummy.image.processing?
end

context "without a processing column" do
let(:dummy_options) { { with_processed: false } }

it "returns false" do
expect(dummy.image.processing?).to be_false
end
end
end

describe "processing_stye?" do
let(:style) { :background }
let(:processing_style?) { dummy.image.processing_style?(style) }

context "without a processing column" do
let(:dummy_options) { { with_processed: true, process_column: false } }

specify { expect(processing_style?).to be_false }
end

context "with a processing column" do
context "when not processing" do
before { dummy.image_processing = false }

specify { expect(processing_style?).to be_false }
end

context "when processing" do
before { dummy.image_processing = true }

context "when not split processing" do
specify { expect(processing_style?).to be_true }
end

context "when split processing" do
let(:dummy_options) { {
paperclip: {
styles: {
online: "400x400x",
background: "600x600x"
},
only_process: [:online]
},

delayed_paperclip: {
only_process: [:background]
}
}}

specify { expect(processing_style?).to be }
end
end
end
end

describe "process_delayed!" do
Expand Down Expand Up @@ -158,4 +211,4 @@
dummy.image.instance_variable_get(:@post_processing_with_delay).should == true
end
end
end
end
41 changes: 36 additions & 5 deletions spec/delayed_paperclip/url_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
require 'spec_helper'

describe DelayedPaperclip::UrlGenerator do
before :all do
before :each do
DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque
reset_dummy
reset_dummy(dummy_options)
end

let(:dummy) { Dummy.create }
let(:attachment) { dummy.image }

let(:dummy_options) { {} }

describe "for_with_processed" do
context "with split pcoessing" do
# everything in this hash is passed to delayed_paperclip, expect for the
# paperclip stuff
let(:dummy_options) { {
paperclip: {
styles: {
online: "400x400x",
background: "600x600x"
},
only_process: [:online]
},

only_process: [:background]
}}

it "returns the default_url when the style is still being processed" do
expect(attachment.url(:background)).to eql "/images/background/missing.png"
end
end
end

describe "#most_appropriate_url_with_processed" do
context "without delayed_default_url" do
Expand All @@ -17,7 +39,6 @@
before :each do
subject.stubs(:delayed_default_url?).returns false
end

context "with original file name" do
before :each do
attachment.stubs(:original_filename).returns "blah"
Expand Down Expand Up @@ -121,6 +142,7 @@
attachment.delayed_options[:url_with_processing] = true
attachment.instance.stubs(:respond_to?).with(:image_processing?).returns true
attachment.stubs(:processing?).returns true
attachment.stubs(:processing_style?).with(anything).returns true
end

it "has all false, delayed_default_url returns true" do
Expand Down Expand Up @@ -167,5 +189,14 @@
subject.delayed_default_url?.should be_false
end
end

context "style is provided and is being processed" do
let(:style) { :main }
before :each do
attachment.stubs(:processing_style?).with(style).returns(true)
end

specify { expect(subject.delayed_default_url?(style)).to be }
end
end
end
end
8 changes: 5 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,23 @@

# Reset table and class with image_processing column or not
def reset_dummy(options = {})

options[:with_processed] = true unless options.key?(:with_processed)
build_dummy_table(options[:with_processed])
options[:processed_column] = options[:with_processed] unless options.has_key?(:processed_column)
build_dummy_table(options.delete(:processed_column))
reset_class("Dummy", options)
end

# Dummy Table for images
# with or without image_processing column
def build_dummy_table(with_processed)
def build_dummy_table(with_column)
ActiveRecord::Base.connection.create_table :dummies, :force => true do |t|
t.string :name
t.string :image_file_name
t.string :image_content_type
t.integer :image_file_size
t.datetime :image_updated_at
t.boolean(:image_processing, :default => false) if with_processed
t.boolean(:image_processing, :default => false) if with_column
end
end

Expand Down

0 comments on commit b653d91

Please sign in to comment.