Created
March 13, 2014 02:31
-
-
Save JonKernPA/9520920 to your computer and use it in GitHub Desktop.
Dragonfly and MongoMapper and Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also a mongoid version: https://gist.github.com/rdetert/5538121