Permalink
Browse files

Merge pull request #109 from did/master

the call to the default attribute_will_change method was not enough when...
  • Loading branch information...
2 parents 5b27063 + 53b8752 commit 131b85a87016d44637a89db2d924eb5e36e16abd @rmm5t rmm5t committed Aug 13, 2013
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

0 comments on commit 131b85a

Please sign in to comment.