Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #dup with custom serialisation #2771

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/carrierwave/orm/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def initialize_dup(other)
super
@_mounters[:"#{column}"] = nil
# The attribute needs to be cleared to prevent it from picked up as identifier
write_attribute(_mounter(:#{column}).serialization_column, nil)
write_uploader(_mounter(:#{column}).serialization_column, nil)
_mounter(:"#{column}").cache(old_uploaders)
end

Expand Down
24 changes: 24 additions & 0 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2181,6 +2181,30 @@ def initialize_dup(*)
end
end

context 'when #write_uploader is overridden in the model' do
before do
Event.class_eval do
attribute :called_write_uploader_with, :json, default: {}
def write_uploader(*args)
self.called_write_uploader_with[args] ||= 0
self.called_write_uploader_with[args] += 1
write_attribute(*args)
end
end
@event = Event.new
end

it "clears previous attribute value to prevent unintended deduplication" do
@event.image = stub_file('test.jpeg')
@event.save
new_event = @event.dup
new_event.save

expect(new_event.called_write_uploader_with[[:image, nil].to_s]).to eq 1
expect(new_event.image.url).to eq '/uploads/test.jpeg'
end
end

context ':mount_on in options' do
before { Event.mount_uploader(:image_v2, @uploader, mount_on: :image) }
it do
Expand Down