pry-rails 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f1bf84b59262a2cae9e27ff70243ece0801883ac
4
+ data.tar.gz: 561db8abcdc9a12bcc24d4ac4bbb34c15bd1f672
5
+ SHA512:
6
+ metadata.gz: c86ae06eb7cbeb6ecd938e8680fe6c580b43f5457137ad9d5f9fa71298a3d9dc5a1185b1a3c784bc0fb65be10ff5558122dab59da4b5537d7908a5e3741e6ebb
7
+ data.tar.gz: 99719f8dbc4abc3b624ad052211b1f290a82dd506cf0846dc8715be2634a94414efe36263f1c010547a90b82820fee3a1a386b6f2425cc16b29b0351b58a622f
data/.gitignore CHANGED
@@ -2,5 +2,6 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- test/app
5
+ spec/log
6
+ spec/tmp
6
7
  gemfiles
data/Appraisals CHANGED
@@ -5,18 +5,22 @@ end
5
5
 
6
6
  appraise "rails31" do
7
7
  gem "rails", "3.1.6"
8
+ gem "mongoid"
8
9
  gem "sqlite3"
9
10
  end
10
11
 
11
12
  appraise "rails32" do
12
13
  gem "rails", "3.2.6"
14
+ gem "mongoid"
13
15
  gem "sqlite3"
14
16
  end
15
17
 
16
18
  appraise "rails4" do
17
19
  gem "rails",
18
20
  :git => "git@github.com:rails/rails.git"
19
- gem "active_record_deprecated_finders",
20
- :git => "git@github.com:rails/active_record_deprecated_finders"
21
+ gem "activerecord-deprecated_finders",
22
+ :git => "git@github.com:rails/activerecord-deprecated_finders"
23
+ gem "journey",
24
+ :git => "git@github.com:rails/journey"
21
25
  gem "sqlite3"
22
26
  end
@@ -0,0 +1,14 @@
1
+ appraise "rails30" do
2
+ gem "rails", "3.0.15"
3
+ gem "sqlite3"
4
+ end
5
+
6
+ appraise "rails31" do
7
+ gem "rails", "3.1.6"
8
+ gem "sqlite3"
9
+ end
10
+
11
+ appraise "rails32" do
12
+ gem "rails", "3.2.6"
13
+ gem "sqlite3"
14
+ end
data/Rakefile CHANGED
@@ -1,43 +1,45 @@
1
1
  require "rubygems"
2
2
  require "bundler/setup"
3
3
  require "bundler/gem_tasks"
4
+ require "rake/testtask"
4
5
  require "appraisal"
5
6
 
6
- include FileUtils
7
-
8
- desc 'Create test Rails app'
9
- task :init_test_app => 'appraisal:install' do
10
- # Remove and generate test app using Rails 3.0
11
- rm_rf 'test/app'
12
- system 'env BUNDLE_GEMFILE=gemfiles/rails30.gemfile bundle exec rails new test/app'
13
-
14
- # Copy test routes file into place
15
- cp 'test/routes.rb', 'test/app/config/routes.rb'
16
-
17
- # Remove rjs line from environment, since it's gone in versions >= 3.1
18
- env_contents = File.readlines('test/app/config/environments/development.rb')
19
- File.open('test/app/config/environments/development.rb', 'w') do |f|
20
- f.puts env_contents.reject { |l| l =~ /rjs/ }.join("\n")
7
+ if RUBY_VERSION =~ /^1.8/
8
+ class Appraisal::File
9
+ def path
10
+ 'Appraisals-1.8'
11
+ end
21
12
  end
13
+ end
22
14
 
23
- # Generate a few models
24
- cd 'test/app'
25
- system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Pokemon name:string caught:binary species:string abilities:string'
26
- system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Hacker social_ability:integer'
27
- system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rails g model Beer name:string type:string rating:integer ibu:integer abv:integer'
28
- system 'env BUNDLE_GEMFILE=../../gemfiles/rails30.gemfile bundle exec rake db:migrate'
29
-
30
- # Replace generated models
31
- cd '../..'
32
- cp_r 'test/models', 'test/app/app/models'
15
+ Rake::TestTask.new do |t|
16
+ t.libs.concat %w(pry-rails spec)
17
+ t.pattern = "spec/*_spec.rb"
33
18
  end
34
19
 
35
20
  desc 'Start the Rails server'
36
- task :server do
37
- exec 'cd test/app && rails server'
21
+ task :server => :development_env do
22
+ require 'rails/commands/server'
23
+ Rails::Server.start(
24
+ :server => 'WEBrick',
25
+ :environment => 'development',
26
+ :Host => '0.0.0.0',
27
+ :Port => 3000,
28
+ :config => 'config/config.ru'
29
+ )
38
30
  end
39
31
 
40
32
  desc 'Start the Rails console'
41
- task :console do
42
- exec 'cd test/app && rails console'
33
+ task :console => :development_env do
34
+ require 'rails/commands/console'
35
+ Rails::Console.start(Rails.application)
36
+ end
37
+
38
+ task :development_env do
39
+ ENV['RAILS_ENV'] = 'development'
40
+ require File.expand_path('../spec/config/environment', __FILE__)
41
+ Dir.chdir(Rails.application.root)
43
42
  end
43
+
44
+ # Must invoke indirectly, using `rake appraisal`.
45
+ task :default => [:test]
data/Readme.md CHANGED
@@ -70,20 +70,24 @@ Pokemon
70
70
  updated_at: datetime
71
71
  belongs_to hacker
72
72
  has_many beers through hacker
73
+
74
+ $ DISABLE_PRY_RAILS=1 rails console
75
+ irb(main):001:0>
73
76
  ```
74
77
 
75
- # Developing
78
+ # Developing and Testing
76
79
 
77
- To initialize the dev environment for pry-rails, run
78
- `rake init_test_app`.
80
+ To generate Gemfiles for Rails 3.0, 3.1, 3.2, and 4.0, run
81
+ `rake appraisal:gemfiles appraisal:install`.
79
82
 
80
- To test any changes across Rails 3.0, 3.1, 3.2, and 4.0, run
81
- `rake appraisal console` and `rake appraisal server`.
83
+ You can then run the tests across all four versions with `rake appraisal`.
84
+ You can also manually run the Rails console and server with `rake
85
+ appraisal console` and `rake appraisal server`.
82
86
 
83
- For a specific version of Rails, you can use `rake appraisal:rails30`,
84
- `rake appraisal:rails31`, `rake appraisal:rails32`, and `rake
85
- appraisal:rails4`.
87
+ For a specific version of Rails, use `rake appraisal:rails30`, `rake
88
+ appraisal:rails31`, `rake appraisal:rails32`, or `rake appraisal:rails4`.
86
89
 
87
90
  # Alternative
88
91
 
89
- If you want to enable pry everywhere, make sure to check out [pry everywhere](http://lucapette.com/pry/pry-everywhere/).
92
+ If you want to enable pry everywhere, make sure to check out
93
+ [pry everywhere](http://lucapette.com/pry/pry-everywhere/).
@@ -3,9 +3,7 @@
3
3
  require 'pry'
4
4
  require 'pry-rails/version'
5
5
 
6
- if defined?(Rails)
6
+ if defined?(Rails) && !ENV['DISABLE_PRY_RAILS']
7
7
  require 'pry-rails/railtie'
8
- require "pry-rails/commands"
9
-
10
- Pry.commands.import PryRails::Commands
8
+ require 'pry-rails/commands'
11
9
  end
@@ -1,205 +1,11 @@
1
- module PryRails
2
- Commands = Pry::CommandSet.new do
3
- create_command "show-routes", "Print out all defined routes in match order, with names." do
4
- group "Rails"
1
+ # encoding: UTF-8
5
2
 
6
- def options(opt)
7
- opt.banner unindent <<-USAGE
8
- Usage: show-routes [-G]
3
+ PryRails::Commands = Pry::CommandSet.new
9
4
 
10
- show-routes displays the current Rails app's routes.
11
- USAGE
5
+ command_glob = File.expand_path('../commands/*.rb', __FILE__)
12
6
 
13
- opt.on :G, "grep", "Filter output by regular expression", :argument => true
14
- end
15
-
16
- def process
17
- Rails.application.reload_routes!
18
- all_routes = Rails.application.routes.routes
19
-
20
- all_routes = begin
21
- begin
22
- # rails 4
23
- require 'action_dispatch/routing/inspector'
24
- inspector = ActionDispatch::Routing::RoutesInspector.new
25
- rescue LoadError => e
26
- # rails 3.2
27
- require 'rails/application/route_inspector'
28
- inspector = Rails::Application::RouteInspector.new
29
- end
30
- inspector.format(all_routes)
31
- rescue LoadError => e
32
- # rails 3.0 and 3.1. cribbed from
33
- # https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake
34
- routes = all_routes.collect do |route|
35
-
36
- reqs = route.requirements.dup
37
- reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
38
- reqs = reqs.empty? ? "" : reqs.inspect
39
-
40
- {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
41
- end
42
-
43
- # Skip the route if it's internal info route
44
- routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
45
-
46
- name_width = routes.map{ |r| r[:name].length }.max
47
- verb_width = routes.map{ |r| r[:verb].length }.max
48
- path_width = routes.map{ |r| r[:path].length }.max
49
-
50
- routes.map do |r|
51
- "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
52
- end
53
- end
54
-
55
- output.puts all_routes.grep(Regexp.new(opts[:G] || ".")).join "\n"
56
- end
57
- end
58
-
59
- create_command "show-models", "Print out all defined models, with attribrutes." do
60
- group "Rails"
61
-
62
- def options(opt)
63
- opt.banner unindent <<-USAGE
64
- Usage: show-models
65
-
66
- show-models displays the current Rails app's models.
67
- USAGE
68
-
69
- opt.on :G, "grep", "Color output red by regular expression", :argument => true
70
- end
71
-
72
- def process
73
- Rails.application.eager_load!
74
-
75
- if defined?(ActiveRecord::Base)
76
- models = ActiveRecord::Base.descendants.map do |mod|
77
- model_string = mod.to_s + "\n"
78
- if mod.table_exists?
79
- model_string << mod.columns.map { |col| " #{col.name}: #{col.type.to_s}" }.join("\n")
80
- else
81
- model_string << " Table doesn't exist"
82
- end
83
- mod.reflections.each do |model,ref|
84
- model_string << "\n #{ref.macro.to_s} #{model}"
85
- model_string << " through #{ref.options[:through]}" unless ref.options[:through].nil?
86
- end
87
- model_string
88
- end.join("\n")
89
- elsif defined?(Mongoid::Document)
90
- models = get_files.map do |path|
91
- mod = extract_class_name(path)
92
- model_string = "\033[1;34m#{mod.to_s}\033[0m\n"
93
- begin
94
- if mod.constantize.included_modules.include?(Mongoid::Document)
95
- model_string << mod.constantize.fields.values.sort_by(&:name).map { |col|
96
- " #{col.name}: \033[1;33m#{col.options[:type].to_s.downcase}\033[0m"
97
- }.join("\n")
98
- mod.constantize.relations.each do |model,ref|
99
- model_string << "\n #{kind_of_relation(ref.relation.to_s)} \033[1;34m#{model}\033[0m"
100
- model_string << ", autosave" if ref.options[:autosave]
101
- model_string << ", autobuild" if ref.options[:autobuild]
102
- model_string << ", validate" if ref.options[:validate]
103
- model_string << ", dependent-#{ref.options[:dependent]}" if ref.options[:dependent]
104
- end
105
- else
106
- model_string << " Collection doesn't exist"
107
- end
108
- model_string
109
-
110
- rescue Exception
111
- STDERR.puts "Warning: exception #{$!} raised while trying to load model class #{path}"
112
- end
113
- end.join("\n")
114
- end
115
-
116
- models.gsub!(Regexp.new(opts[:G] || ".", Regexp::IGNORECASE)) { |s| text.red(s) } unless opts[:G].nil?
117
- output.puts models
118
- end
119
-
120
- def get_files(prefix ='')
121
- Dir.glob(prefix << "app/models/**/*.rb")
122
- end
123
-
124
- def extract_class_name(filename)
125
- filename.split('/')[2..-1].collect { |i| i.camelize }.join('::').chomp(".rb")
126
- end
127
-
128
- def kind_of_relation(string)
129
- case string.gsub('Mongoid::Relations::', '')
130
- when 'Referenced::Many' then 'has_many'
131
- when 'Referenced::One' then 'has_one'
132
- when 'Referenced::In' then 'belongs_to'
133
- when 'Embedded::Many' then 'embeds_many'
134
- when 'Embedded::One' then 'embeds_one'
135
- when 'Embedded::In' then 'embedded_in'
136
- end
137
- end
138
- end
139
-
140
- create_command "show-middleware" do
141
- group "Rails"
142
-
143
- def options(opt)
144
- opt.banner unindent <<-USAGE
145
- Usage: show-middleware [-G]
146
-
147
- show-middleware shows the Rails app's middleware.
148
-
149
- If this pry REPL is attached to a Rails server, the entire middleware
150
- stack is displayed. Otherwise, only the middleware Rails knows about is
151
- printed.
152
- USAGE
153
-
154
- opt.on :G, "grep", "Filter output by regular expression", :argument => true
155
- end
156
-
157
- def process
158
- # assumes there is only one Rack::Server instance
159
- server = nil
160
- ObjectSpace.each_object(Rack::Server) do |object|
161
- server = object
162
- end
163
-
164
- middlewares = []
165
-
166
- if server
167
- stack = server.instance_variable_get("@wrapped_app")
168
- middlewares << stack.class.to_s
169
-
170
- while stack.instance_variable_defined?("@app") do
171
- stack = stack.instance_variable_get("@app")
172
- # Rails 3.0 uses the Application class rather than the application
173
- # instance itself, so we grab the instance.
174
- stack = Rails.application if stack == Rails.application.class
175
- middlewares << stack.class.to_s if stack != Rails.application
176
- end
177
- else
178
- middleware_names = Rails.application.middleware.map do |middleware|
179
- # After Rails 3.0, the middleware are wrapped in a special class
180
- # that responds to #name.
181
- if middleware.respond_to?(:name)
182
- middleware.name
183
- else
184
- middleware.inspect
185
- end
186
- end
187
- middlewares.concat middleware_names
188
- end
189
- middlewares << Rails.application.class.to_s
190
- print_middleware middlewares.grep(Regexp.new(opts[:G] || "."))
191
- end
192
-
193
- def print_middleware(middlewares)
194
- middlewares.each do |middleware|
195
- string = if middleware == Rails.application.class.to_s
196
- "run #{middleware}.routes"
197
- else
198
- "use #{middleware}"
199
- end
200
- output.puts string
201
- end
202
- end
203
- end
204
- end
7
+ Dir[command_glob].each do |command|
8
+ require command
205
9
  end
10
+
11
+ Pry.commands.import PryRails::Commands
@@ -0,0 +1,67 @@
1
+ # encoding: UTF-8
2
+
3
+ PryRails::Commands.create_command "show-middleware" do
4
+ group "Rails"
5
+ description "Show all middleware (that Rails knows about)."
6
+
7
+ def options(opt)
8
+ opt.banner unindent <<-USAGE
9
+ Usage: show-middleware [-G]
10
+
11
+ show-middleware shows the Rails app's middleware.
12
+
13
+ If this pry REPL is attached to a Rails server, the entire middleware
14
+ stack is displayed. Otherwise, only the middleware Rails knows about is
15
+ printed.
16
+ USAGE
17
+
18
+ opt.on :G, "grep", "Filter output by regular expression", :argument => true
19
+ end
20
+
21
+ def process
22
+ # assumes there is only one Rack::Server instance
23
+ server = nil
24
+ ObjectSpace.each_object(Rack::Server) do |object|
25
+ server = object
26
+ end
27
+
28
+ middlewares = []
29
+
30
+ if server
31
+ stack = server.instance_variable_get("@wrapped_app")
32
+ middlewares << stack.class.to_s
33
+
34
+ while stack.instance_variable_defined?("@app") do
35
+ stack = stack.instance_variable_get("@app")
36
+ # Rails 3.0 uses the Application class rather than the application
37
+ # instance itself, so we grab the instance.
38
+ stack = Rails.application if stack == Rails.application.class
39
+ middlewares << stack.class.to_s if stack != Rails.application
40
+ end
41
+ else
42
+ middleware_names = Rails.application.middleware.map do |middleware|
43
+ # After Rails 3.0, the middleware are wrapped in a special class
44
+ # that responds to #name.
45
+ if middleware.respond_to?(:name)
46
+ middleware.name
47
+ else
48
+ middleware.inspect
49
+ end
50
+ end
51
+ middlewares.concat middleware_names
52
+ end
53
+ middlewares << Rails.application.class.to_s
54
+ print_middleware middlewares.grep(Regexp.new(opts[:G] || "."))
55
+ end
56
+
57
+ def print_middleware(middlewares)
58
+ middlewares.each do |middleware|
59
+ string = if middleware == Rails.application.class.to_s
60
+ "run #{middleware}.routes"
61
+ else
62
+ "use #{middleware}"
63
+ end
64
+ output.puts string
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,153 @@
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
+ display_activerecord_models
22
+ display_mongoid_models
23
+ end
24
+
25
+ def display_activerecord_models
26
+ return unless defined?(ActiveRecord::Base)
27
+
28
+ models = ActiveRecord::Base.descendants
29
+
30
+ models.sort_by(&:to_s).each do |model|
31
+ out = []
32
+ out.push format_model_name model
33
+
34
+ if model.table_exists?
35
+ model.columns.each do |column|
36
+ out.push format_column column.name, column.type
37
+ end
38
+ else
39
+ out.push format_error "Table doesn't exist"
40
+ end
41
+
42
+ reflections = model.reflections.sort_by do |other_model, reflection|
43
+ [reflection.macro.to_s, other_model.to_s]
44
+ end
45
+
46
+ reflections.each do |other_model, reflection|
47
+ options = []
48
+
49
+ if reflection.options[:through].present?
50
+ options << "through #{text.blue ":#{reflection.options[:through]}"}"
51
+ end
52
+
53
+ out.push format_association reflection.macro, other_model, options
54
+ end
55
+
56
+ print_unless_filtered out
57
+ end
58
+ end
59
+
60
+ def display_mongoid_models
61
+ return unless defined?(Mongoid::Document)
62
+
63
+ models = []
64
+
65
+ ObjectSpace.each_object do |o|
66
+ is_model = false
67
+
68
+ begin
69
+ is_model = o.class == Class && o.ancestors.include?(Mongoid::Document)
70
+ rescue => e
71
+ # If it's a weird object, it's not what we want anyway.
72
+ end
73
+
74
+ models << o if is_model
75
+ end
76
+
77
+ models.sort_by(&:to_s).each do |model|
78
+ out = []
79
+ out.push format_model_name model
80
+
81
+ model.fields.values.sort_by(&:name).each do |column|
82
+ out.push format_column column.name, column.options[:type]
83
+ end
84
+
85
+ model.relations.each do |other_model, ref|
86
+ options = []
87
+ options << 'autosave' if ref.options[:autosave]
88
+ options << 'autobuild' if ref.options[:autobuild]
89
+ options << 'validate' if ref.options[:validate]
90
+
91
+ if ref.options[:dependent]
92
+ options << "dependent-#{ref.options[:dependent]}"
93
+ end
94
+
95
+ out.push format_association \
96
+ kind_of_relation(ref.relation), other_model, options
97
+ end
98
+
99
+ print_unless_filtered out
100
+ end
101
+ end
102
+
103
+ def format_model_name(model)
104
+ text.bright_blue model
105
+ end
106
+
107
+ def format_column(name, type)
108
+ " #{name}: #{text.green type}"
109
+ end
110
+
111
+ def format_association(type, other, options = [])
112
+ options_string = (options.any?) ? " (#{options.join(', ')})" : ''
113
+ " #{type} #{text.blue ":#{other}"}#{options_string}"
114
+ end
115
+
116
+ def format_error(message)
117
+ " #{text.red message}"
118
+ end
119
+
120
+ def print_unless_filtered array_of_strings
121
+ result = array_of_strings.join("\n")
122
+ if opts.present?(:G)
123
+ return unless result =~ grep_regex
124
+ result = colorize_matches(result) # :(
125
+ end
126
+ output.puts result
127
+ end
128
+
129
+ def colorize_matches(string)
130
+ if Pry.color
131
+ string.to_s.gsub(grep_regex) { |s| "\e[7m#{s}\e[27m" }
132
+ else
133
+ string
134
+ end
135
+ end
136
+
137
+ def grep_regex
138
+ @grep_regex ||= Regexp.new(opts[:G], Regexp::IGNORECASE)
139
+ end
140
+
141
+ def kind_of_relation(relation)
142
+ case relation.to_s.sub(/^Mongoid::Relations::/, '')
143
+ when 'Referenced::Many' then 'has_many'
144
+ when 'Referenced::One' then 'has_one'
145
+ when 'Referenced::In' then 'belongs_to'
146
+ when 'Embedded::Many' then 'embeds_many'
147
+ when 'Embedded::One' then 'embeds_one'
148
+ when 'Embedded::In' then 'embedded_in'
149
+ end
150
+ end
151
+ end
152
+
153
+ PryRails::Commands.add_command PryRails::ShowModels
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+
3
+ PryRails::Commands.create_command "show-routes" do
4
+ group "Rails"
5
+ description "Show all routes in match order."
6
+
7
+ def options(opt)
8
+ opt.banner unindent <<-USAGE
9
+ Usage: show-routes [-G]
10
+
11
+ show-routes displays the current Rails app's routes.
12
+ USAGE
13
+
14
+ opt.on :G, "grep", "Filter output by regular expression", :argument => true
15
+ end
16
+
17
+ def process
18
+ Rails.application.reload_routes!
19
+ all_routes = Rails.application.routes.routes
20
+
21
+ formatted = case Rails.version.to_s
22
+ when /^4/
23
+ process_rails_4(all_routes)
24
+ when /^3\.2/
25
+ process_rails_3_2(all_routes)
26
+ else
27
+ process_rails_3_0_and_3_1(all_routes)
28
+ end
29
+
30
+ output.puts formatted.grep(Regexp.new(opts[:G] || ".")).join("\n")
31
+ end
32
+
33
+ # Cribbed from https://github.com/rails/rails/blob/3-1-stable/railties/lib/rails/tasks/routes.rake
34
+ def process_rails_3_0_and_3_1(all_routes)
35
+ routes = all_routes.collect do |route|
36
+ reqs = route.requirements.dup
37
+ reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
38
+ reqs = reqs.empty? ? "" : reqs.inspect
39
+
40
+ {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
41
+ end
42
+
43
+ # Skip the route if it's internal info route
44
+ routes.reject! { |r| r[:path] =~ %r{/rails/info/properties|^/assets} }
45
+
46
+ name_width = routes.map{ |r| r[:name].length }.max
47
+ verb_width = routes.map{ |r| r[:verb].length }.max
48
+ path_width = routes.map{ |r| r[:path].length }.max
49
+
50
+ routes.map do |r|
51
+ "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
52
+ end
53
+ end
54
+
55
+ def process_rails_3_2(all_routes)
56
+ require 'rails/application/route_inspector'
57
+ Rails::Application::RouteInspector.new.format(all_routes)
58
+ end
59
+
60
+ def process_rails_4(all_routes)
61
+ require 'action_dispatch/routing/inspector'
62
+ ActionDispatch::Routing::RoutesInspector.new(all_routes).format(ActionDispatch::Routing::ConsoleFormatter.new).split(/\n/)
63
+ end
64
+ end
@@ -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
@@ -3,6 +3,9 @@
3
3
  module PryRails
4
4
  class Railtie < Rails::Railtie
5
5
  console do
6
+ require 'pry'
7
+ require 'pry-rails/commands'
8
+
6
9
  if Rails::VERSION::MAJOR == 3
7
10
  Rails::Console::IRB = Pry
8
11
 
@@ -24,4 +27,3 @@ module PryRails
24
27
  end
25
28
  end
26
29
  end
27
-
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module PryRails
4
- VERSION = "0.2.2"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -9,6 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.email = ["robin@wenglewski.de"]
10
10
  s.homepage = "https://github.com/rweng/pry-rails"
11
11
  s.summary = %q{Use Pry as your rails console}
12
+ s.license = "MIT"
12
13
  # s.description = %q{TODO: Write a gem description}
13
14
 
14
15
  # s.rubyforge_project = "pry-rails"
@@ -20,4 +21,6 @@ Gem::Specification.new do |s|
20
21
 
21
22
  s.add_dependency "pry", ">= 0.9.10"
22
23
  s.add_development_dependency "appraisal"
24
+ s.add_development_dependency "minitest"
25
+ s.add_development_dependency "rr"
23
26
  end
@@ -0,0 +1 @@
1
+ run Rails.application
@@ -0,0 +1,6 @@
1
+ development:
2
+ adapter: sqlite3
3
+ database: ":memory:"
4
+ test:
5
+ adapter: sqlite3
6
+ database: ":memory:"
@@ -0,0 +1,79 @@
1
+ require 'rails'
2
+ require 'rails/all'
3
+ require 'active_support/core_ext'
4
+
5
+ require 'pry-rails'
6
+
7
+ # Initialize our test app
8
+
9
+ class TestApp < Rails::Application
10
+ config.active_support.deprecation = :log
11
+ config.eager_load = false
12
+
13
+ config.secret_token = 'a' * 100
14
+
15
+ config.root = File.expand_path('../..', __FILE__)
16
+ end
17
+
18
+ TestApp.initialize!
19
+
20
+ # Create in-memory database
21
+
22
+ ActiveRecord::Migration.verbose = false
23
+
24
+ ActiveRecord::Schema.define do
25
+ create_table :pokemons do |t|
26
+ t.string :name
27
+ t.binary :caught
28
+ t.string :species
29
+ t.string :abilities
30
+ end
31
+
32
+ create_table :hackers do |t|
33
+ t.integer :social_ability
34
+ end
35
+
36
+ create_table :beers do |t|
37
+ t.string :name
38
+ t.string :type
39
+ t.integer :rating
40
+ t.integer :ibu
41
+ t.integer :abv
42
+ end
43
+ end
44
+
45
+ # Define models
46
+
47
+ class Beer < ActiveRecord::Base
48
+ belongs_to :hacker
49
+ end
50
+
51
+ class Hacker < ActiveRecord::Base
52
+ has_many :pokemons
53
+ has_many :beers
54
+ end
55
+
56
+ class Pokemon < ActiveRecord::Base
57
+ belongs_to :hacker
58
+ has_many :beers, :through => :hacker
59
+ end
60
+
61
+ begin
62
+ require 'mongoid'
63
+
64
+ class Artist
65
+ include Mongoid::Document
66
+
67
+ field :name, :type => String
68
+ embeds_one :beer
69
+ embeds_many :instruments
70
+ end
71
+
72
+ class Instrument
73
+ include Mongoid::Document
74
+
75
+ field :name, :type => String
76
+ embedded_in :artist
77
+ end
78
+ rescue LoadError # Mongoid doesn't support Rails 3.0 or 4.0 or Ruby 1.8
79
+ end
@@ -1,4 +1,4 @@
1
- App::Application.routes.draw do
1
+ TestApp.routes.draw do
2
2
  resource :pokemon, :beer
3
3
  get 'pry' => proc { binding.pry; [200, {}, ['']] }
4
4
  end
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+ require 'rails/commands/console'
5
+
6
+ describe PryRails::Railtie do
7
+ it 'should start Pry instead of IRB and make the helpers available' do
8
+ mock(Pry).start
9
+ Rails::Console.start(Rails.application)
10
+ assert RR.verify
11
+
12
+ %w(app helper reload!).each do |helper|
13
+ TOPLEVEL_BINDING.eval("respond_to?(:#{helper}, true)").must_equal true
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "show-middleware" do
6
+ it "should print a list of middleware" do
7
+ output = mock_pry('show-middleware', 'exit-all')
8
+
9
+ output.must_match %r{\Ause ActionDispatch::Static}
10
+ output.must_match %r{^use ActionDispatch::ShowExceptions$}
11
+ output.must_match %r{^run TestApp.routes\Z}
12
+ end
13
+ end
14
+
@@ -0,0 +1,77 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe "show-models" do
6
+ it "should print a list of models" do
7
+ output = mock_pry('show-models', 'exit-all')
8
+
9
+ ar_models = <<MODELS
10
+ Beer
11
+ id: integer
12
+ name: string
13
+ type: string
14
+ rating: integer
15
+ ibu: integer
16
+ abv: integer
17
+ belongs_to :hacker
18
+ Hacker
19
+ id: integer
20
+ social_ability: integer
21
+ has_many :beers
22
+ has_many :pokemons
23
+ Pokemon
24
+ id: integer
25
+ name: string
26
+ caught: binary
27
+ species: string
28
+ abilities: string
29
+ belongs_to :hacker
30
+ has_many :beers (through :hacker)
31
+ MODELS
32
+
33
+ mongoid_models = <<MODELS
34
+ Artist
35
+ _id: Moped::BSON::ObjectId
36
+ name: String
37
+ embeds_one :beer (validate)
38
+ embeds_many :instruments (validate)
39
+ Instrument
40
+ _id: Moped::BSON::ObjectId
41
+ name: String
42
+ embedded_in :artist
43
+ MODELS
44
+
45
+ if defined?(Mongoid)
46
+ output.gsub! /^ *_type: String\n/, '' # mongoid 3.0 and 3.1 differ on this
47
+ output.must_equal [ar_models, mongoid_models].join
48
+ else
49
+ output.must_equal ar_models
50
+ end
51
+ end
52
+
53
+ it "should highlight the given phrase with --grep" do
54
+ begin
55
+ Pry.color = true
56
+
57
+ output = mock_pry('show-models --grep rating', 'exit-all')
58
+
59
+ output.must_include "Beer"
60
+ output.must_include "\e[7mrating\e[27m"
61
+ output.wont_include "Pokemon"
62
+
63
+ if defined?(Mongoid)
64
+ output.wont_include "Artist"
65
+ end
66
+ ensure
67
+ Pry.color = false
68
+ end
69
+ end
70
+
71
+ if defined?(Mongoid)
72
+ it "should also filter for mongoid" do
73
+ output = mock_pry('show-models --grep beer', 'exit-all')
74
+ output.must_include 'Artist'
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ # We can just have a smoke test for this one since it's mostly using built-in
4
+ # Rails functionality. Plus the output is a bit different between Rails
5
+ # versions, so that's annoying.
6
+
7
+ require 'spec_helper'
8
+
9
+ describe "show-routes" do
10
+ it "should print a list of routes" do
11
+ output = mock_pry('show-routes', 'exit-all')
12
+
13
+ output.must_match %r{^edit_pokemon GET /pokemon/edit}
14
+ end
15
+ end
@@ -0,0 +1,41 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ require 'rr'
5
+
6
+ class MiniTest::Spec
7
+ include RR::Adapters::RRMethods
8
+ end
9
+
10
+ require 'config/environment'
11
+
12
+ # Pry testing stuff (taken from Pry itself)
13
+
14
+ Pry.color = false
15
+
16
+ def redirect_pry_io(new_in, new_out = StringIO.new)
17
+ old_in = Pry.input
18
+ old_out = Pry.output
19
+
20
+ Pry.input = new_in
21
+ Pry.output = new_out
22
+
23
+ begin
24
+ yield
25
+ ensure
26
+ Pry.input = old_in
27
+ Pry.output = old_out
28
+ end
29
+ end
30
+
31
+ def mock_pry(*args)
32
+ binding = args.first.is_a?(Binding) ? args.shift : binding()
33
+ input = StringIO.new(args.join("\n"))
34
+ output = StringIO.new
35
+
36
+ redirect_pry_io(input, output) do
37
+ Pry.start(binding, :hooks => Pry::Hooks.new)
38
+ end
39
+
40
+ output.string
41
+ end
metadata CHANGED
@@ -1,46 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pry-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Robin Wenglewski
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-09-21 00:00:00.000000000 Z
11
+ date: 2013-05-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: pry
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: 0.9.10
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: 0.9.10
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: appraisal
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
44
67
  - !ruby/object:Gem::Version
45
68
  version: '0'
46
69
  description:
@@ -52,45 +75,61 @@ extra_rdoc_files: []
52
75
  files:
53
76
  - .gitignore
54
77
  - Appraisals
78
+ - Appraisals-1.8
55
79
  - Gemfile
56
80
  - LICENCE
57
81
  - Rakefile
58
82
  - Readme.md
59
83
  - lib/pry-rails.rb
60
84
  - lib/pry-rails/commands.rb
85
+ - lib/pry-rails/commands/show_middleware.rb
86
+ - lib/pry-rails/commands/show_models.rb
87
+ - lib/pry-rails/commands/show_routes.rb
88
+ - lib/pry-rails/console.rb
61
89
  - lib/pry-rails/railtie.rb
62
90
  - lib/pry-rails/version.rb
63
91
  - pry-rails.gemspec
64
- - test/models/beer.rb
65
- - test/models/hacker.rb
66
- - test/models/pokemon.rb
67
- - test/routes.rb
92
+ - spec/config/config.ru
93
+ - spec/config/database.yml
94
+ - spec/config/environment.rb
95
+ - spec/config/routes.rb
96
+ - spec/railtie_spec.rb
97
+ - spec/show_middleware_spec.rb
98
+ - spec/show_models_spec.rb
99
+ - spec/show_routes_spec.rb
100
+ - spec/spec_helper.rb
68
101
  homepage: https://github.com/rweng/pry-rails
69
- licenses: []
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
70
105
  post_install_message:
71
106
  rdoc_options: []
72
107
  require_paths:
73
108
  - lib
74
109
  required_ruby_version: !ruby/object:Gem::Requirement
75
- none: false
76
110
  requirements:
77
- - - ! '>='
111
+ - - '>='
78
112
  - !ruby/object:Gem::Version
79
113
  version: '0'
80
114
  required_rubygems_version: !ruby/object:Gem::Requirement
81
- none: false
82
115
  requirements:
83
- - - ! '>='
116
+ - - '>='
84
117
  - !ruby/object:Gem::Version
85
118
  version: '0'
86
119
  requirements: []
87
120
  rubyforge_project:
88
- rubygems_version: 1.8.24
121
+ rubygems_version: 2.0.3
89
122
  signing_key:
90
- specification_version: 3
123
+ specification_version: 4
91
124
  summary: Use Pry as your rails console
92
125
  test_files:
93
- - test/models/beer.rb
94
- - test/models/hacker.rb
95
- - test/models/pokemon.rb
96
- - test/routes.rb
126
+ - spec/config/config.ru
127
+ - spec/config/database.yml
128
+ - spec/config/environment.rb
129
+ - spec/config/routes.rb
130
+ - spec/railtie_spec.rb
131
+ - spec/show_middleware_spec.rb
132
+ - spec/show_models_spec.rb
133
+ - spec/show_routes_spec.rb
134
+ - spec/spec_helper.rb
135
+ has_rdoc:
@@ -1,3 +0,0 @@
1
- class Beer < ActiveRecord::Base
2
- belongs_to :hacker
3
- end
@@ -1,4 +0,0 @@
1
- class Hacker < ActiveRecord::Base
2
- has_many :pokemons
3
- has_many :beers
4
- end
@@ -1,4 +0,0 @@
1
- class Pokemon < ActiveRecord::Base
2
- belongs_to :hacker
3
- has_many :beers, through: :hacker
4
- end