Skip to content

Commit

Permalink
Atomic_promote fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Art3606 committed Jan 2, 2020
1 parent 87a1764 commit 27bec0c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/shrine/plugins/mongoid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ def mongoid_persist

# Yields the reloaded record. Used by the _persistence plugin.
def mongoid_reload
record_copy = record.dup
record_copy.id = record.id

record_copy.reload unless record_copy.embedded?
if record.embedded?
parent_copy = record._parent.dup
parent_copy.id = record._parent.id
parent_copy.reload
record_copy = parent_copy._children.find do |child|
child.id == record.id
end
else
record_copy = record.dup
record_copy.id = record.id
record_copy.reload
end

yield record_copy
end
Expand Down
16 changes: 16 additions & 0 deletions test/mongoid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,22 @@ def validate
assert_equal file, attacher.file
end
end

describe "#atomic_promote" do
it "promotes cached file to permanent storage" do
photo = Photo.new(user: @user)
attacher = @shrine::Attacher.from_model(photo, :image)

attacher.attach_cached(fakeio)
photo.save

attacher.atomic_promote

assert attacher.stored?
attacher.reload
assert attacher.stored?
end
end
end
end
end
Expand Down

0 comments on commit 27bec0c

Please sign in to comment.