Skip to content

Commit

Permalink
url on a unpersisted record returns default_url
Browse files Browse the repository at this point in the history
It should never be the case that `url` returns `nil`. If there is no url
that can be reasonable achieved, as is the case when the record the
attachment is attached to is unpersisted, then `url` should return the
default url.
  • Loading branch information
Jon Yurek committed Jun 26, 2015
1 parent 7f732ee commit 36095bc
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 4 deletions.
2 changes: 0 additions & 2 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ def assign(uploaded_file)
# +#for(style_name, options_hash)+

def url(style_name = default_style, options = {})
return nil if @instance.new_record?

if options == true || options == false # Backwards compatibility.
@url_generator.for(style_name, default_options.merge(:timestamp => options))
else
Expand Down
5 changes: 3 additions & 2 deletions spec/paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,16 @@

context "without an Attachment" do
before do
rebuild_model default_url: "default.url"
@dummy = Dummy.new
end

it "returns false when asked exists?" do
assert !@dummy.avatar.exists?
end

it "#url returns nil" do
assert_nil @dummy.avatar.url
it "#url returns the default_url" do
expect(@dummy.avatar.url).to eq "default.url"
end
end

Expand Down

0 comments on commit 36095bc

Please sign in to comment.