--- layout: page title: Using with Rails ---
Rails::Initializer.run do |config| . . . config.after_initialize do require "vanity" end endIf you have a @config/vanity.yml@ file, Vanity will read the configuration for the current environment. For example:
staging: adapter: redis host: staging.internal production: adapter: mongo host: live.internal database: vanityIf you want to use Google Analytics, you must also tell Rails to include the @garb@ gem, and login for a new session. You'll want to do that for production, not for development if you like developing offline:
config.after_initialize do require "garb" Garb::Session.login('..ga email..', '..ga pwd..', account_type: "GOOGLE") endThere's generally no need to collect metric and experiment data outside production environment. Under Rails, Vanity turns collection on only if the environment name is "production". You can control this from @config/environments@ by setting @Vanity.playground.collecting@ to true/false. When collection is off, Vanity doesn't connect to the database server, so there's no need to set a database configuration for these environments. h3(#dashboard). Dashboard Start by adding a new resource in @config/routes.rb@:
map.vanity "/vanity/:action/:id", :controller=>:vanityCreate a new controller for Vanity:
class VanityController < ApplicationController include Vanity::Rails::Dashboard endNow open your browser to "http://localhost:3000/vanity":http://localhost:3000/vanity. The Dashboard renders complete HTML pages with CSS and all necessary JavaScript libraries. Thankfully, HTML is forgiving enough that it will render correctly even with your existing application layout. You can decide to keep your layout, or tell the controller to set @layout false@. h3(#fork). Unicorn and Forking Servers Unicorn forks the master process to create worker processes efficiently. Since the master processes opens a connection to the database, all workers end up sharing that connection, resulting in ugly contention issues. The cure is simple, use the @after_fork@ hook to reconnect each worker process. Here's the relevant part from my @config/unicorn.rb@:
after_fork do |server, worker| ActiveRecord::Base.establish_connection Vanity.playground.establish_connection endYou'll run into this issue with other forking servers. Vanity can detect when it runs under Passenger and automatically reconnect each forked process.