-
-
Save michaeldv/792850 to your computer and use it in GitHub Desktop.
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
#!/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