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

Duplicate the context hash when duplicating an Attacher #653

Merged
merged 1 commit into from
Sep 17, 2023
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
7 changes: 7 additions & 0 deletions lib/shrine/attacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ def shrine_class

private

# The copy constructor that's called on #dup and #clone
# We need to duplicate the context to prevent it from being shared
def initialize_copy(other)
super
@context = @context.dup
end

# Converts a String or Hash value into an UploadedFile object and ensures
# it's uploaded to temporary storage.
#
Expand Down
7 changes: 7 additions & 0 deletions test/attacher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
@shrine = @attacher.shrine_class
end

describe '#initialize_copy' do
it 'duplicates the context when duplicating the attacher' do
attacher_copy = @attacher.dup
refute_equal @attacher.context.object_id, attacher_copy.context.object_id
end
end

describe ".from_data" do
it "instantiates an attacher from file data" do
file = @attacher.upload(fakeio)
Expand Down
1 change: 1 addition & 0 deletions test/plugin/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
assert_equal model_copy, model_copy.file_attacher.record
assert_equal :file, model_copy.file_attacher.name
assert_equal Hash[record: model_copy, name: :file], model_copy.file_attacher.context
assert_equal Hash[record: model, name: :file], model.file_attacher.context
assert model_copy.file_attacher.changed? # retains any state
end

Expand Down