Skip to content

Commit

Permalink
Allow retrieval of uploader index within uploaders. Closes #1771
Browse files Browse the repository at this point in the history
  • Loading branch information
mshibuya committed Jun 18, 2019
1 parent 19c97c7 commit 1a2ce6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/carrierwave/uploader/mountable.rb
Expand Up @@ -33,6 +33,12 @@ def initialize(model=nil, mounted_as=nil)
@mounted_as = mounted_as
end

##
# Returns array index of given uploader within currently mounted uploaders
#
def index
model.__send__(:_mounter, mounted_as).uploaders.index(self)
end
end # Mountable
end # Uploader
end # CarrierWave
13 changes: 13 additions & 0 deletions spec/uploader/mountable_spec.rb
Expand Up @@ -23,4 +23,17 @@
expect(uploader.mounted_as).to eq(:llama)
end
end

describe '#index' do
let(:model) { Class.new.send(:extend, CarrierWave::Mount) }
let(:instance) { model.new }
before do
model.mount_uploaders(:images, uploader_class)
instance.images = [stub_file('test.jpg'), stub_file('bork.txt')]
end

it "returns the current index in uploaders" do
expect(instance.images.map(&:index)).to eq [0, 1]
end
end
end

0 comments on commit 1a2ce6c

Please sign in to comment.