Skip to content

Commit 27bec0c

Browse files
committed
Atomic_promote fix
1 parent 87a1764 commit 27bec0c

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

lib/shrine/plugins/mongoid.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,18 @@ def mongoid_persist
9797

9898
# Yields the reloaded record. Used by the _persistence plugin.
9999
def mongoid_reload
100-
record_copy = record.dup
101-
record_copy.id = record.id
102-
103-
record_copy.reload unless record_copy.embedded?
100+
if record.embedded?
101+
parent_copy = record._parent.dup
102+
parent_copy.id = record._parent.id
103+
parent_copy.reload
104+
record_copy = parent_copy._children.find do |child|
105+
child.id == record.id
106+
end
107+
else
108+
record_copy = record.dup
109+
record_copy.id = record.id
110+
record_copy.reload
111+
end
104112

105113
yield record_copy
106114
end

test/mongoid_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,22 @@ def validate
639639
assert_equal file, attacher.file
640640
end
641641
end
642+
643+
describe "#atomic_promote" do
644+
it "promotes cached file to permanent storage" do
645+
photo = Photo.new(user: @user)
646+
attacher = @shrine::Attacher.from_model(photo, :image)
647+
648+
attacher.attach_cached(fakeio)
649+
photo.save
650+
651+
attacher.atomic_promote
652+
653+
assert attacher.stored?
654+
attacher.reload
655+
assert attacher.stored?
656+
end
657+
end
642658
end
643659
end
644660
end

0 commit comments

Comments
 (0)