mirror of
https://github.com/pry/pry-rails.git
synced 2022-11-09 12:36:03 -05:00
Merge pull request #20 from TreyLawrence/master
Add show-routes command
This commit is contained in:
commit
aa8399d0f4
6 changed files with 123 additions and 0 deletions
6
Rakefile
6
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'
|
||||
|
|
29
Readme.md
29
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/).
|
||||
|
|
|
@ -4,3 +4,7 @@ require 'pry'
|
|||
|
||||
require 'pry-rails/version'
|
||||
require 'pry-rails/railtie'
|
||||
require "pry-rails/commands"
|
||||
|
||||
Pry.commands.import PryRails::Commands
|
||||
|
||||
|
|
80
lib/pry-rails/commands.rb
Normal file
80
lib/pry-rails/commands.rb
Normal file
|
@ -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
|
|
@ -24,3 +24,4 @@ module PryRails
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
3
test/routes.rb
Normal file
3
test/routes.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
App::Application.routes.draw do
|
||||
resource :pokemon, :beer
|
||||
end
|
Loading…
Reference in a new issue