Skip to content

Commit

Permalink
Merge pull request #2519 from skyeagle/url-twice-call
Browse files Browse the repository at this point in the history
Performance improvement, fixes twice #url method call
  • Loading branch information
mshibuya committed Jan 17, 2021
2 parents 3084b24 + 43ee674 commit 3d1259f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
9 changes: 6 additions & 3 deletions lib/carrierwave/uploader/url.rb
Expand Up @@ -15,9 +15,12 @@ module Url
# [String] the location where this file is accessible via a url
#
def url(options = {})
if file.respond_to?(:url) && !(tmp_url = file.url).blank?
file.method(:url).arity.zero? ? tmp_url : file.url(options)
elsif file.respond_to?(:path)
if file.respond_to?(:url)
tmp_url = file.method(:url).arity.zero? ? file.url : file.url(options)
return tmp_url if tmp_url.present?
end

if file.respond_to?(:path)
path = encode_path(file.path.sub(File.expand_path(root), ''))

if host = asset_host
Expand Down
24 changes: 13 additions & 11 deletions spec/uploader/url_spec.rb
Expand Up @@ -60,21 +60,23 @@
end
end

context "when File#url method doesn't get params" do
context "File#url" do
let(:file_class) { FileX = Class.new }
let(:file) { file_class.new }

before do
module StorageX
class File
def url
true
end
end
end
allow(uploader).to receive(:file).and_return(file)
end

allow(uploader).to receive(:file).and_return(StorageX::File.new)
it "does not accept arguments" do
file.define_singleton_method(:url) { true }
uploader.url
end

it "raises ArgumentError" do
expect { uploader.url }.not_to raise_error
it "does accept arguments" do
file.define_singleton_method(:url) { |x = true| x }
expect(file).to receive(:url).once.and_call_original
uploader.url
end
end

Expand Down

0 comments on commit 3d1259f

Please sign in to comment.