spec/mongoid_spec.rb in glebtv-carrierwave-mongoid-0.6.4 vs spec/mongoid_spec.rb in glebtv-carrierwave-mongoid-0.6.5

- old
+ new

@@ -567,10 +567,11 @@ embedded_in :mongo_user end @class.class_eval do embeds_many :mongo_locations, cascade_callbacks: true + accepts_nested_attributes_for :mongo_locations end @doc = @class.new @embedded_doc = @doc.mongo_locations.build @embedded_doc.image = stub_file('old.jpeg') @@ -590,10 +591,22 @@ doc.reload 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 + + it "removes a file" do + @doc.update_attributes mongo_locations_attributes: { '0' => { _id: @embedded_doc._id, remove_image: "1" } } + @doc.reload + @doc.mongo_locations.first[:image].should_not be_present + end + describe 'with double embedded documents' do before do @double_embedded_doc_class = define_mongo_class('MongoItem') do include Mongoid::Document @@ -662,9 +675,41 @@ @double_embedded_doc.image = stub_file('old.jpeg') @double_embedded_doc.save.should be_true end include_examples "double embedded documents" + end + end + + describe 'with embedded documents and nested attributes' do + before do + @embedded_doc_class = define_mongo_class('MongoLocation') do + include Mongoid::Document + mount_uploader :image, @uploader + embedded_in :mongo_user + end + + @class.class_eval do + embeds_many :mongo_locations, cascade_callbacks: true + accepts_nested_attributes_for :mongo_locations + end + + @doc = @class.new(mongo_locations_attributes: [{image: stub_file("old.jpeg")}]) + @doc.save.should be_true + @embedded_doc = @doc.mongo_locations.first + end + + it "should set the image on a save" do + @doc.reload + @doc.mongo_locations.first.image.path.should match(/old\.jpeg$/) + @embedded_doc.image.path.should match(/old\.jpeg$/) + end + + it "should update the image on update_attributes" do + @doc.update_attributes(mongo_locations_attributes: [{id: @embedded_doc.id, image: stub_file("new.jpeg")}]).should be_true + @doc.reload + @doc.mongo_locations.first.image.path.should match(/new\.jpeg$/) + @embedded_doc.image.path.should match(/new\.jpeg$/) end end end describe '#mount_uploader removing old files with versions' do