Skip to content

Commit

Permalink
Merge pull request #2 from skyeagle/master
Browse files Browse the repository at this point in the history
Consistency with mongoid ~> 2.1 and fully extracted from carrierwave(0.5.6)
  • Loading branch information
jnicklas committed Aug 12, 2011
2 parents 1bd92e2 + 74c29ca commit c093bd3
Show file tree
Hide file tree
Showing 13 changed files with 510 additions and 100 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -3,4 +3,5 @@ pkg/*
.bundle
spec/public
.rvmrc
Gemfile.lock
Gemfile.lock
log/*
68 changes: 68 additions & 0 deletions 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)
53 changes: 0 additions & 53 deletions README.rdoc

This file was deleted.

11 changes: 6 additions & 5 deletions carrierwave-mongoid.gemspec
Expand Up @@ -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
32 changes: 27 additions & 5 deletions lib/carrierwave/mongoid.rb
@@ -1,6 +1,7 @@
# encoding: utf-8

require 'mongoid'
require 'carrierwave'
require 'carrierwave/validations/active_model'

module CarrierWave
Expand All @@ -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
Expand Down
Empty file added log/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions 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.
1 change: 1 addition & 0 deletions 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.
1 change: 1 addition & 0 deletions 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.
1 change: 1 addition & 0 deletions 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.

0 comments on commit c093bd3

Please sign in to comment.