Skip to content

Dragonfly gem extension that lets you add paperclip compatibility

License

Notifications You must be signed in to change notification settings

sinuso/dragonfly-rails

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

About dragonfly-rails

It’s a rails engine that loads dragonfly gem, adding scope and id_partion storing style. It allows paperclip users to migrate to this solution when more image size flexibility is required, without losing paperclip’s file storing structure.

Dragonfly stores files using the current time, but for paperclip you can make Dragonfly to save files at /:dragonfly_scope/:id_partition

class User < ActiveRecord::Base
  dragonfly_for :avatar_file, :scope => 'avatars'
end

User avatar files will be stored as /avatars/000/000/001/filename_original.jpg

class Image < ActiveRecord::Base
  dragonfly_for :file # scope defaults to 'images'
end

Image files will be stored as /images/000/000/001/filename_original.jpg

Basic configuration

Rails Gemfile dependencies

# Plugin based on the edge dragonfly code
gem 'dragonfly', :git => "https://github.com/markevans/dragonfly.git"
gem 'dragonfly-rails', :require => 'dragonfly_rails', :git => 'https://github.com/ritxi/dragonfly-rails.git'

App configuration (config/application.rb)

config.dragonfly_rails.security_key = 'mysecretkey'
config.dragonfly_rails.protect_from_dos_attacks = true
config.dragonfly_rails.route_path = 'media' # /media

# Default is Rails.root.join('public','assets'), this will only be used on development environment.
# On production environment is expected to use amazon and heroku.
config.dragonfly_rails.assets_path = 'local/path/where/assets/should/be/stored'

Model setup

Is Default Images scope good for you?

class Image < ActiveRecord::Base
  dragonfly_for :file # scope defaults to 'images'
end

# Add file_uid field to your images table.
create_table "images", :force => true do |t|
  ...
  t.column "file_uid", :string
end

Prefere custom scope?

class User < ActiveRecord::Base
  dragonfly_for :avatar_file, :scope => 'avatars'
end

# Add the following migration to your images migration file or add a new migration adding the avatar_file_uid field
create_table "users", :force => true do |t|
  ...
  t.column "avatar_file_uid", :string
end

Using both Paperclip and Dragonfly

If you want to continue to use Paperclip, but retain the ability to use Dragonfly, you can pass the :with_paperclip option so Paperclip will be the master accessor and Dragonfly _uid field will be updated when Paperclip fields are updated.

class User < ActiveRecord::Base
  dragonfly_for :avatar_file, :scope => 'avatars', :with_paperclip => :pc_avatar
  has_attached_file :pc_avatar,
                    {:styles => {
                      :thumb => '50x50>',
                      :original => '1200x1200>'},
                    :default_style => :thumb,
                   :path => ":root_path/paperclips/:id_partition/:basename_:style.:extension",
                   :url => "/:environment/paperclips/:id_partition/:basename_:style.:extension"}
end

On your forms file_field :pc_avatar must be used:

<% form_for :user, :html => { :multipart => true } do |form| %>
  <%= form.file_field :pc_avatar %>
<% end %>

Paperclip migration

Create a rake task with / Run the following code

User.each do |u|
  u.create_dragonfly_uid(:avatar_file_uid, :paper_clip_accessor)
end

About

Dragonfly gem extension that lets you add paperclip compatibility

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%