Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Add support for Mongoid in Dragonfly
Ruby
Branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
rails initial commit
tasks
test
.gitignore
MIT-LICENSE
README.textile
Rakefile
init.rb
install.rb
uninstall.rb

README.textile

DragonflyMongoidExtensions

Install

This plugin makes Dragonfly works with Mongoid like it works with ActiveRecord

To install just do:

script/plugin install git://github.com/wilkerlucio/dragonfly_mongoid_extensions.git

Then you will need to configure, to do it just generate dragonfly initializer like you do with ActiveRecord:

script/generate dragonfly_app images

Open this file and edit the integration, the default should be something like this:

# Extend ActiveRecord
# This allows you to use e.g.
#   other_accessor :my_attribute
# in your models.
ActiveRecord::Base.extend Dragonfly::ActiveRecordExtensions
ActiveRecord::Base.register_dragonfly_app(:image, Dragonfly::App[:images])

You should replace these lines with:

# Plug Mongoid Extensions
Mongoid::Document::InstanceMethods.module_eval do
  def self.included(base)
    base.extend Dragonfly::MongoidExtensions::ClassMethods
    base.send :include, Dragonfly::MongoidExtensions::InstanceMethods
    base.register_dragonfly_app :image, Dragonfly::App[:images]
  end
end

Them you are ready to go!

Example model

A sample model for you:

class User
  include Mongoid::Document
  
  field :name
  field :email
  
  image_accessor :avatar
end

Notice you dont need to declare avatar_uid field, the extension will do it automatic for you.

Removing uploads

For helping your life we also provides an extra field to remove the image.

<% form_for @user, :html => {:multipart => true} do |f| %>
  <p>
    <label>My Avatar</label>
    <%= f.file_field :avatar %>
  </p>

  <p>
    <label>
      <%= f.check_box :remove_avatar %>
      Remove avatar
    </label>
  </p>
<% end %>

Simple like this.

Using GridFS as Data Storage

We like Mongo, so, lets use it to store the content too :)
To configure GridFS with mongoid is really simple, lets take a look at relevant part in documentation for it:

File.open(File.join(RAILS_ROOT, 'config/database.mongo.yml'), 'r') do |f|
  @settings = YAML.load(f)[RAILS_ENV]
end

app = Dragonfly::App[:images]
app.configure_with(Dragonfly::RMagickConfiguration)
app.datastore = Dragonfly::DataStorage::MongoGridFsStore.new @settings["host"], @settings["database"]
app.defaults_path = File.join(RAILS_ROOT, %w[public images])
app.configure do |c|
  c.log = Rails.logger
  c.url_handler.configure do |u|
    u.secret = 'ce61f1fdfef4b467a9b559d37c6bd28c9453ff08'
    u.path_prefix = '/media'
  end
end

Its just that, if you configured Mongoid like the manual says, it will just works :)

Default image support

If you use my branch of dragonfly you can use the support for
default images. In order to do it you first need to configure initializers to setup the default images path
(if you used my configuration above, its already set). Them just do in your model:

class User
  image_accessor :avatar, :default => "image.png"
end

So, this will use the image.png for users that don’t send an avatar. Sure you can use it as a regular uploaded
file (can do any image modifications on fly).

Something went wrong with that request. Please try again.