Permalink
Browse files

attach a first file in an existing embedded document

  • Loading branch information...
1 parent 28b8340 commit 5010c270ae1911f8067ba832aa0b1908cef03fda @did did committed May 6, 2013
Showing with 16 additions and 1 deletion.
  1. +4 −0 lib/carrierwave/mongoid.rb
  2. +12 −1 spec/mongoid_spec.rb
@@ -35,6 +35,10 @@ def mount_uploader(column, uploader=nil, options={}, &block)
class_eval <<-RUBY, __FILE__, __LINE__+1
def #{column}=(new_file)
column = _mounter(:#{column}).serialization_column
+
+ # mongoid won't upload a new file if there was no file previously.
+ write_uploader(column, '_old_') if self.persisted? && read_uploader(column).nil?
+
send(:"\#{column}_will_change!")
super
end
View
@@ -568,7 +568,7 @@ def filename
end
@class.class_eval do
- embeds_many :mongo_locations
+ embeds_many :mongo_locations, cascade_callbacks: true
end
@doc = @class.new
@@ -579,6 +579,17 @@ def filename
include_examples "embedded documents"
+ it "attaches a new file to the document which didn't have one at first" do
+ doc = @class.new
+ doc.mongo_locations.build
+ doc.save & doc.reload
+
+ doc.mongo_locations.first.image = stub_file('test.jpeg')
+ doc.save & doc.reload
+
+ doc.mongo_locations.first[:image].should == 'test.jpeg'
+ end
+
describe 'with double embedded documents' do
before do

0 comments on commit 5010c27

Please sign in to comment.