diff --git a/lib/carrierwave.rb b/lib/carrierwave.rb index 7d5337f27..b3a996973 100644 --- a/lib/carrierwave.rb +++ b/lib/carrierwave.rb @@ -135,3 +135,8 @@ module Test CarrierWave.config[:public] = Sinatra::Application.public end + +# MongoMapper is framework agnostic so we could need this in any environment. +if defined?(MongoMapper) + require File.join(File.dirname(__FILE__), "carrierwave", "orm", "mongomapper") +end diff --git a/lib/carrierwave/orm/mongomapper.rb b/lib/carrierwave/orm/mongomapper.rb new file mode 100644 index 000000000..761aa410d --- /dev/null +++ b/lib/carrierwave/orm/mongomapper.rb @@ -0,0 +1,27 @@ +# encoding: utf-8 +require 'mongomapper' + +module CarrierWave + module MongoMapper + include CarrierWave::Mount + ## + # See +CarrierWave::Mount#mount_uploader+ for documentation + # + def mount_uploader(column, uploader, options={}, &block) + # We need to set the mount_on column (or key in MongoMapper's case) + # since MongoMapper will attempt to set the filename on + # the uploader instead of the file on a Document's initialization. + options[:mount_on] ||= "#{column}_filename" + key options[:mount_on] + + super + alias_method :read_uploader, :[] + alias_method :write_uploader, :[]= + after_save "store_#{column}!".to_sym + before_save "write_#{column}_identifier".to_sym + after_destroy "remove_#{column}!".to_sym + end + end # MongoMapper +end # CarrierWave + +MongoMapper::Document::ClassMethods.send(:include, CarrierWave::MongoMapper) diff --git a/spec/orm/mongomapper_spec.rb b/spec/orm/mongomapper_spec.rb new file mode 100644 index 000000000..ce2741cd1 --- /dev/null +++ b/spec/orm/mongomapper_spec.rb @@ -0,0 +1,184 @@ +# encoding: utf-8 +require File.dirname(__FILE__) + '/../spec_helper' + +require 'carrierwave/orm/mongomapper' + +MongoMapper.database = "carrierwave_test" + +describe CarrierWave::MongoMapper do + + before do + uploader = Class.new(CarrierWave::Uploader::Base) + + @class = Class.new + @class.class_eval do + include MongoMapper::Document + mount_uploader :image, uploader + end + + @uploader = uploader + end + + describe '#image' do + + context "when nothing is assigned" do + + before do + @document = @class.new + end + + it "returns a blank uploader" do + @document.image.should be_blank + end + + end + + context "when an empty string is assigned" do + + before do + @document = @class.new(:image_filename => "") + @document.save + end + + it "returns a blank uploader" do + @saved_doc = @class.find(:first) + @saved_doc.image.should be_blank + end + + end + + context "when a filename is saved in the database" do + + before do + @document = @class.new(:image_filename => "test.jpg") + @document.save + @doc = @class.find(:first) + end + + it "returns an uploader" do + @doc.image.should be_an_instance_of(@uploader) + end + + it "sets the path to the store directory" do + @doc.image.current_path.should == public_path('uploads/test.jpg') + end + + end + + end + + describe '#image=' do + + before do + @doc = @class.new + end + + context "when nil is assigned" do + + it "does not set the value" do + @doc.image = nil + @doc.image.should be_blank + end + + end + + context "when an empty string is assigned" do + + it "does not set the value" do + @doc.image = '' + @doc.image.should be_blank + end + + end + + context "when a file is assigned" do + + it "should cache a file" do + @doc.image = stub_file('test.jpeg') + @doc.image.should be_an_instance_of(@uploader) + end + + it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do + @doc[:image_filename].should be_nil + end + + it "should copy a file into into the cache directory" do + @doc.image = stub_file('test.jpeg') + @doc.image.current_path.should =~ /^#{public_path('uploads/tmp')}/ + end + + end + + end + + describe "#save" do + + before do + @doc = @class.new + end + + context "when no file is assigned" do + + it "image is blank" do + @doc.save + @doc.image.should be_blank + end + + end + + context "when a file is assigned" do + + it "copies the file to the upload directory" do + @doc.image = stub_file('test.jpg') + @doc.save + @doc.image.should be_an_instance_of(@uploader) + @doc.image.current_path.should == public_path('uploads/test.jpg') + end + + it "saves the filename in the database" do + @doc.image = stub_file('test.jpg') + @doc.save + @doc[:image_filename].should == 'test.jpg' + end + + context "when remove_image? is true" do + + it "removes the image" do + @doc.image = stub_file('test.jpeg') + @doc.save + @doc.remove_image = true + @doc.save + @doc.image.should be_blank + @doc[:image_filename].should == '' + end + + end + + end + + end + + describe '#destroy' do + + before do + @doc = @class.new + end + + context "when file assigned" do + + it "removes the file from the filesystem" do + @doc.image = stub_file('test.jpeg') + @doc.save.should be_true + File.exist?(public_path('uploads/test.jpeg')).should be_true + @doc.image.should be_an_instance_of(@uploader) + @doc.image.current_path.should == public_path('uploads/test.jpeg') + @doc.destroy + File.exist?(public_path('uploads/test.jpeg')).should be_false + end + + end + + end + + +end \ No newline at end of file