Skip to content

Instantly share code, notes, and snippets.

@markevans
Created March 14, 2012 12:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markevans/2036284 to your computer and use it in GitHub Desktop.
Save markevans/2036284 to your computer and use it in GitHub Desktop.
Ideas for changing dragonfly configuration API
### EXAMPLE: USING DRAGONFLY WITH S3 ON HEROKU ###
#### CURRENT API #####
# in initializer file
require 'dragonfly'
app = Dragonfly[:images]
app.configure_with(:imagemagick)
app.configure_with(:rails)
app.configure do |c|
if Rails.env.production?
c.datastore = Dragonfly::DataStorage::S3DataStore.new(
:bucket_name => 'my-bucket-name',
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
)
end
c.some_other_option = 'blah'
end
app.define_macro(ActiveRecord::Base, :image_accessor)
# in Model:
class MyModel < ActiveRecord::Base
image_accessor :image
end
#######################################################################
#### NEW API #####
# in initializer file:
require 'dragonfly'
Dragonfly[:app].configure do
use :imagemagick
use :rails
datastore :s3 do
bucket_name 'my-bucket-name'
access_key_id ENV['S3_KEY']
secret_access_key ENV['S3_SECRET']
end if Rails.env.production?
some_other_option 'blah'
end
# in Model:
class MyModel < ActiveRecord::Base
dragonfly_accessor :image
end
@markevans
Copy link
Author

Note: there would be a default dragonfly app Dragonfly[:app]
If you wanted another dragonfly app, e.g. Dragonfly[:videos], you would configure it as usual but have to specify it when declaring the accessor, i.e.

dragonfly_accessor :video, :app => :videos

@shingara
Copy link

I love the new configuration api.

About the dragonfly_accessor, that can help to avoid thinking that dragonfly can manage only image.

About the switching app directly on the dragonfly_accessor. It can be good choice. Because chaging app its hard actually.

👍 on this new api

@tanraya
Copy link

tanraya commented Mar 14, 2012

New API looks great! But I think this would cleaner:

datastore :s3, :env => [:production, :staging] do
  bucket_name 'my-bucket-name'
  access_key_id ENV['S3_KEY']
  secret_access_key ENV['S3_SECRET']
end

what do you think?

@markevans
Copy link
Author

Nice idea, though I'm not so keen on that because then it depends on Rails (at the moment it would work anywhere)
I guess I could use RACK_ENV or something, but still I'd rather not complicate the "datastore" configuration method - it should simply configure the datastore, and any conditions should go outside
what do you reckon?

@shingara
Copy link

The env option can't work because in rack application it's define by RACK_ENV but RAILS_ENV in rails.

@markevans
Copy link
Author

@shingara - yes exactly I agree - avoiding thinking it's only for images is a benefit

@tanraya
Copy link

tanraya commented Mar 14, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment