Skip to content

Commit

Permalink
Got Job#url using attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Evans committed Mar 22, 2011
1 parent a8be405 commit 1a78209
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
13 changes: 5 additions & 8 deletions lib/dragonfly/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ def from_a(steps_array, app)
job
end

def from_path(path, app)
path = path.dup
path.sub!(app.url_path_prefix, '') if app.url_path_prefix
path.sub!('/', '')
deserialize(path, app)
end

def deserialize(string, app)
from_a(Serializer.marshal_decode(string), app)
end
Expand Down Expand Up @@ -306,7 +299,7 @@ def validate_sha!(sha)
# URLs, etc.

def url(opts={})
app.url_for(self, opts) unless steps.empty?
app.server.url_for(self, attributes_for_url.merge(opts)) unless steps.empty?
end

def b64_data
Expand Down Expand Up @@ -414,6 +407,10 @@ def ext
def attributes
@attributes ||= {}
end

def attributes_for_url
attributes.reject{|k, v| !app.server.params_in_url.include?(k.to_s) }
end

def update_attributes(attrs)
self.name = attrs[:name] if attrs[:name]
Expand Down
3 changes: 3 additions & 0 deletions lib/dragonfly/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class Server
configurable_attr :protect_from_dos_attacks, false
configurable_attr :url_format, '/media/:job/:basename.:format'
configurable_attr :url_host

extend Forwardable
def_delegator :url_mapper, :params_in_url

def initialize(app)
@app = app
Expand Down
30 changes: 26 additions & 4 deletions spec/dragonfly/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,35 @@
@app = test_app
@job = Dragonfly::Job.new(@app)
end
it "should pass any args straight to url_for" do
@app.should_receive(:url_for).with(@job, :egg => 'head', :host => 'beans').and_return 'hello'
@job.generate!(:blah).url(:egg => 'head', :host => 'beans').should == 'hello'
end

it "should return nil if there are no steps" do
@job.url.should be_nil
end

describe "using attributes in the url" do
before(:each) do
@app.server.url_format = '/media/:job/:zoo'
@job.generate!(:fish)
end
it "should act as per usual if no params given" do
@job.url.should == "/media/#{@job.serialize}"
end
it "should add given params" do
@job.url(:zoo => 'jokes', :on => 'me').should == "/media/#{@job.serialize}/jokes?on=me"
end
it "should use the attribute if it exists" do
@job.attributes[:zoo] = 'hair'
@job.url.should == "/media/#{@job.serialize}/hair"
end
it "should not add an attribute that isn't needed" do
@job.attributes[:gump] = 'flub'
@job.url.should == "/media/#{@job.serialize}"
end
it "should override if a param is passed in" do
@job.attributes[:zoo] = 'hair'
@job.url(:zoo => 'dare').should == "/media/#{@job.serialize}/dare"
end
end
end

describe "to_fetched_job" do
Expand Down

0 comments on commit 1a78209

Please sign in to comment.