Permalink
Browse files

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

…0.5.6)
  • Loading branch information...
1 parent 1bd92e2 commit 74c29cab6961ece91cbd702e3e04d892ddc57091 @skyeagle skyeagle committed Aug 1, 2011
View
@@ -3,4 +3,5 @@ pkg/*
.bundle
spec/public
.rvmrc
-Gemfile.lock
+Gemfile.lock
+log/*
View
@@ -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
@@ -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]
-
@@ -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
@@ -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
No changes.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -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.
Oops, something went wrong.

0 comments on commit 74c29ca

Please sign in to comment.