diff --git a/lib/shrine/plugins/mongoid.rb b/lib/shrine/plugins/mongoid.rb index d140f1c..a361d4e 100644 --- a/lib/shrine/plugins/mongoid.rb +++ b/lib/shrine/plugins/mongoid.rb @@ -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 diff --git a/test/mongoid_test.rb b/test/mongoid_test.rb index a304be4..fa0208c 100644 --- a/test/mongoid_test.rb +++ b/test/mongoid_test.rb @@ -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