Skip to content

Commit

Permalink
Add keep-stored and reorder feature for multiple file uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jun 18, 2019
1 parent b57f8a1 commit 0ffad68
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ u.avatars[0].current_path # => 'path/to/file.png'
u.avatars[0].identifier # => 'file.png'
```

If you want to preserve existing files on uploading new one, you can go like:

```erb
<% user.avatars.each do |avatar| %>
<%= hidden_field :user, :avatars, multiple: true, value: avatar.identifier %>
<% end %>
<%= form.file_field :avatars, multiple: true %>
```

Sorting avatars is supported as well by reordering `hidden_field`.

## Changing the storage directory

In order to change where uploaded files are put, just override the `store_dir`
Expand Down
13 changes: 9 additions & 4 deletions lib/carrierwave/mounter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ def uploaders

def cache(new_files)
return if new_files.blank?
old_uploaders = uploaders
@uploaders = new_files.map do |new_file|
uploader = blank_uploader
uploader.cache!(new_file)
uploader
end
if new_file.is_a?(String)
old_uploaders.detect { |uploader| uploader.identifier == new_file }
else
uploader = blank_uploader
uploader.cache!(new_file)
uploader
end
end.compact

@integrity_error = nil
@processing_error = nil
Expand Down
48 changes: 48 additions & 0 deletions spec/mount_multiple_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,54 @@ def monkey

it { expect(instance.images).to be_empty }
end

describe "with stored files" do
before do
instance.images = [text_file_stub, test_file_stub]
instance.store_images!
end
let(:identifiers) { instance.images.map(&:identifier) }

it "preserves existing image of given identifier" do
instance.images = [identifiers[0], old_image_stub]
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['bork.txt','old.jpeg']
end

it "reorders existing image" do
instance.images = identifiers.reverse
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['test.jpg', 'bork.txt']
end

it "allows uploading and reordering at once" do
instance.images = [identifiers[1], old_image_stub, identifiers[0]]
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['test.jpg', 'old.jpeg', 'bork.txt']
end

it "allows repeating the same identifiers" do
instance.images = ['bork.txt', 'test.jpg', 'bork.txt']
instance.store_images!
expect(instance.images.map(&:identifier)).to eq ['bork.txt', 'test.jpg', 'bork.txt']
end

it "removes image which is unused" do
@image_paths = instance.images.map(&:current_path)
instance.images = [identifiers[0]]
instance.store_images!
instance.send(:_mounter, :images).remove_previous(identifiers, identifiers[0..0])
expect(instance.images.map(&:identifier)).to eq ['bork.txt']
expect(File.exist?(@image_paths[0])).to be_truthy
expect(File.exist?(@image_paths[1])).to be_falsey
end

it "ignores unknown identifier" do
instance.images = ['unknown.txt']
expect { instance.store_images! }.not_to raise_error
expect(instance.images.map(&:identifier)).to be_empty
end
end
end

describe '#images?' do
Expand Down

0 comments on commit 0ffad68

Please sign in to comment.