README.md in carrierwave-mongoid-0.2.2 vs README.md in carrierwave-mongoid-0.3.0

- old
+ new

@@ -1,57 +1,80 @@ # CarrierWave for Mongoid -This gem adds support for Mongoid and MongoDB's GridFS to [CarrierWave](https://github.com/jnicklas/carrierwave/) +This gem adds support for Mongoid and MongoDB's GridFS to +[CarrierWave](https://github.com/jnicklas/carrierwave/) -This functionality used to be part of CarrierWave but has since been extracted into this gem. +This functionality used to be part of CarrierWave but has since been extracted +into this gem. ## Installation +Install the latest release: + gem install carrierwave-mongoid -## Requiring the gem +Require it in your code: require 'carrierwave/mongoid' -## Using Bundler +Or, in Rails you can add it to your Gemfile: gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid' -## Using MongoDB's GridFS store +## Getting Started -You'll need to configure the database and host to use: +Follow the "Getting Started" directions in the main +[Carrierwave repository](https://raw.github.com/jnicklas/carrierwave/). -```ruby -CarrierWave.configure do |config| - config.grid_fs_database = 'my_mongo_database' - config.grid_fs_host = 'mongo.example.com' -end -``` +[Suggested] Add the field to your attr_accessor list for mass assignment +protection: -The defaults are `carrierwave` and `localhost`. + attr_accessible :avatar, :avatar_cache -And then in your uploader, set the storage to `:grid_fs`: +Now you can cache files by assigning them to the attribute; they will +automatically be stored when the record is saved. Ex: + u = User.new + u.avatar = File.open('somewhere') + u.save! + +## Using MongoDB's GridFS store + +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: +can optionally 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" + config.grid_fs_access_url = "/systems/uploads" end ``` +Bringing it all together, you can also configure Carrierwave to use Mongoid's +database connection and default all storage to GridFS. That might look something +like this: + +```ruby +CarrierWave.configure do |config| + config.grid_fs_connection = Mongoid.database + config.storage = :grid_fs + config.root = Rails.root.join('tmp') + config.cache_dir = "uploads" +end +``` + ## Version differences -### 0.2.0 +### 0.2.x carrierwave-mongoid ~> 0.2.0 is only compatible with Rails 3.2 or higher. ### 0.1.x