Skip to content

Require rails console methods rails 7.2 #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/pry-rails/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Railtie < Rails::Railtie
TOPLEVEL_BINDING.eval('self').extend ::Rails::ConsoleMethods
end

if major == 7 && minor >= 2
require "rails/console/methods"
end

if major > 7 || (major == 7 && minor >= 2)
require "rails/commands/console/irb_console"

Expand Down
2 changes: 1 addition & 1 deletion scenarios/rails60.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ FROM ruby:2.5
RUN mkdir -p /scenario
WORKDIR /scenario
ENV LANG=C.UTF-8
CMD (bundle check || (gem install bundler && bundle install)) && bundle exec rake
CMD (bundle check || (gem install bundler --version 2.3.27 && bundle install)) && bundle exec rake
2 changes: 1 addition & 1 deletion scenarios/rails60.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem "rails", github: "rails/rails"
gem "rails", "~> 6.0"
gem "mongoid"
gem "sqlite3"

Expand Down
14 changes: 14 additions & 0 deletions scenarios/rails72.docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
services:
scenario:
build:
context: ..
dockerfile: scenarios/rails72.dockerfile
image: pryrails_scenario_rails72
volumes:
- "..:/scenario"
- "bundle_rails72:/usr/local/bundle"
environment:
BUNDLE_GEMFILE: scenarios/rails72.gemfile
volumes:
bundle_rails72: {}
5 changes: 5 additions & 0 deletions scenarios/rails72.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM ruby:3.3
RUN mkdir -p /scenario
WORKDIR /scenario
ENV LANG=C.UTF-8
CMD (bundle check || (gem install bundler && bundle install)) && bundle exec rake
7 changes: 7 additions & 0 deletions scenarios/rails72.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source "https://rubygems.org"

gem "rails", "~> 7.2"
gem "mongoid"
gem "sqlite3"

gemspec :path => "../"
34 changes: 34 additions & 0 deletions spec/config/storage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

local:
service: Disk
root: <%= Rails.root.join("storage") %>

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
# amazon:
# service: S3
# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
# region: us-east-1
# bucket: your_own_bucket

# Remember not to checkin your GCS keyfile to a repository
# google:
# service: GCS
# project: your_project
# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %>
# bucket: your_own_bucket

# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key)
# microsoft:
# service: AzureStorage
# storage_account_name: your_account_name
# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %>
# container: your_container_name

# mirror:
# service: Mirror
# primary: local
# mirrors: [ amazon, google, microsoft ]
24 changes: 12 additions & 12 deletions spec/find_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,29 @@

it 'returns the route for a single action' do
output = mock_pry('find-route Admin::UsersController#show', 'exit-all')
output.must_match(/show GET/)
output.wont_match(/index GET/)
_(output).must_match(/show GET/)
_(output).wont_match(/index GET/)
end

it 'returns all the routes for a controller' do
output = mock_pry('find-route Admin::UsersController', 'exit-all')
output.must_match(/index GET/)
output.must_match(/show GET/)
output.must_match(/new GET/)
output.must_match(/edit GET/)
output.must_match(/update (PATCH|PUT)/)
output.must_match(/update PUT/)
output.must_match(/destroy DELETE/)
_(output).must_match(/index GET/)
_(output).must_match(/show GET/)
_(output).must_match(/new GET/)
_(output).must_match(/edit GET/)
_(output).must_match(/update (PATCH|PUT)/)
_(output).must_match(/update PUT/)
_(output).must_match(/destroy DELETE/)
end

it 'returns all routes for controllers under a namespace' do
output = mock_pry('find-route Admin', 'exit-all')
output.must_match(/Routes for Admin::UsersController/)
output.must_match(/Routes for Admin::ImagesController/)
_(output).must_match(/Routes for Admin::UsersController/)
_(output).must_match(/Routes for Admin::ImagesController/)
end

it 'returns no routes found when controller is not recognized' do
output = mock_pry('find-route Foo', 'exit-all')
output.must_match(/No routes found/)
_(output).must_match(/No routes found/)
end
end
2 changes: 1 addition & 1 deletion spec/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def self.start(*)
end

%w(app helper reload!).each do |helper|
TOPLEVEL_BINDING.eval("respond_to?(:#{helper}, true)").must_equal true
_(TOPLEVEL_BINDING.eval("respond_to?(:#{helper}, true)")).must_equal true
end
end
end
22 changes: 11 additions & 11 deletions spec/recognize_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,36 @@

it 'fails gracefully if no path is given' do
output = mock_pry('recognize-path', 'exit-all')
output.must_equal \
"Error: The command 'recognize-path' requires an argument.\n"
_(output).must_equal \
"\e[0GError: The command 'recognize-path' requires an argument.\n"
end

it "prints info about controller/action that is bound to the given path" do
output = mock_pry('recognize-path example.com', 'exit-all')
output.must_match(/controller.+foo/)
output.must_match(/action.+index/)
_(output).must_match(/controller.+foo/)
_(output).must_match(/action.+index/)
end

it "accepts short path" do
output = mock_pry('recognize-path /booms/1/edit', 'exit-all')
output.must_match(/action.+edit/)
output.must_match(/controller.+booms/)
output.must_match(/id.+1/)
_(output).must_match(/action.+edit/)
_(output).must_match(/controller.+booms/)
_(output).must_match(/id.+1/)
end

it "accepts -m switch" do
output = mock_pry('recognize-path example.com/booms -m post', 'exit-all')
output.must_match(/controller.+booms/)
output.must_match(/action.+create/)
_(output).must_match(/controller.+booms/)
_(output).must_match(/action.+create/)
end

it "doesn't accept unknown methods" do
output = mock_pry('recognize-path example.com/booms -m posty', 'exit-all')
output.must_match 'Unknown HTTP method: posty'
_(output).must_match 'Unknown HTTP method: posty'
end

it "doesn't accept unknown routes" do
output = mock_pry('recognize-path bing/bang/bong', 'exit-all')
output.must_match 'No route matches "http://bing/bang/bong"'
_(output).must_match 'No route matches "http://bing/bang/bong"'
end
end
6 changes: 3 additions & 3 deletions spec/show_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
it "should print a list of middleware" do
output = mock_pry('show-middleware', 'exit-all')

output.must_match %r{^use ActionDispatch::Static$}
output.must_match %r{^use ActionDispatch::ShowExceptions$}
output.must_match %r{^run TestApp.routes\Z}
_(output).must_match %r{^use ActionDispatch::Static$}
_(output).must_match %r{^use ActionDispatch::ShowExceptions$}
_(output).must_match %r{^run TestApp.routes\Z}
end
end

14 changes: 7 additions & 7 deletions spec/show_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
output = mock_pry('show-model Beer', 'exit-all')

expected = <<MODEL
Beer
\e[0GBeer
id: integer
name: string
type: string
Expand All @@ -17,15 +17,15 @@
belongs_to :hacker
MODEL

output.must_equal expected
_(output).must_equal expected
end

if defined? Mongoid
it "should print one Mongoid model" do
output = mock_pry('show-model Artist', 'exit-all')

expected = <<MODEL
Artist
\e[0GArtist
_id: BSON::ObjectId
name: String
embeds_one :beer (validate)
Expand All @@ -34,22 +34,22 @@

output.gsub!(/^ *_type: String\n/, '') # mongoid 3.0 and 3.1 differ on this
output.gsub!(/Moped::BSON/, 'BSON') # mongoid 3 and 4 differ on this
output.must_equal expected
_(output).must_equal expected
end
end

it "should print an error if the model doesn't exist" do
output = mock_pry('show-model FloojBulb', 'exit-all')
output.must_equal "Couldn't find model FloojBulb!\n"
_(output).must_equal "\e[0GCouldn't find model FloojBulb!\n"
end

it "should print an error if it doesn't know what to do with the model" do
output = mock_pry('show-model PryRails', 'exit-all')
output.must_equal "Don't know how to show PryRails!\n"
_(output).must_equal "\e[0GDon't know how to show PryRails!\n"
end

it "should print help if no model name is given" do
output = mock_pry('show-model', 'exit-all')
output.must_match(/Usage: show-model/)
_(output).must_match(/Usage: show-model/)
end
end
56 changes: 49 additions & 7 deletions spec/show_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,48 @@
_id: BSON::ObjectId
name: String
embedded_in :artist
Mongoid::GlobalDiscriminatorKeyAssignment::InvalidFieldHost
_id: BSON::ObjectId
MODELS

rails_7_2_models = <<RAILS_7_2_MODELS
\e[0GActionMailbox::InboundEmail
Table doesn't exist
has_one :raw_email_attachment (class_name :ActiveStorage::Attachment)
has_one :raw_email_blob (through :raw_email_attachment, class_name :ActiveStorage::Blob)
ActionMailbox::Record
Table doesn't exist
ActionText::EncryptedRichText
Table doesn't exist
belongs_to :record
has_many :embeds_attachments (class_name :ActiveStorage::Attachment)
has_many :embeds_blobs (through :embeds_attachments, class_name :ActiveStorage::Blob)
ActionText::Record
Table doesn't exist
ActionText::RichText
Table doesn't exist
belongs_to :record
has_many :embeds_attachments (class_name :ActiveStorage::Attachment)
has_many :embeds_blobs (through :embeds_attachments, class_name :ActiveStorage::Blob)
ActiveStorage::Attachment
Table doesn't exist
belongs_to :blob (class_name :ActiveStorage::Blob)
belongs_to :record
ActiveStorage::Blob
Table doesn't exist
has_many :attachments
has_many :variant_records (class_name :ActiveStorage::VariantRecord)
has_one :preview_image_attachment (class_name :ActiveStorage::Attachment)
has_one :preview_image_blob (through :preview_image_attachment, class_name :ActiveStorage::Blob)
ActiveStorage::Record
Table doesn't exist
ActiveStorage::VariantRecord
Table doesn't exist
belongs_to :blob
has_one :image_attachment (class_name :ActiveStorage::Attachment)
has_one :image_blob (through :image_attachment, class_name :ActiveStorage::Blob)
RAILS_7_2_MODELS

internal_models = <<MODELS
ActiveRecord::InternalMetadata
key: string
Expand All @@ -58,11 +98,13 @@
expected_output += mongoid_models
end

if Rails::VERSION::MAJOR >= 5
if (Rails::VERSION::MAJOR >= 7 && Rails::VERSION::MINOR >= 2) || Rails::VERSION::MAJOR >= 8
expected_output = rails_7_2_models + expected_output
elsif Rails::VERSION::MAJOR >= 5
expected_output = internal_models + expected_output
end

output.must_equal expected_output
_(output).must_equal expected_output
end

it "should highlight the given phrase with --grep" do
Expand All @@ -71,12 +113,12 @@

output = mock_pry('show-models --grep rating', 'exit-all')

output.must_include "Beer"
output.must_include "\e[7mrating\e[27m"
output.wont_include "Pokemon"
_(output).must_include "Beer"
_(output).must_include "\e[7mrating\e[27m"
_(output).wont_include "Pokemon"

if defined?(Mongoid)
output.wont_include "Artist"
_(output).wont_include "Artist"
end
ensure
Pry.color = false
Expand All @@ -86,7 +128,7 @@
if defined?(Mongoid)
it "should also filter for mongoid" do
output = mock_pry('show-models --grep beer', 'exit-all')
output.must_include 'Artist'
_(output).must_include 'Artist'
end
end
end
10 changes: 5 additions & 5 deletions spec/show_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
it "should print a list of routes" do
output = mock_pry('show-routes', 'exit-all')

output.must_match %r{edit_pokemon GET /pokemon/edit}
_(output).must_match %r{edit_pokemon GET /pokemon/edit}
end

it "should print a list of routes which include grep option" do
output = mock_pry('show-routes -G edit', 'exit-all')

output.must_match %r{edit_pokemon GET /pokemon/edit}
output.must_match %r{ edit_beer GET /beer/edit}
_(output).must_match %r{edit_pokemon GET /pokemon/edit}
_(output).must_match %r{ edit_beer GET /beer/edit}
end

it "should filter list based on multiple grep options" do
output = mock_pry('show-routes -G edit -G pokemon', 'exit-all')

output.must_match %r{edit_pokemon GET /pokemon/edit}
output.wont_match %r{edit_beer}
_(output).must_match %r{edit_pokemon GET /pokemon/edit}
_(output).wont_match %r{edit_beer}
end
end