Skip to content

Instantly share code, notes, and snippets.

@HotFusionMan
Last active Jul 26, 2016
Embed
What would you like to do?
A useful Rails 4 .irbrc file (for use with Rails-project-specific .irbrc file loading capability from http://railsbros.de/2010/11/29/a-custom-irbrc-for-rails-3.html ). See earlier revisions for Rails 3 version.
def show_controller_action_callbacks
_process_action_callbacks.map(&:filter)
end
alias :action_callbacks :show_controller_action_callbacks
def recognize_path(path, environment = { :method => 'GET', :extras => {} })
(@named_routes ||= Rails.application.routes).recognize_path(path, environment)
end
alias :recognize_route :recognize_path
def path_helper_for(path, environment = { :method => 'GET', :extras => {} })
@map_from_routes_to_route_names ||= Rails.application.routes.named_routes.routes.invert
@request_class ||= Rails.application.routes.request_class
@set ||= Rails.application.routes.set
@router ||= ActionDispatch::Journey::Router.new(@set)
path = ActionDispatch::Journey::Router::Utils.normalize_path(path) unless path =~ %r{://}
begin
env = Rack::MockRequest.env_for(path, { :method => environment[:method] })
rescue URI::InvalidURIError => e
raise ActionController::RoutingError, e.message
end
req = @request_class.new(env)
@router.recognize(req) do |route, _matches, params|
return "#{@map_from_routes_to_route_names[route]}_path"
end
end
if (['test', 'development']).include?(Rails.env)
# From http://www.particlewave.com/2012/07/14/using-factorygirl-with-the-rails-console/ :
def irb_enable_factory_girl
require 'factory_girl_rails'
puts 'Now you can call FactoryGirl.{build|build_stubbed|create} methods in this console session.'
end
end
class Object
def switch_db( env )
config = Rails.configuration.database_configuration
fail ArgumentError, "Invalid environment #{env}" unless config[env]
ActiveRecord::Base.establish_connection( config[env] )
Logger.new( STDOUT ).info( "Successfully changed to #{env} environment." )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment