Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

the call to the default attribute_will_change method was not enough w…

…hen uploading a new file in an embedded document (#64)
  • Loading branch information...
commit 53b8752bba948b33bdf03c3cae49c53828d6381f 1 parent 5b27063
@did did authored
Showing with 15 additions and 0 deletions.
  1. +8 −0 lib/carrierwave/mongoid.rb
  2. +7 −0 spec/mongoid_spec.rb
View
8 lib/carrierwave/mongoid.rb
@@ -57,6 +57,14 @@ def #{column}_changed?
changed_attributes.has_key?("#{column}")
end
+ # The default Mongoid attribute_will_change! method is not enough
+ # when we want to upload a new file in an existing embedded document.
+ # The custom version of that method forces the callbacks to be
+ # ran and so does the upload.
+ def #{column}_will_change!
+ changed_attributes["#{column}"] = '_new_'
+ end
+
def find_previous_model_for_#{column}
if self.embedded?
ancestors = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
View
7 spec/mongoid_spec.rb
@@ -569,6 +569,7 @@ def filename
@class.class_eval do
embeds_many :mongo_locations, cascade_callbacks: true
+ accepts_nested_attributes_for :mongo_locations
end
@doc = @class.new
@@ -592,6 +593,12 @@ def filename
doc.mongo_locations.first[:image].should == 'test.jpeg'
end
+ it "changes the file" do
+ @doc.update_attributes mongo_locations_attributes: { '0' => { _id: @embedded_doc._id, image: stub_file('test.jpeg') } }
+ @doc.reload
+ @doc.mongo_locations.first[:image].should == 'test.jpeg'
+ end
+
describe 'with double embedded documents' do
before do
Please sign in to comment.
Something went wrong with that request. Please try again.