From 8bac1d3602a323c5fde664428e67ef0ddec1689b Mon Sep 17 00:00:00 2001 From: Trey Lawrence Date: Tue, 10 Jul 2012 16:47:00 -0400 Subject: [PATCH 01/13] Add show-routes --- lib/pry-rails/railtie.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index 6a32735..0838bff 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -23,4 +23,17 @@ module PryRails end end end + + Commands = Pry::CommandSet.new do + create_command "show-routes", "Print out all defined routes in match order, with names." do + def process + all_routes = Rails.application.routes.routes + require 'rails/application/route_inspector' + inspector = Rails::Application::RouteInspector.new + output.puts inspector.format(all_routes).join "\n" + end + end + end end + +Pry.commands.import PryGit::Commands \ No newline at end of file From 8537ce1de963b23cf0106c4108099af1ad70528e Mon Sep 17 00:00:00 2001 From: Trey Lawrence Date: Tue, 10 Jul 2012 17:28:33 -0400 Subject: [PATCH 02/13] Fix import commands --- lib/pry-rails/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index 0838bff..a9fa77f 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -36,4 +36,4 @@ module PryRails end end -Pry.commands.import PryGit::Commands \ No newline at end of file +Pry.commands.import PryRails::Commands \ No newline at end of file From 9f4a7da49b41d9d75e52cc1bd2d9802cb4ea2c4f Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 13:19:19 -0400 Subject: [PATCH 03/13] Show-routes command can grep displayed routes. --- lib/pry-rails/railtie.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index a9fa77f..9df815b 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -26,14 +26,18 @@ module PryRails Commands = Pry::CommandSet.new do create_command "show-routes", "Print out all defined routes in match order, with names." do - def process - all_routes = Rails.application.routes.routes - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new - output.puts inspector.format(all_routes).join "\n" - end + def options(opt) + opt.on :G, "grep", "Filter output by regular expression", :argument => true + end + + def process + all_routes = Rails.application.routes.routes + require 'rails/application/route_inspector' + inspector = Rails::Application::RouteInspector.new + output.puts inspector.format(all_routes).grep(Regexp.new(opts[:G] || ".")).join "\n" + end end end end -Pry.commands.import PryRails::Commands \ No newline at end of file +Pry.commands.import PryRails::Commands From ce3841e3cca5aa9d130dbe2b371549ddd36ef411 Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 14:25:06 -0400 Subject: [PATCH 04/13] Add usage section to README. --- Readme.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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/). From 3918dff8bd1eec34974b0a112e544e17f1e03c62 Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 14:35:35 -0400 Subject: [PATCH 05/13] Add USAGE banner to show-routes. --- lib/pry-rails/railtie.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index 9df815b..24530fb 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -27,6 +27,12 @@ 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 From 49cfb3877ea7690be34cae2b2738ed80f9501b08 Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 16:20:37 -0400 Subject: [PATCH 06/13] Copy over routes.rb with our own routes.rb --- Rakefile | 1 + test/routes.rb | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 test/routes.rb diff --git a/Rakefile b/Rakefile index c2c9f6f..3598ef4 100644 --- a/Rakefile +++ b/Rakefile @@ -7,6 +7,7 @@ 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") end desc 'Start the Rails server' 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 From 49a63c2b9a335be7e1fb41b2b071948297dd99cd Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 17:32:56 -0400 Subject: [PATCH 07/13] Separate commands into own file. --- lib/pry-rails.rb | 4 ++++ lib/pry-rails/commands.rb | 23 +++++++++++++++++++++++ lib/pry-rails/railtie.rb | 22 ---------------------- 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 lib/pry-rails/commands.rb 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..586c920 --- /dev/null +++ b/lib/pry-rails/commands.rb @@ -0,0 +1,23 @@ +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 + all_routes = Rails.application.routes.routes + require 'rails/application/route_inspector' + inspector = Rails::Application::RouteInspector.new + output.puts inspector.format(all_routes).grep(Regexp.new(opts[:G] || ".")).join "\n" + end + end + end +end + diff --git a/lib/pry-rails/railtie.rb b/lib/pry-rails/railtie.rb index 24530fb..ff2ba5c 100644 --- a/lib/pry-rails/railtie.rb +++ b/lib/pry-rails/railtie.rb @@ -23,27 +23,5 @@ module PryRails end end end - - 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 - all_routes = Rails.application.routes.routes - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new - output.puts inspector.format(all_routes).grep(Regexp.new(opts[:G] || ".")).join "\n" - end - end - end end -Pry.commands.import PryRails::Commands From 1f92bb88c1e59b8cb0e2d247a59b55a22d312892 Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 18:20:23 -0400 Subject: [PATCH 08/13] Add support for earlier versions of Rails. --- lib/pry-rails/commands.rb | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb index 586c920..6e41081 100644 --- a/lib/pry-rails/commands.rb +++ b/lib/pry-rails/commands.rb @@ -12,10 +12,38 @@ module PryRails end def process + Rails.application.reload_routes! all_routes = Rails.application.routes.routes - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new - output.puts inspector.format(all_routes).grep(Regexp.new(opts[:G] || ".")).join "\n" + + # cribbed from + # https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake + all_routes = begin + require 'rails/application/route_inspector' + inspector = Rails::Application::RouteInspector.new + inspector.format(all_routes) + rescue LoadError => e + 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 end From ac8fb6ce2a05e5c8cf23119d830e20b2172e900f Mon Sep 17 00:00:00 2001 From: David Peter Date: Fri, 13 Jul 2012 18:20:48 -0400 Subject: [PATCH 09/13] Make it easier for developers to develop pry-rails --- Rakefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Rakefile b/Rakefile index 3598ef4..c8a5bbf 100644 --- a/Rakefile +++ b/Rakefile @@ -8,6 +8,7 @@ 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 => "../../"}) } end desc 'Start the Rails server' From abac30c9feaba622766a34a58c367bcc28b6a3ea Mon Sep 17 00:00:00 2001 From: David Peter Date: Sat, 14 Jul 2012 07:33:54 -0400 Subject: [PATCH 10/13] Add rails 4 support for show-routes. --- lib/pry-rails/commands.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb index 6e41081..6a718a9 100644 --- a/lib/pry-rails/commands.rb +++ b/lib/pry-rails/commands.rb @@ -15,13 +15,20 @@ module PryRails Rails.application.reload_routes! all_routes = Rails.application.routes.routes - # cribbed from - # https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake all_routes = begin - require 'rails/application/route_inspector' - inspector = Rails::Application::RouteInspector.new + 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 From 93a7dc69999b922593c2d2f3af67855bd4fc6081 Mon Sep 17 00:00:00 2001 From: Trey Lawrence Date: Mon, 16 Jul 2012 01:14:23 -0400 Subject: [PATCH 11/13] Add show-routes command --- lib/pry-rails/commands.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb index 6a718a9..5a0391e 100644 --- a/lib/pry-rails/commands.rb +++ b/lib/pry-rails/commands.rb @@ -53,6 +53,24 @@ module PryRails output.puts all_routes.grep(Regexp.new(opts[:G] || ".")).join "\n" end end - 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 + 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") + + output.puts models + end + end + end +end \ No newline at end of file From 78113e6a322e3fa1fea3ae973d7df11d08dd70e3 Mon Sep 17 00:00:00 2001 From: Trey Lawrence Date: Mon, 16 Jul 2012 01:15:12 -0400 Subject: [PATCH 12/13] Add 3 test models to init_test_app rake task --- Rakefile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Rakefile b/Rakefile index c8a5bbf..f9aba20 100644 --- a/Rakefile +++ b/Rakefile @@ -9,6 +9,10 @@ task :init_test_app do `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' From b9736ea4f78475de39b6c28fe3ceace2f39446fd Mon Sep 17 00:00:00 2001 From: Trey Lawrence Date: Mon, 16 Jul 2012 01:44:49 -0400 Subject: [PATCH 13/13] Add grep option with coloring --- lib/pry-rails/commands.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/pry-rails/commands.rb b/lib/pry-rails/commands.rb index 5a0391e..ead1954 100644 --- a/lib/pry-rails/commands.rb +++ b/lib/pry-rails/commands.rb @@ -61,6 +61,8 @@ module PryRails show-models displays the current Rails app's models. USAGE + + opt.on :G, "grep", "Filter output by regular expression", :argument => true end def process @@ -69,6 +71,8 @@ module PryRails 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