Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Consistency with mongoid ~> 2.1 and fully extracted from carrierwave(…

…0.5.6)
  • Loading branch information...
commit 74c29cab6961ece91cbd702e3e04d892ddc57091 1 parent 1bd92e2
@skyeagle skyeagle authored
View
3  .gitignore
@@ -3,4 +3,5 @@ pkg/*
.bundle
spec/public
.rvmrc
-Gemfile.lock
+Gemfile.lock
+log/*
View
68 README.md
@@ -0,0 +1,68 @@
+# CarrierWave for Mongoid
+
+This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the
+CarrierWave documentation for more detailed usage instructions.
+
+### This version supports ONLY version of mongoid ~> 2.1
+Keep in mind that if you came up from previous versions you should make a few steps to go with it:
+
+* change(rename in db) field name from `avatar_name` to `avatar`(appropriate to your uploader name)
+* fix you code where you need to operate with uploaded file's filename from `avatar` to `avatar_identifier`
+
+Install it like this:
+
+ gem install carrierwave-mongoid
+
+Use it like this:
+
+```ruby
+require 'carrierwave/mongoid'
+```
+
+Make sure to disable auto_validation on the mounted column.
+
+Using bundler:
+
+```ruby
+gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
+```
+
+This used to be part of CarrierWave but has been extracted.
+
+## Using MongoDB's GridFS store
+
+You'll need to configure the database and host to use:
+
+```ruby
+CarrierWave.configure do |config|
+ config.grid_fs_database = 'my_mongo_database'
+ config.grid_fs_host = 'mongo.example.com'
+end
+```
+
+The defaults are `carrierwave` and `localhost`.
+
+And then in your uploader, set the storage to `:grid_fs`:
+
+```ruby
+class AvatarUploader < CarrierWave::Uploader::Base
+ storage :grid_fs
+end
+```
+
+Since GridFS doesn't make the files available via HTTP, you'll need to stream
+them yourself. In Rails for example, you could use the `send_data` method. You
+can tell CarrierWave the URL you will serve your images from, allowing it to
+generate the correct URL, by setting eg:
+
+```ruby
+CarrierWave.configure do |config|
+ config.grid_fs_access_url = "/image/show"
+end
+```
+
+## Known issues/ limitations
+
+If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
+You must explicitly call save on embedded documents in order to save their attached files.
+You can read more about this [here](https://github.com/jnicklas/carrierwave/issues#issue/81)
View
53 README.rdoc
@@ -1,53 +0,0 @@
-= CarrierWave for Mongoid
-
-This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the
-CarrierWave documentation for more detailed usage instructions.
-
-Install it like this:
-
- gem install carrierwave-mongoid
-
-Use it like this:
-
- require 'carrierwave/mongoid'
-
-Make sure to disable auto_validation on the mounted column.
-
-Using bundler:
-
- gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
-
-This used to be part of CarrierWave but has been extracted.
-
-== Using MongoDB's GridFS store
-
-You'll need to configure the database and host to use:
-
- CarrierWave.configure do |config|
- config.grid_fs_database = 'my_mongo_database'
- config.grid_fs_host = 'mongo.example.com'
- end
-
-The defaults are 'carrierwave' and 'localhost'.
-
-And then in your uploader, set the storage to <code>:grid_fs</code>:
-
- class AvatarUploader < CarrierWave::Uploader::Base
- storage :grid_fs
- end
-
-Since GridFS doesn't make the files available via HTTP, you'll need to stream
-them yourself. In Rails for example, you could use the +send_data+ method. You
-can tell CarrierWave the URL you will serve your images from, allowing it to
-generate the correct URL, by setting eg:
-
- CarrierWave.configure do |config|
- config.grid_fs_access_url = "/image/show"
- end
-
-== Known issues/ limitations
-
-If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
-You must explicitly call save on embedded documents in order to save their attached files.
-You can read more about this {here}[https://github.com/jnicklas/carrierwave/issues#issue/81]
-
View
11 carrierwave-mongoid.gemspec
@@ -19,9 +19,10 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- s.add_dependency "carrierwave"
- s.add_dependency "mongoid"
- s.add_development_dependency "rspec", ["~> 2.0"]
- s.add_development_dependency "bson_ext", ["> 1.3.0"]
- s.add_development_dependency "sqlite3"
+ s.add_dependency "carrierwave", [">= 0.5.6"]
+ s.add_dependency "mongoid", ["~> 2.1"]
+ s.add_development_dependency "rspec", ["~> 2.6"]
+ s.add_development_dependency "bson_ext", ["~> 1.3"]
+ s.add_development_dependency "rake", ["~> 0.9"]
+ s.add_development_dependency "mini_magick"
end
View
32 lib/carrierwave/mongoid.rb
@@ -1,6 +1,7 @@
# encoding: utf-8
require 'mongoid'
+require 'carrierwave'
require 'carrierwave/validations/active_model'
module CarrierWave
@@ -10,22 +11,43 @@ module Mongoid
# See +CarrierWave::Mount#mount_uploader+ for documentation
#
def mount_uploader(column, uploader=nil, options={}, &block)
- options[:mount_on] ||= "#{column}_filename"
- field options[:mount_on]
+ field options[:mount_on] || column
super
alias_method :read_uploader, :read_attribute
alias_method :write_uploader, :write_attribute
+ public :read_uploader
+ public :write_uploader
include CarrierWave::Validations::ActiveModel
validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
- after_save "store_#{column}!".to_sym
- before_save "write_#{column}_identifier".to_sym
- after_destroy "remove_#{column}!".to_sym
+ after_save :"store_#{column}!"
+ before_save :"write_#{column}_identifier"
+ after_destroy :"remove_#{column}!"
+ before_update :"store_previous_model_for_#{column}"
+ after_save :"remove_previously_stored_#{column}"
+
+ class_eval <<-RUBY, __FILE__, __LINE__+1
+ def #{column}=(new_file)
+ column = _mounter(:#{column}).serialization_column
+ send(:"\#{column}_will_change!")
+ super
+ 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? }
+ reloaded_parent = ancestors.first.last.reload
+ ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }.find(to_key.first)
+ else
+ self.class.find(to_key.first)
+ end
+ end
+ RUBY
end
end # Mongoid
end # CarrierWave
View
0  log/.gitkeep
No changes.
View
1  spec/fixtures/new.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  spec/fixtures/new.txt
@@ -0,0 +1 @@
+bork bork bork Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
View
1  spec/fixtures/old.jpeg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  spec/fixtures/old.txt
@@ -0,0 +1 @@
+bork bork bork Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
View
389 spec/mongoid_spec.rb
@@ -1,19 +1,21 @@
# encoding: utf-8
require 'spec_helper'
-connection = Mongo::Connection.new
-Mongoid.database = connection.db("carrierwave_test")
-
+require 'carrierwave/mongoid'
def reset_mongo_class(uploader = MongoUploader)
- class_name = 'MongoUser'
- Object.send(:remove_const, class_name) rescue nil
- klass = Object.const_set(class_name, Class.new)
- klass.class_eval do
+ define_mongo_class('MongoUser') do
include Mongoid::Document
store_in :users
+ field :folder, :default => ''
mount_uploader :image, uploader
end
+end
+
+def define_mongo_class(class_name, &block)
+ Object.send(:remove_const, class_name) rescue nil
+ klass = Object.const_set(class_name, Class.new)
+ klass.class_eval(&block)
klass
end
@@ -61,7 +63,7 @@ def extension_white_list
before do
mongo_user_klass = reset_mongo_class
- @document = mongo_user_klass.new(:image_filename => "")
+ @document = mongo_user_klass.new(:image => "")
@document.save
end
@@ -76,7 +78,9 @@ def extension_white_list
before do
mongo_user_klass = reset_mongo_class
- @document = mongo_user_klass.new(:image_filename => "test.jpg")
+ @document = mongo_user_klass.new
+ # should retrieve a file from the storage if a value is stored in the database
+ @document[:image] = "test.jpg" # NOT @document.image = 'text.jpg'
@document.save
@doc = MongoUser.first
end
@@ -126,7 +130,7 @@ def extension_white_list
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
+ @doc[:image].should be_nil
end
it "should copy a file into into the cache directory" do
@@ -151,13 +155,13 @@ def extension_white_list
@doc.valid?
@doc.errors[:image].should == ['is not an allowed file type']
- change_locale_and_store_translations(:pt,
+ change_locale_and_store_translations(:pt, :mongoid => {
:errors => {
- :messages => {
- :carrierwave_integrity_error => 'tipo de imagem não permitido.'
- }
- }
- ) do
+ :messages => {
+ :carrierwave_integrity_error => 'tipo de imagem não permitido.'
+ }
+ }
+ }) do
@doc.should_not be_valid
@doc.errors[:image].should == ['tipo de imagem não permitido.']
end
@@ -179,13 +183,13 @@ def extension_white_list
@doc.valid?
@doc.errors[:image].should == ['failed to be processed']
- change_locale_and_store_translations(:pt,
+ change_locale_and_store_translations(:pt, :mongoid => {
:errors => {
- :messages => {
- :carrierwave_processing_error => 'falha ao processar imagem.'
- }
- }
- ) do
+ :messages => {
+ :carrierwave_processing_error => 'falha ao processar imagem.'
+ }
+ }
+ }) do
@doc.should_not be_valid
@doc.errors[:image].should == ['falha ao processar imagem.']
end
@@ -196,6 +200,13 @@ def extension_white_list
describe "#save" do
+ it "after it was initialized with params" do
+ doc = reset_mongo_class.new(:image => stub_file('test.jpg'))
+ doc.save.should be_true
+ doc.image.should be_an_instance_of(MongoUploader)
+ doc.image.current_path.should == public_path('uploads/test.jpg')
+ end
+
before do
mongo_user_klass = reset_mongo_class
@doc = mongo_user_klass.new
@@ -204,7 +215,7 @@ def extension_white_list
context "when no file is assigned" do
it "image is blank" do
- @doc.save
+ @doc.save.should be_true
@doc.image.should be_blank
end
@@ -214,15 +225,16 @@ def extension_white_list
it "copies the file to the upload directory" do
@doc.image = stub_file('test.jpg')
- @doc.save
+ @doc.save.should be_true
@doc.image.should be_an_instance_of(MongoUploader)
@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'
+ @doc.save.should be_true
+ @doc[:image].should == 'test.jpg'
+ @doc.image_identifier.should == 'test.jpg'
end
context "when remove_image? is true" do
@@ -231,14 +243,45 @@ def extension_white_list
@doc.image = stub_file('test.jpeg')
@doc.save
@doc.remove_image = true
- @doc.save
+ @doc.save.should be_true
@doc.reload
@doc.image.should be_blank
- @doc.image_filename.should == ''
+ @doc.image_identifier.should == ''
end
end
+ it "should mark image as changed when saving a new image" do
+ @doc.image_changed?.should be_false
+ @doc.image = stub_file("test.jpeg")
+ @doc.image_changed?.should be_true
+ @doc.save
+ @doc.reload
+ @doc.image_changed?.should be_false
+ @doc.image = stub_file("test.jpg")
+ @doc.image_changed?.should be_true
+ end
+
+ end
+
+ end
+
+ describe '#update' do
+
+ before do
+ mongo_user_klass = reset_mongo_class
+ @doc = mongo_user_klass.new
+ @doc.image = stub_file('test.jpeg')
+ @doc.save
+ @doc.reload
+ end
+
+ it "replaced it by a file with the same name" do
+ @doc.image = stub_file('test.jpeg')
+ @doc.save
+ @doc.reload
+ @doc[:image].should == 'test.jpeg'
+ @doc.image_identifier.should == 'test.jpeg'
end
end
@@ -283,5 +326,295 @@ def extension_white_list
end
+ describe '#mount_uploader removing old files' do
+
+ before do
+ @uploader = Class.new(MongoUploader)
+ @class = reset_mongo_class(@uploader)
+ @class.field :foo
+ @doc = @class.new
+ @doc.image = stub_file('old.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ after do
+ FileUtils.rm_rf(file_path("uploads"))
+ end
+
+ describe 'normally' do
+
+ it "should remove old file if old file had a different path" do
+ @doc.image = stub_file('new.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ end
+
+ it "should not remove old file if old file had a different path but config is false" do
+ @uploader.stub!(:remove_previously_stored_files_after_update).and_return(false)
+ @doc.image = stub_file('new.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should not remove file if old file had the same path" do
+ @doc.image = stub_file('old.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should not remove file if validations fail on save" do
+ @class.validate { |r| r.errors.add :textfile, "FAIL!" }
+ @doc.image = stub_file('new.jpeg')
+ @doc.save.should be_false
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+ end
+
+ describe 'with an overriden filename' do
+ before do
+ @uploader.class_eval do
+ def filename
+ model.foo + File.extname(super)
+ end
+ end
+
+ @doc.image = stub_file('old.jpeg')
+ @doc.foo = "test"
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/test.jpeg')).should be_true
+ @doc.image.read.should == "this is stuff"
+ end
+
+ it "should not remove file if old file had the same dynamic path" do
+ @doc.image = stub_file('test.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/test.jpeg')).should be_true
+ end
+
+ it "should remove old file if old file had a different dynamic path" do
+ @doc.foo = "new"
+ @doc.image = stub_file('test.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/test.jpeg')).should be_false
+ end
+ end
+
+ describe 'with embedded documents' 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
+ end
+
+ @doc = @class.new
+ @embedded_doc = @doc.mongo_locations.build
+ @embedded_doc.image = stub_file('old.jpeg')
+ @embedded_doc.save.should be_true
+ end
+
+ it "should remove old file if old file had a different path" do
+ @embedded_doc.image = stub_file('new.jpeg')
+ @embedded_doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ end
+
+ it "should not remove old file if old file had a different path but config is false" do
+ @embedded_doc.image.stub!(:remove_previously_stored_files_after_update).and_return(false)
+ @embedded_doc.image = stub_file('new.jpeg')
+ @embedded_doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should not remove file if old file had the same path" do
+ @embedded_doc.image = stub_file('old.jpeg')
+ @embedded_doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should not remove file if validations fail on save" do
+ @embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
+ @embedded_doc.image = stub_file('new.jpeg')
+ @embedded_doc.save.should be_false
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ describe 'with double embedded documents' do
+
+ before do
+ @double_embedded_doc_class = define_mongo_class('MongoItem') do
+ include Mongoid::Document
+ mount_uploader :image, @uploader
+ embedded_in :mongo_location
+ end
+
+ @embedded_doc_class.class_eval do
+ embeds_many :mongo_items
+ end
+
+ @doc = @class.new
+ @embedded_doc = @doc.mongo_locations.build
+ @embedded_doc.image = stub_file('old.jpeg')
+ @embedded_doc.save.should be_true
+
+ @double_embedded_doc = @embedded_doc.mongo_items.build
+ @double_embedded_doc.image = stub_file('old.jpeg')
+ @double_embedded_doc.save.should be_true
+ end
+
+ it "should remove old file if old file had a different path" do
+ @double_embedded_doc.image = stub_file('new.jpeg')
+ @double_embedded_doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ end
+
+ it "should not remove old file if old file had a different path but config is false" do
+ @double_embedded_doc.image.stub!(:remove_previously_stored_files_after_update).and_return(false)
+ @double_embedded_doc.image = stub_file('new.jpeg')
+ @double_embedded_doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should not remove file if old file had the same path" do
+ @double_embedded_doc.image = stub_file('old.jpeg')
+ @double_embedded_doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ it "should not remove file if validations fail on save" do
+ @double_embedded_doc_class.validate { |r| r.errors.add :textfile, "FAIL!" }
+ @double_embedded_doc.image = stub_file('new.jpeg')
+ @double_embedded_doc.save.should be_false
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ end
+
+ end
+ end
+
+ describe '#mount_uploader removing old files with versions' do
+
+ before do
+ @uploader = Class.new(MongoUploader)
+ @uploader.version :thumb
+ @class = reset_mongo_class(@uploader)
+ @doc = @class.new
+ @doc.image = stub_file('old.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ File.exists?(public_path('uploads/thumb_old.jpeg')).should be_true
+ end
+
+ after do
+ FileUtils.rm_rf(file_path("uploads"))
+ end
+
+ it "should remove old file if old file had a different path" do
+ @doc.image = stub_file('new.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/thumb_new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ File.exists?(public_path('uploads/thumb_old.jpeg')).should be_false
+ end
+
+ it "should not remove file if old file had the same path" do
+ @doc.image = stub_file('old.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ File.exists?(public_path('uploads/thumb_old.jpeg')).should be_true
+ end
+ end
+
+ describe '#mount_uploader removing old files with multiple uploaders' do
+
+ before do
+ @uploader = Class.new(MongoUploader)
+ @class = reset_mongo_class(@uploader)
+ @uploader1 = Class.new(CarrierWave::Uploader::Base)
+ @class.mount_uploader(:textfile, @uploader1)
+ @doc = @class.new
+ @doc.image = stub_file('old.jpeg')
+ @doc.textfile = stub_file('old.txt')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.txt')).should be_true
+ end
+
+ after do
+ FileUtils.rm_rf(file_path("uploads"))
+ end
+
+ it "should remove old file1 and file2 if old file1 and file2 had a different paths" do
+ @doc.image = stub_file('new.jpeg')
+ @doc.textfile = stub_file('new.txt')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ File.exists?(public_path('uploads/new.txt')).should be_true
+ File.exists?(public_path('uploads/old.txt')).should be_false
+ end
+
+ it "should remove old file1 but not file2 if old file1 had a different path but old file2 has the same path" do
+ @doc.image = stub_file('new.jpeg')
+ @doc.textfile = stub_file('old.txt')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ File.exists?(public_path('uploads/old.txt')).should be_true
+ end
+
+ it "should not remove file1 or file2 if file1 and file2 have the same paths" do
+ @doc.image = stub_file('old.jpeg')
+ @doc.textfile = stub_file('old.txt')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.txt')).should be_true
+ end
+ end
+
+ describe '#mount_uploader removing old files with with mount_on' do
+
+ before do
+ @class = reset_mongo_class
+ @uploader1 = Class.new(CarrierWave::Uploader::Base)
+ @class.mount_uploader(:avatar, @uploader1, :mount_on => :another_image)
+ @doc = @class.new
+ @doc.avatar = stub_file('old.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+
+ after do
+ FileUtils.rm_rf(file_path("uploads"))
+ end
+
+ it "should remove old file if old file had a different path" do
+ @doc.avatar = stub_file('new.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/new.jpeg')).should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_false
+ end
+
+ it "should not remove file if old file had the same path" do
+ @doc.avatar = stub_file('old.jpeg')
+ @doc.save.should be_true
+ File.exists?(public_path('uploads/old.jpeg')).should be_true
+ end
+ end
end
View
16 spec/spec_helper.rb
@@ -6,6 +6,13 @@
require 'carrierwave'
require 'carrierwave/mongoid'
+Mongoid.configure do |config|
+ logger = Logger.new('log/test.log')
+ config.logger = logger
+ config.master = Mongo::Connection.new('localhost', 27017,
+ :logger => logger).db('carrierwave_test')
+end
+
def file_path( *paths )
File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
end
@@ -30,12 +37,9 @@ def stub_tempfile(filename, mime_type=nil, fake_name=nil)
t = Tempfile.new(filename)
FileUtils.copy_file(file_path(filename), t.path)
- # This is stupid, but for some reason rspec won't play nice...
- eval <<-EOF
- def t.original_filename; '#{fake_name || filename}'; end
- def t.content_type; '#{mime_type}'; end
- def t.local_path; path; end
- EOF
+ t.stub!(:local_path => "",
+ :original_filename => filename || fake_name,
+ :content_type => mime_type)
return t
end
View
34 spec/storage/grid_fs_spec.rb
@@ -100,9 +100,12 @@
describe "#recreate_versions!" do
before do
@uploader_class = Class.new(CarrierWave::Uploader::Base)
- @uploader_class.class_eval do
+ @uploader_class.class_eval{
+ include CarrierWave::MiniMagick
storage :grid_fs
- end
+
+ process :resize_to_fit => [10, 10]
+ }
@versioned = @uploader_class.new
@versioned.stub!(:grid_fs_connection).and_return(@database)
@@ -122,6 +125,33 @@
@versioned.should be_present
end
end
+
+
+ describe "resize_to_fill" do
+ before do
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
+ @uploader_class.class_eval{
+ include CarrierWave::MiniMagick
+ storage :grid_fs
+ }
+
+ @versioned = @uploader_class.new
+ @versioned.stub!(:grid_fs_connection).and_return(@database)
+
+ @versioned.store! File.open(file_path('portrait.jpg'))
+ end
+
+ after do
+ FileUtils.rm_rf(public_path)
+ end
+
+ it "resizes the file with out error" do
+ lambda {
+ @versioned.resize_to_fill(200, 200)
+ }.should_not raise_error
+
+ end
+ end
end
context "when setting a connection manually" do
Please sign in to comment.
Something went wrong with that request. Please try again.