Skip to content

Instantly share code, notes, and snippets.

@JonKernPA
Created March 13, 2014 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonKernPA/9520920 to your computer and use it in GitHub Desktop.
Save JonKernPA/9520920 to your computer and use it in GitHub Desktop.
Dragonfly and MongoMapper and Heroku
require 'dragonfly'
require 'dragonfly/mongo_data_store'
require 'mongo'
require 'uri'
# From https://devcenter.heroku.com/articles/mongohq#use-with-ruby
def get_connection
return @db_connection if @db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
@db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
@db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
@db_connection
end
# Configure
Dragonfly.app.configure do
plugin :imagemagick
protect_from_dos_attacks true
secret "cbbbf...3ab879"
url_format "/media/:job/:name"
# Added this bit to tweak the dragonfly mongo options to point to MongoHQ
if ENV['MONGOHQ_URL']
uri = URI.parse(ENV['MONGOHQ_URL'])
name = uri.path.gsub(/^\//, '')
datastore :mongo,
db: get_connection,
root_path: Rails.root.join('public/system/dragonfly', Rails.env),
server_root: Rails.root.join('public')
else
datastore :mongo,
db: MongoMapper.database,
root_path: Rails.root.join('public/system/dragonfly', Rails.env),
server_root: Rails.root.join('public')
end
end
# Logger
Dragonfly.logger = Rails.logger
# Mount as middleware
Rails.application.middleware.use Dragonfly::Middleware
# Add model functionality
if defined?(ActiveRecord::Base)
ActiveRecord::Base.extend Dragonfly::Model
ActiveRecord::Base.extend Dragonfly::Model::Validations
end
@JonKernPA
Copy link
Author

See also a mongoid version: https://gist.github.com/rdetert/5538121

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