super-smart-box 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/pry-rails-0.3.11/Gemfile +4 -0
  3. data/pry-rails-0.3.11/LICENCE +20 -0
  4. data/pry-rails-0.3.11/Rakefile +44 -0
  5. data/pry-rails-0.3.11/Readme.md +136 -0
  6. data/pry-rails-0.3.11/lib/pry-rails/commands/find_route.rb +84 -0
  7. data/pry-rails-0.3.11/lib/pry-rails/commands/recognize_path.rb +35 -0
  8. data/pry-rails-0.3.11/lib/pry-rails/commands/show_middleware.rb +71 -0
  9. data/pry-rails-0.3.11/lib/pry-rails/commands/show_model.rb +44 -0
  10. data/pry-rails-0.3.11/lib/pry-rails/commands/show_models.rb +84 -0
  11. data/pry-rails-0.3.11/lib/pry-rails/commands/show_routes.rb +92 -0
  12. data/pry-rails-0.3.11/lib/pry-rails/commands.rb +11 -0
  13. data/pry-rails-0.3.11/lib/pry-rails/console.rb +7 -0
  14. data/pry-rails-0.3.11/lib/pry-rails/model_formatter.rb +111 -0
  15. data/pry-rails-0.3.11/lib/pry-rails/prompt.rb +51 -0
  16. data/pry-rails-0.3.11/lib/pry-rails/railtie.rb +50 -0
  17. data/pry-rails-0.3.11/lib/pry-rails/version.rb +5 -0
  18. data/pry-rails-0.3.11/lib/pry-rails.rb +11 -0
  19. data/pry-rails-0.3.11/pry-rails.gemspec +26 -0
  20. data/pry-rails-0.3.11/scenarios/rails30.docker-compose.yml +15 -0
  21. data/pry-rails-0.3.11/scenarios/rails30.dockerfile +5 -0
  22. data/pry-rails-0.3.11/scenarios/rails30.gemfile +7 -0
  23. data/pry-rails-0.3.11/scenarios/rails31.docker-compose.yml +15 -0
  24. data/pry-rails-0.3.11/scenarios/rails31.dockerfile +5 -0
  25. data/pry-rails-0.3.11/scenarios/rails31.gemfile +8 -0
  26. data/pry-rails-0.3.11/scenarios/rails32.docker-compose.yml +15 -0
  27. data/pry-rails-0.3.11/scenarios/rails32.dockerfile +5 -0
  28. data/pry-rails-0.3.11/scenarios/rails32.gemfile +8 -0
  29. data/pry-rails-0.3.11/scenarios/rails40.docker-compose.yml +15 -0
  30. data/pry-rails-0.3.11/scenarios/rails40.dockerfile +5 -0
  31. data/pry-rails-0.3.11/scenarios/rails40.gemfile +6 -0
  32. data/pry-rails-0.3.11/scenarios/rails41.docker-compose.yml +15 -0
  33. data/pry-rails-0.3.11/scenarios/rails41.dockerfile +5 -0
  34. data/pry-rails-0.3.11/scenarios/rails41.gemfile +7 -0
  35. data/pry-rails-0.3.11/scenarios/rails42.docker-compose.yml +15 -0
  36. data/pry-rails-0.3.11/scenarios/rails42.dockerfile +5 -0
  37. data/pry-rails-0.3.11/scenarios/rails42.gemfile +7 -0
  38. data/pry-rails-0.3.11/scenarios/rails50.docker-compose.yml +15 -0
  39. data/pry-rails-0.3.11/scenarios/rails50.dockerfile +5 -0
  40. data/pry-rails-0.3.11/scenarios/rails50.gemfile +7 -0
  41. data/pry-rails-0.3.11/scenarios/rails51.docker-compose.yml +15 -0
  42. data/pry-rails-0.3.11/scenarios/rails51.dockerfile +5 -0
  43. data/pry-rails-0.3.11/scenarios/rails51.gemfile +7 -0
  44. data/pry-rails-0.3.11/scenarios/rails52.docker-compose.yml +15 -0
  45. data/pry-rails-0.3.11/scenarios/rails52.dockerfile +5 -0
  46. data/pry-rails-0.3.11/scenarios/rails52.gemfile +7 -0
  47. data/pry-rails-0.3.11/scenarios/rails60.docker-compose.yml +15 -0
  48. data/pry-rails-0.3.11/scenarios/rails60.dockerfile +5 -0
  49. data/pry-rails-0.3.11/scenarios/rails60.gemfile +7 -0
  50. data/pry-rails-0.3.11/scenarios.yml +30 -0
  51. data/pry-rails-0.3.11/spec/config/config.ru +1 -0
  52. data/pry-rails-0.3.11/spec/config/database.yml +6 -0
  53. data/pry-rails-0.3.11/spec/config/environment.rb +81 -0
  54. data/pry-rails-0.3.11/spec/config/routes.rb +5 -0
  55. data/pry-rails-0.3.11/spec/find_route_spec.rb +44 -0
  56. data/pry-rails-0.3.11/spec/railtie_spec.rb +41 -0
  57. data/pry-rails-0.3.11/spec/recognize_path_spec.rb +57 -0
  58. data/pry-rails-0.3.11/spec/show_middleware_spec.rb +14 -0
  59. data/pry-rails-0.3.11/spec/show_model_spec.rb +55 -0
  60. data/pry-rails-0.3.11/spec/show_models_spec.rb +92 -0
  61. data/pry-rails-0.3.11/spec/show_routes_spec.rb +29 -0
  62. data/pry-rails-0.3.11/spec/spec_helper.rb +33 -0
  63. data/super-smart-box.gemspec +11 -0
  64. metadata +102 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ff967adc39cbfc311e3bcc9edb8b1be379b27c2c4146d3d82bdfb5ca4f6d8f8d
4
+ data.tar.gz: 94ef320d715588bcced1fb32863cb083cdbdb0c71fd5a2c04c76ce0b25138327
5
+ SHA512:
6
+ metadata.gz: b8a4e4d437f283760131cfa37f96cd43b54f528bb40e600bc7e79e271316a842da6f9bbfa5ded694fcb167eb140354cc0bc8d5ae1e89cdaafa41de402312e7e4
7
+ data.tar.gz: 2166d451ed1abf3f5761199bd659d04946765c79fe0fbbccd0274a1402bda429a185827d6a99b9d8ca974b5d15c395aa44973bcc19a77133bfe1c3d6a92c6536
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pry-rails.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Robin Wenglewski
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ require "rubygems"
2
+ require "bundler/setup"
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+ require "appraisal"
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs.concat %w(pry-rails spec)
9
+ t.pattern = "spec/*_spec.rb"
10
+ end
11
+
12
+ desc 'Start the Rails server'
13
+ task :server => :development_env do
14
+ require 'rails/commands/server'
15
+ Rails::Server.start(
16
+ :server => 'WEBrick',
17
+ :environment => 'development',
18
+ :Host => '0.0.0.0',
19
+ :Port => 3000,
20
+ :config => 'config/config.ru'
21
+ )
22
+ end
23
+
24
+ desc 'Start the Rails console'
25
+ task :console => :development_env do
26
+ if (Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR >= 1) ||
27
+ Rails::VERSION::MAJOR >= 6
28
+ require 'rails/command'
29
+ require 'rails/commands/console/console_command'
30
+ else
31
+ require 'rails/commands/console'
32
+ end
33
+
34
+ Rails::Console.start(Rails.application)
35
+ end
36
+
37
+ task :development_env do
38
+ ENV['RAILS_ENV'] = 'development'
39
+ require File.expand_path('../spec/config/environment', __FILE__)
40
+ Dir.chdir(Rails.application.root)
41
+ end
42
+
43
+ # Must invoke indirectly, using `rake appraisal`.
44
+ task :default => [:test]
@@ -0,0 +1,136 @@
1
+ This project is not actively maintained and looking for a maintainer!
2
+
3
+
4
+ # Description
5
+
6
+ Avoid repeating yourself, use pry-rails instead of copying the initializer to every rails project.
7
+ This is a small gem which causes `rails console` to open [pry](https://pry.github.io/). It therefore depends on *pry*.
8
+
9
+ # Prerequisites
10
+
11
+ - A Rails >= 3.0 Application
12
+ - Ruby >= 1.9
13
+
14
+ # Installation
15
+
16
+ Add this line to your gemfile:
17
+
18
+ gem 'pry-rails', :group => :development
19
+
20
+ `bundle install` and enjoy pry.
21
+
22
+ # Usage
23
+
24
+ ```
25
+ $ rails console
26
+ [1] pry(main)> show-routes
27
+ pokemon POST /pokemon(.:format) pokemons#create
28
+ new_pokemon GET /pokemon/new(.:format) pokemons#new
29
+ edit_pokemon GET /pokemon/edit(.:format) pokemons#edit
30
+ GET /pokemon(.:format) pokemons#show
31
+ PUT /pokemon(.:format) pokemons#update
32
+ DELETE /pokemon(.:format) pokemons#destroy
33
+ beer POST /beer(.:format) beers#create
34
+ new_beer GET /beer/new(.:format) beers#new
35
+ edit_beer GET /beer/edit(.:format) beers#edit
36
+ GET /beer(.:format) beers#show
37
+ PUT /beer(.:format) beers#update
38
+ DELETE /beer(.:format) beers#destroy
39
+ [2] pry(main)> show-routes --grep beer
40
+ beer POST /beer(.:format) beers#create
41
+ new_beer GET /beer/new(.:format) beers#new
42
+ edit_beer GET /beer/edit(.:format) beers#edit
43
+ GET /beer(.:format) beers#show
44
+ PUT /beer(.:format) beers#update
45
+ DELETE /beer(.:format) beers#destroy
46
+ [3] pry(main)> show-routes --grep new
47
+ new_pokemon GET /pokemon/new(.:format) pokemons#new
48
+ new_beer GET /beer/new(.:format) beers#new
49
+ [4] pry(main)> show-models
50
+ Beer
51
+ id: integer
52
+ name: string
53
+ type: string
54
+ rating: integer
55
+ ibu: integer
56
+ abv: integer
57
+ created_at: datetime
58
+ updated_at: datetime
59
+ belongs_to hacker
60
+ Hacker
61
+ id: integer
62
+ social_ability: integer
63
+ created_at: datetime
64
+ updated_at: datetime
65
+ has_many pokemons
66
+ has_many beers
67
+ Pokemon
68
+ id: integer
69
+ name: string
70
+ caught: binary
71
+ species: string
72
+ abilities: string
73
+ created_at: datetime
74
+ updated_at: datetime
75
+ belongs_to hacker
76
+ has_many beers through hacker
77
+ ```
78
+
79
+ ## Custom Rails prompt
80
+
81
+ If you want to permanently include the current Rails environment and project name
82
+ in the Pry prompt, put the following lines in your project's `.pryrc`:
83
+
84
+ ```ruby
85
+ Pry.config.prompt = Pry::Prompt[:rails]
86
+ ```
87
+
88
+ If `.pryrc` could be loaded without pry-rails being available or installed,
89
+ guard against setting `Pry.config.prompt` to `nil`:
90
+
91
+ ```ruby
92
+ if Pry::Prompt[:rails]
93
+ Pry.config.prompt = Pry::Prompt[:rails]
94
+ end
95
+ ```
96
+
97
+ Check out `change-prompt --help` for information about temporarily
98
+ changing the prompt for the current Pry session.
99
+
100
+ ## Disabling pry-rails
101
+
102
+ If pry-rails is included in your application but you would prefer not to use it, you may run the following command to set the appropriate environment variable to disable initialization and fall back to the default IRB console:
103
+ ```shell
104
+ DISABLE_PRY_RAILS=1 rails console
105
+ ```
106
+
107
+ Note that you may need to run `spring stop` first.
108
+
109
+ # Developing and Testing
110
+
111
+ This repo uses [Roadshow] to generate a [Docker Compose] file for each
112
+ supported version of Rails (with a compatible version of Ruby for each one).
113
+
114
+ To run specs across all versions, you can either [get the Roadshow tool] and
115
+ run `roadshow run`, or use Docker Compose directly:
116
+
117
+ ```
118
+ $ for fn in scenarios/*.docker-compose.yml; do docker-compose -f $fn run --rm scenario; done
119
+ ```
120
+
121
+ You can also manually run the Rails console and server on each version with
122
+ `roadshow run rake console` and `roadshow run rake server`, or run them on a
123
+ specific version with, e.g., `roadshow run -s rails40 rake console`.
124
+
125
+ To update the set of scenarios, edit `scenarios.yml` and run `roadshow
126
+ generate`, although the Gemfiles in the `scenarios` directory need to be
127
+ maintained manually.
128
+
129
+ [Roadshow]: https://github.com/rf-/roadshow
130
+ [Docker Compose]: https://docs.docker.com/compose/
131
+ [get the Roadshow tool]: https://github.com/rf-/roadshow/releases
132
+
133
+ # Alternative
134
+
135
+ If you want to enable pry everywhere, make sure to check out
136
+ [pry everywhere](http://lucapette.me/pry-everywhere).
@@ -0,0 +1,84 @@
1
+ class PryRails::FindRoute < Pry::ClassCommand
2
+ match 'find-route'
3
+ group 'Rails'
4
+ description 'See which URLs match a given Controller.'
5
+ banner <<-BANNER
6
+ Usage: find-route <controller>
7
+
8
+ Returns the URL(s) that match a given controller or controller action.
9
+
10
+ find-route MyController#show #=> The URL that matches the MyController show action
11
+ find-route MyController #=> All the URLs that hit MyController
12
+ find-route Admin #=> All the URLs that hit the Admin namespace
13
+ find-route Com #=> All the URLS whose controller regex matches /Comm/, e.g CommentsController
14
+ BANNER
15
+
16
+ def process(controller)
17
+ controller_string = controller.to_s
18
+ if single_action?(controller_string)
19
+ single_action(controller_string)
20
+ else
21
+ all_actions(controller_string)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def single_action(controller)
28
+ show_routes { |route| route.defaults == controller_and_action_from(controller) }
29
+ end
30
+
31
+ def all_actions(controller)
32
+ show_routes do |route|
33
+ route.defaults[:controller].to_s =~ /#{normalize_controller_name(controller)}/
34
+ end
35
+ end
36
+
37
+ def controller_and_action_from(controller_and_action)
38
+ controller, action = controller_and_action.split("#")
39
+ {controller: normalize_controller_name(controller), action: action}
40
+ end
41
+
42
+ def routes
43
+ Rails.application.routes.routes
44
+ end
45
+
46
+ def normalize_controller_name(controller)
47
+ controller.underscore.chomp('_controller')
48
+ end
49
+
50
+ def show_routes(&block)
51
+ all_routes = routes.select(&block)
52
+ if all_routes.any?
53
+ grouped_routes = all_routes.group_by { |route| route.defaults[:controller] }
54
+ result = grouped_routes.each_with_object("") do |(controller, routes), res|
55
+ res << "Routes for " + bold(controller.to_s.camelize + "Controller") + "\n"
56
+ res << "--\n"
57
+ routes.each do |route|
58
+ spec = route.path.is_a?(String) ? route.path : route.path.spec
59
+ res << "#{route.defaults[:action]} #{bold(verb_for(route))} #{spec} #{route_helper(route.name)}" + "\n"
60
+ end
61
+ res << "\n"
62
+ end
63
+ stagger_output result
64
+ else
65
+ output.puts "No routes found."
66
+ end
67
+ end
68
+
69
+ def route_helper(name)
70
+ name && "[#{name}]"
71
+ end
72
+
73
+ def verb_for(route)
74
+ %w(GET PUT POST PATCH DELETE).find { |v| route.verb === v }
75
+ end
76
+
77
+ def single_action?(controller)
78
+ controller =~ /#/
79
+ end
80
+
81
+ PryRails::Commands.add_command(self)
82
+ end
83
+
84
+ PryRails::Commands.alias_command "find-routes", "find-route"
@@ -0,0 +1,35 @@
1
+ class PryRails::RecognizePath < Pry::ClassCommand
2
+ match 'recognize-path'
3
+ group 'Rails'
4
+ description 'See which route matches a URL.'
5
+ command_options argument_required: true
6
+ banner <<-BANNER
7
+ Usage: recognize-path <path> [-m|--method METHOD]
8
+
9
+ Verifies that a given path is mapped to the right controller and action.
10
+
11
+ recognize-path example.com
12
+ recognize-path example.com -m post
13
+ BANNER
14
+
15
+ def options(opt)
16
+ opt.on :m, :method, "Methods", :argument => true
17
+ end
18
+
19
+ def process(path)
20
+ method = (opts.m? ? opts[:m] : :get)
21
+ routes = Rails.application.routes
22
+
23
+ begin
24
+ info = routes.recognize_path("http://#{path}", :method => method)
25
+ rescue ActionController::UnknownHttpMethod
26
+ output.puts "Unknown HTTP method: #{method}"
27
+ rescue ActionController::RoutingError => e
28
+ output.puts e
29
+ end
30
+
31
+ output.puts Pry::Helpers::BaseHelpers.colorize_code(info)
32
+ end
33
+
34
+ PryRails::Commands.add_command(self)
35
+ end
@@ -0,0 +1,71 @@
1
+ class PryRails::ShowMiddleware < Pry::ClassCommand
2
+ match 'show-middleware'
3
+ group 'Rails'
4
+ description 'Show all middleware (that Rails knows about).'
5
+ banner <<-BANNER
6
+ Usage: show-middleware [-G]
7
+
8
+ show-middleware shows the Rails app's middleware.
9
+
10
+ If this pry REPL is attached to a Rails server, the entire middleware
11
+ stack is displayed. Otherwise, only the middleware Rails knows about is
12
+ printed.
13
+ BANNER
14
+
15
+ def options(opt)
16
+ opt.on :G, "grep", "Filter output by regular expression", :argument => true
17
+ end
18
+
19
+ def process
20
+ # Assumes there is only one Rack::Server instance.
21
+ # TODO: Figure out what replaced Rack::Server so that we can show the
22
+ # entire middleware stack on recent Rails versions.
23
+ server = nil
24
+ if defined?(Rack::Server)
25
+ ObjectSpace.each_object(Rack::Server) do |object|
26
+ server = object
27
+ end
28
+ end
29
+
30
+ middlewares = []
31
+
32
+ if server
33
+ stack = server.instance_variable_get("@wrapped_app")
34
+ middlewares << stack.class.to_s
35
+
36
+ while stack.instance_variable_defined?("@app") do
37
+ stack = stack.instance_variable_get("@app")
38
+ # Rails 3.0 uses the Application class rather than the application
39
+ # instance itself, so we grab the instance.
40
+ stack = Rails.application if stack == Rails.application.class
41
+ middlewares << stack.class.to_s if stack != Rails.application
42
+ end
43
+ else
44
+ middleware_names = Rails.application.middleware.map do |middleware|
45
+ # After Rails 3.0, the middleware are wrapped in a special class
46
+ # that responds to #name.
47
+ if middleware.respond_to?(:name)
48
+ middleware.name
49
+ else
50
+ middleware.inspect
51
+ end
52
+ end
53
+ middlewares.concat middleware_names
54
+ end
55
+ middlewares << Rails.application.class.to_s
56
+ print_middleware middlewares.grep(Regexp.new(opts[:G] || "."))
57
+ end
58
+
59
+ def print_middleware(middlewares)
60
+ middlewares.each do |middleware|
61
+ string = if middleware == Rails.application.class.to_s
62
+ "run #{middleware}.routes"
63
+ else
64
+ "use #{middleware}"
65
+ end
66
+ output.puts string
67
+ end
68
+ end
69
+
70
+ PryRails::Commands.add_command(self)
71
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: UTF-8
2
+
3
+ class PryRails::ShowModel < Pry::ClassCommand
4
+ match "show-model"
5
+ group "Rails"
6
+ description "Show the given model."
7
+
8
+ def options(opt)
9
+ opt.banner unindent <<-USAGE
10
+ Usage: show-model <model name>
11
+
12
+ show-model displays one model from the current Rails app.
13
+ USAGE
14
+ end
15
+
16
+ def process
17
+ Rails.application.eager_load!
18
+
19
+ if args.empty?
20
+ output.puts opts
21
+ return
22
+ end
23
+
24
+ begin
25
+ model = Object.const_get(args.first)
26
+ rescue NameError
27
+ output.puts "Couldn't find model #{args.first}!"
28
+ return
29
+ end
30
+
31
+ formatter = PryRails::ModelFormatter.new
32
+
33
+ case
34
+ when defined?(ActiveRecord::Base) && model < ActiveRecord::Base
35
+ output.puts formatter.format_active_record(model)
36
+ when defined?(Mongoid::Document) && model < Mongoid::Document
37
+ output.puts formatter.format_mongoid(model)
38
+ else
39
+ output.puts "Don't know how to show #{model}!"
40
+ end
41
+ end
42
+ end
43
+
44
+ PryRails::Commands.add_command PryRails::ShowModel
@@ -0,0 +1,84 @@
1
+ # encoding: UTF-8
2
+
3
+ class PryRails::ShowModels < Pry::ClassCommand
4
+ match "show-models"
5
+ group "Rails"
6
+ description "Show all models."
7
+
8
+ def options(opt)
9
+ opt.banner unindent <<-USAGE
10
+ Usage: show-models
11
+
12
+ show-models displays the current Rails app's models.
13
+ USAGE
14
+
15
+ opt.on :G, "grep", "Filter output by regular expression", :argument => true
16
+ end
17
+
18
+ def process
19
+ Rails.application.eager_load!
20
+
21
+ @formatter = PryRails::ModelFormatter.new
22
+
23
+ display_activerecord_models
24
+ display_mongoid_models
25
+ end
26
+
27
+ def display_activerecord_models
28
+ return unless defined?(ActiveRecord::Base)
29
+
30
+ models = ActiveRecord::Base.descendants
31
+
32
+ models.sort_by(&:to_s).each do |model|
33
+ print_unless_filtered @formatter.format_active_record(model)
34
+ end
35
+ end
36
+
37
+ def display_mongoid_models
38
+ return unless defined?(Mongoid::Document)
39
+
40
+ models = []
41
+
42
+ ObjectSpace.each_object do |o|
43
+ # If this is deprecated, calling any methods on it will emit a warning,
44
+ # so just back away slowly.
45
+ next if ActiveSupport::Deprecation::DeprecationProxy === o
46
+
47
+ is_model = false
48
+
49
+ begin
50
+ is_model = o.class == Class && o.ancestors.include?(Mongoid::Document)
51
+ rescue
52
+ # If it's a weird object, it's not what we want anyway.
53
+ end
54
+
55
+ models << o if is_model
56
+ end
57
+
58
+ models.sort_by(&:to_s).each do |model|
59
+ print_unless_filtered @formatter.format_mongoid(model)
60
+ end
61
+ end
62
+
63
+ def print_unless_filtered(str)
64
+ if opts.present?(:G)
65
+ return unless str =~ grep_regex
66
+ str = colorize_matches(str) # :(
67
+ end
68
+ output.puts str
69
+ end
70
+
71
+ def colorize_matches(string)
72
+ if Pry.color
73
+ string.to_s.gsub(grep_regex) { |s| "\e[7m#{s}\e[27m" }
74
+ else
75
+ string
76
+ end
77
+ end
78
+
79
+ def grep_regex
80
+ @grep_regex ||= Regexp.new(opts[:G], Regexp::IGNORECASE)
81
+ end
82
+ end
83
+
84
+ PryRails::Commands.add_command PryRails::ShowModels
@@ -0,0 +1,92 @@
1
+ class PryRails::ShowRoutes < Pry::ClassCommand
2
+ match 'show-routes'
3
+ group 'Rails'
4
+ description 'Show all routes in match order.'
5
+ banner <<-BANNER
6
+ Usage: show-routes [-G]
7
+ show-routes displays the current Rails app's routes.
8
+ BANNER
9
+
10
+ def options(opt)
11
+ opt.on :G, "grep", "Filter output by regular expression",
12
+ :argument => true,
13
+ :as => Array
14
+ end
15
+
16
+ def process
17
+ Rails.application.reload_routes!
18
+ all_routes = Rails.application.routes.routes
19
+
20
+ formatted =
21
+ if Rails::VERSION::MAJOR >= 6
22
+ process_rails_6_and_higher(all_routes)
23
+ elsif Rails::VERSION::MAJOR == 4 || Rails::VERSION::MAJOR == 5
24
+ process_rails_4_and_5(all_routes)
25
+ elsif Rails::VERSION::MAJOR >= 3 && Rails::VERSION::MINOR >= 2
26
+ process_rails_3_2(all_routes)
27
+ else
28
+ process_rails_3_0_and_3_1(all_routes)
29
+ end
30
+
31
+ output.puts grep_routes(formatted).join("\n")
32
+ end
33
+
34
+ # Takes an array of lines. Returns a list filtered by the conditions in
35
+ # `opts[:G]`.
36
+ def grep_routes(formatted)
37
+ return formatted unless opts[:G]
38
+ grep_opts = opts[:G]
39
+
40
+ grep_opts.reduce(formatted) do |lines, pattern|
41
+ lines.grep(Regexp.new(pattern))
42
+ end
43
+ end
44
+
45
+ # Cribbed from https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake
46
+ def process_rails_3_0_and_3_1(all_routes)
47
+ routes = all_routes.collect do |route|
48
+ reqs = route.requirements.dup
49
+ reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
50
+ reqs = reqs.empty? ? "" : reqs.inspect
51
+
52
+ {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
53
+ end
54
+
55
+ # Skip the route if it's internal info route
56
+ routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
57
+
58
+ name_width = routes.map{ |r| r[:name].length }.max
59
+ verb_width = routes.map{ |r| r[:verb].length }.max
60
+ path_width = routes.map{ |r| r[:path].length }.max
61
+
62
+ routes.map do |r|
63
+ "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
64
+ end
65
+ end
66
+
67
+ def process_rails_3_2(all_routes)
68
+ require 'rails/application/route_inspector'
69
+
70
+ Rails::Application::RouteInspector.new.format(all_routes)
71
+ end
72
+
73
+ def process_rails_4_and_5(all_routes)
74
+ require 'action_dispatch/routing/inspector'
75
+
76
+ ActionDispatch::Routing::RoutesInspector.
77
+ new(all_routes).
78
+ format(ActionDispatch::Routing::ConsoleFormatter.new).
79
+ split(/\n/)
80
+ end
81
+
82
+ def process_rails_6_and_higher(all_routes)
83
+ require 'action_dispatch/routing/inspector'
84
+
85
+ ActionDispatch::Routing::RoutesInspector.
86
+ new(all_routes).
87
+ format(ActionDispatch::Routing::ConsoleFormatter::Sheet.new).
88
+ split(/\n/)
89
+ end
90
+
91
+ PryRails::Commands.add_command(self)
92
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: UTF-8
2
+
3
+ PryRails::Commands = Pry::CommandSet.new
4
+
5
+ command_glob = File.expand_path('../commands/*.rb', __FILE__)
6
+
7
+ Dir[command_glob].each do |command|
8
+ require command
9
+ end
10
+
11
+ Pry.commands.import PryRails::Commands
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'pry-rails/version'
4
+
5
+ if defined?(Rails)
6
+ require 'pry-rails/railtie'
7
+ end