Code Monkey home page Code Monkey logo

carrierwave-mongoid's Introduction

CarrierWave for Mongoid

This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave

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

Require it in your code:

require 'carrierwave/mongoid'

Or, in Rails you can add it to your Gemfile:

gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'

Getting Started

Follow the "Getting Started" directions in the main Carrierwave repository.

[Suggested] Add the field to your attr_accessor list for mass assignment protection:

attr_accessible :avatar, :avatar_cache

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

You can configure Carrierwave to use GridFS instead of the filesystem. For example:

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:

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 optionally 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

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:

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.x

carrierwave-mongoid ~> 0.2.0 is only compatible with Rails 3.2 or higher.

0.1.x

carrierwave-mongoid ~> 0.1.1 depends on carrierwave ~> 0.5.7. This version of carrierwave is only compatible with Rails 3.1 or earlier.

Changes from earlier versions of CarrierWave <= 0.5.6

CarrierWave used to have built-in Mongoid support. This gem replaces that support and only only supports Mongoid ~> 2.1

You can use upload_identifier to retrieve the original name of the uploaded file.

In the earlier version, the mount_uploader-method for mongoid had been defined in lib/carrierwave/orm/mongoid. This code has been moved to carrierwave/mongoid. If you update from earlier versions, don't forget to adjust your require accordingly in your carrierwave-initializer.

The default mount column used to be the name of the upload column plus _filename. Now it is simply the name of the column. Most of the time, the column was called upload, so it would have been mounted to upload_filename. If you'd like to avoid a database migration, simply use the :mount_on option to specify the field name explicitly. Therefore, you only have to add a _filename to your column name. For example, if your column is called :upload:

class Dokument
  mount_uploader :upload, DokumentUploader, mount_on: :upload_filename
end

Known issues and limitations

Note that files mounted in embedded documents aren't saved when parent documents are saved. By default, mongoid does not cascade callbacks on embedded documents. In order to save the attached files on embedded documents, you must either explicitly call save on the embedded documents or you must configure the embedded association to cascade the callbacks automatically. For example:

class User
  embeds_many :pictures, cascade_callbacks: true
end

You can read more about this here

carrierwave-mongoid's People

Contributors

rmm5t avatar trevorturk avatar svperfecta avatar bensie avatar fwoeck avatar jnicklas avatar nickhoffman avatar piotrj avatar escobera avatar romanbsd avatar spiridonov avatar agerlic avatar skyeagle avatar huacnlee avatar coderifous avatar mikdiet avatar mugwump avatar

Watchers

Eduardo avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.