Skip to content

Commit

Permalink
Make #identifier available for a retrieved file. Fixes #1581
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed May 19, 2019
1 parent 818ad98 commit 3f540b9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/carrierwave/uploader/proxy.rb
Expand Up @@ -23,14 +23,14 @@ def current_path
alias_method :path, :current_path

##
# Returns a string that uniquely identifies the last stored file
# Returns a string that uniquely identifies the retrieved or last stored file
#
# === Returns
#
# [String] uniquely identifies a file
#
def identifier
storage.try(:identifier)
@identifier || storage.try(:identifier)
end

##
Expand Down
5 changes: 3 additions & 2 deletions lib/carrierwave/uploader/store.rb
Expand Up @@ -11,7 +11,7 @@ module Store
prepend Module.new {
def initialize(*)
super
@file, @filename, @cache_id = nil
@file, @filename, @cache_id, @identifier = nil
end
}
end
Expand Down Expand Up @@ -69,7 +69,7 @@ def store!(new_file=nil)
cache_storage.delete_dir!(cache_path(nil))
end
@file = new_file
@cache_id = nil
@cache_id = @identifier = nil
end
end
end
Expand All @@ -84,6 +84,7 @@ def store!(new_file=nil)
def retrieve_from_store!(identifier)
with_callbacks(:retrieve_from_store, identifier) do
@file = storage.retrieve!(identifier)
@identifier = identifier
end
end

Expand Down
9 changes: 8 additions & 1 deletion spec/uploader/store_spec.rb
Expand Up @@ -66,6 +66,13 @@
expect(@uploader.identifier).to eq('this-is-me')
end

it "should clear the retrieved identifier when new file is stored" do
allow(@storage).to receive(:retrieve!).and_return(@stored_file)
@uploader.retrieve_from_store!('monkey.txt')
@uploader.store!(@file)
expect(@uploader.identifier).to eq('this-is-me')
end

it "should, if a file is given as argument, cache that file" do
expect(@uploader).to receive(:cache!).with(@file)
@uploader.store!(@file)
Expand Down Expand Up @@ -203,7 +210,7 @@

it "should set the identifier" do
@uploader.retrieve_from_store!('monkey.txt')
expect(@uploader.identifier).to eq('this-is-me')
expect(@uploader.identifier).to eq('monkey.txt')
end

it "should instruct the storage engine to retrieve the file and store the result" do
Expand Down

0 comments on commit 3f540b9

Please sign in to comment.