Skip to content

Instantly share code, notes, and snippets.

@michaeldv
Forked from jugyo/gist:791178
Created January 24, 2011 04:48
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 michaeldv/792850 to your computer and use it in GitHub Desktop.
Save michaeldv/792850 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Usage:
# $ ./script/railssh
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require APP_PATH
Rails.application.require_environment!
module Kernel
alias_method :_exit, :exit
def exit; end
alias_method :_require, :require
def require(name)
if name =~ /^rails\/commands\//
load "#{name}.rb"
else
_require(name)
end
end
end
def routes(controller = nil)
Rails.application.reload_routes!
all_routes = Rails.application.routes.routes
if controller.present?
all_routes = all_routes.select{ |route| route.defaults[:controller] == controller }
end
routes = all_routes.collect do |route|
reqs = route.requirements.dup
reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
reqs = reqs.empty? ? "" : reqs.inspect
{:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
end
routes.reject! { |r| r[:path] =~ %r{/rails/info/properties} } # Skip the route if it's internal info route
name_width = routes.map{ |r| r[:name].length }.max
verb_width = routes.map{ |r| r[:verb].length }.max
path_width = routes.map{ |r| r[:path].length }.max
routes.each do |r|
puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
end
end
require 'readline'
Readline.basic_word_break_characters = ""
Readline.completion_proc = lambda do |word|
['exit', 'routes'].grep(/^#{Regexp.quote(word)}/)
end
while buf = Readline.readline("\e[42mrails>\e[0m ", true)
buf = buf.strip
case buf
when 'exit'
_exit
when /^routes\s*(.*)/
routes $1
else
ARGV.clear
ARGV.concat buf.split(/\s+/)
load 'rails/commands.rb'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment