Created
May 8, 2013 04:07
-
-
Save rdetert/5538121 to your computer and use it in GitHub Desktop.
How to get Dragonfly working on with a Mongo Datastore using Mongoid 3 on both localhost and Heroku. It's a little bit of a pain and here is the quick and dirty approach without Monkey Patching.
This file contains hidden or 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' | |
app = Dragonfly[:images] | |
mconfig = Mongoid.load!("config/mongoid.yml", Rails.env) | |
app.datastore = Dragonfly::DataStorage::MongoDataStore.new | |
app.datastore.configure do |c| | |
cfg = mconfig['sessions']['default'] | |
if ENV['MONGOLAB_URI'] | |
# Painful support of Heroku | |
uri = cfg['uri'] # => "mongodb://appname:[email protected]:29217/database_name" | |
uri = uri.gsub('mongodb://', '') | |
user_pass, host_string = uri.split('@') | |
username, password = user_pass.split(':') | |
host_port, database = host_string.split('/') | |
host, port = host_port.split(':') | |
c.host = host | |
c.port = port | |
c.database = database | |
c.username = username | |
c.password = password | |
else | |
if cfg['hosts'].size < 2 | |
c.host = cfg['hosts'].first.split(':').first | |
c.port = cfg['hosts'].first.split(':').second | |
else | |
c.hosts = cfg['hosts'] | |
end | |
c.database = cfg['database'] | |
c.username = cfg['username'] | |
c.password = cfg['password'] | |
end | |
end | |
app.configure_with(:imagemagick) | |
app.configure_with(:rails) | |
# app.configure do |c| | |
# c.url_format = '/:job' | |
# end | |
# Allow all mongoid models to use the macro 'image_accessor' | |
app.define_macro_on_include(Mongoid::Document, :image_accessor) |
This file contains hidden or 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
<% if ENV['MONGOLAB_URI'] %> | |
production: | |
sessions: | |
default: | |
uri: <%= ENV['MONGOLAB_URI'] %> | |
options: | |
skip_version_check: true | |
safe: true | |
<% else %> | |
production: | |
sessions: | |
default: | |
database: my_production | |
hosts: | |
- localhost:27017 | |
options: | |
skip_version_check: true | |
safe: true | |
<% end %> | |
<% if ENV['MONGOLAB_URI'] %> | |
development: | |
sessions: | |
default: | |
uri: mongodb://appname:[email protected]:29217/database_name | |
options: | |
skip_version_check: true | |
safe: true | |
<% else %> | |
development: | |
# Configure available database sessions. (required) | |
sessions: | |
# Defines the default session. (required) | |
default: | |
# Defines the name of the default database that Mongoid can connect to. | |
# (required). | |
database: my_development | |
# Provides the hosts the default session can connect to. Must be an array | |
# of host:port pairs. (required) | |
hosts: | |
- localhost:27017 | |
options: | |
# Change whether the session persists in safe mode by default. | |
# (default: false) | |
# safe: false | |
# Change the default consistency model to :eventual or :strong. | |
# :eventual will send reads to secondaries, :strong sends everything | |
# to master. (default: :eventual) | |
# consistency: :eventual | |
# How many times Moped should attempt to retry an operation after | |
# failure. (default: 30) | |
# max_retries: 30 | |
# The time in seconds that Moped should wait before retrying an | |
# operation on failure. (default: 1) | |
# retry_interval: 1 | |
# Configure Mongoid specific options. (optional) | |
options: | |
# Configuration for whether or not to allow access to fields that do | |
# not have a field definition on the model. (default: true) | |
# allow_dynamic_fields: true | |
# Enable the identity map, needed for eager loading. (default: false) | |
# identity_map_enabled: false | |
# Includes the root model name in json serialization. (default: false) | |
# include_root_in_json: false | |
# Include the _type field in serializaion. (default: false) | |
# include_type_for_serialization: false | |
# Preload all models in development, needed when models use | |
# inheritance. (default: false) | |
# preload_models: false | |
# Protect id and type from mass assignment. (default: true) | |
# protect_sensitive_fields: true | |
# Raise an error when performing a #find and the document is not found. | |
# (default: true) | |
# raise_not_found_error: true | |
# Raise an error when defining a scope with the same name as an | |
# existing method. (default: false) | |
# scope_overwrite_exception: false | |
# Skip the database version check, used when connecting to a db without | |
# admin access. (default: false) | |
# skip_version_check: false | |
# User Active Support's time zone in conversions. (default: true) | |
# use_activesupport_time_zone: true | |
# Ensure all times are UTC in the app side. (default: false) | |
# use_utc: false | |
<% end %> | |
test: | |
sessions: | |
default: | |
database: my_test | |
hosts: | |
- localhost:27017 | |
options: | |
consistency: :strong | |
# In the test environment we lower the retries and retry interval to | |
# low amounts for fast failures. | |
max_retries: 1 | |
retry_interval: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have linked my dragonfly/mongo_mapper config for heroku to your gist.
Here is my gist: https://gist.github.com/JonKernPA/9520920