diff --git a/Rakefile b/Rakefile index c2c9f6f..f9aba20 100644 --- a/Rakefile +++ b/Rakefile @@ -7,6 +7,12 @@ desc 'Create test Rails app' task :init_test_app do `rm -rf test/app >/dev/null 2>&1` `env BUNDLE_GEMFILE=gemfiles/rails30.gemfile bundle exec rails new test/app` + FileUtils.cp("test/routes.rb", "test/app/config/routes.rb") + File.open("test/app/Gemfile", 'a+') { |f| f.write(%Q{gem "pry-rails", :path => "../../"}) } + FileUtils.cd("test/app") + `env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Pokemon name:string caught:binary species:string abilities:string` + `env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Hacker social_ability:integer` + `env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Beer name:string type:string rating:integer ibu:integer abv:integer` end desc 'Start the Rails server' diff --git a/Readme.md b/Readme.md index 3cb353a..ba27dfe 100644 --- a/Readme.md +++ b/Readme.md @@ -15,6 +15,35 @@ Add this line to your gemfile: `bundle install` and enjoy pry. +# Usage + +``` +$ rails console +[1] pry(main)> show-routes + pokemon POST /pokemon(.:format) pokemons#create + new_pokemon GET /pokemon/new(.:format) pokemons#new +edit_pokemon GET /pokemon/edit(.:format) pokemons#edit + GET /pokemon(.:format) pokemons#show + PUT /pokemon(.:format) pokemons#update + DELETE /pokemon(.:format) pokemons#destroy + beer POST /beer(.:format) beers#create + new_beer GET /beer/new(.:format) beers#new + edit_beer GET /beer/edit(.:format) beers#edit + GET /beer(.:format) beers#show + PUT /beer(.:format) beers#update + DELETE /beer(.:format) beers#destroy +[2] pry(main)> show-routes --grep beer + beer POST /beer(.:format) beers#create + new_beer GET /beer/new(.:format) beers#new + edit_beer GET /beer/edit(.:format) beers#edit + GET /beer(.:format) beers#show + PUT /beer(.:format) beers#update + DELETE /beer(.:format) beers#destroy +[3] pry(main)> show-routes --grep new + new_pokemon GET /pokemon/new(.:format) pokemons#new + new_beer GET /beer/new(.:format) beers#new +``` + # Alternative If you want to enable pry everywhere, make sure to check out [pry everywhere](http://lucapette.com/pry/pry-everywhere/). diff --git a/lib/pry-rails.rb b/lib/pry-rails.rb index e709355..0dce141 100644 --- a/lib/pry-rails.rb +++ b/lib/pry-rails.rb @@ -4,3 +4,7 @@ require 'pry' require 'pry-rails/version' require 'pry-rails/railtie' +require "pry-rails/commands" + +Pry.commands.import PryRails::Commands + diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb new file mode 100644 index 0000000..ead1954 --- /dev/null +++ b/lib/pry-rails/commands.rb @@ -0,0 +1,80 @@ +module PryRails + Commands = Pry::CommandSet.new do + create_command "show-routes", "Print out all defined routes in match order, with names." do + def options(opt) + opt.banner unindent <<-USAGE + Usage: show-routes [-G] + + show-routes displays the current Rails app's routes. + USAGE + + opt.on :G, "grep", "Filter output by regular expression", :argument => true + end + + def process + Rails.application.reload_routes! + all_routes = Rails.application.routes.routes + + all_routes = begin + begin + # rails 4 + require 'action_dispatch/routing/inspector' + inspector = ActionDispatch::Routing::RoutesInspector.new + rescue LoadError => e + # rails 3.2 + require 'rails/application/route_inspector' + inspector = Rails::Application::RouteInspector.new + end + inspector.format(all_routes) + rescue LoadError => e + # rails 3.0 and 3.1. cribbed from + # https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake + 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 + + # Skip the route if it's internal info route + routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} } + + 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.map do |r| + "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}" + end + end + + output.puts all_routes.grep(Regexp.new(opts[:G] || ".")).join "\n" + end + end + + create_command "show-models", "Print out all defined models, with attribrutes." do + def options(opt) + opt.banner unindent <<-USAGE + Usage: show-models + + show-models displays the current Rails app's models. + USAGE + + opt.on :G, "grep", "Filter output by regular expression", :argument => true + end + + def process + Rails.application.eager_load! + models = ActiveRecord::Base.descendants.map do |mod| + mod.to_s + "\n" + mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n") + end.join("\n") + + models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } + + output.puts models + end + end + end +end \ No newline at end of file diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index 6a32735..ff2ba5c 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -24,3 +24,4 @@ module PryRails end end end + diff --git a/test/routes.rb b/test/routes.rb new file mode 100644 index 0000000..73c4017 --- /dev/null +++ b/test/routes.rb @@ -0,0 +1,3 @@ +App::Application.routes.draw do + resource :pokemon, :beer +end