Skip to content

Commit

Permalink
Work around issue with Rails 7 + Ruby 3
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Apr 18, 2023
1 parent 4b0047e commit 5467e49
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
6 changes: 5 additions & 1 deletion lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require "rails/generators/base"
require "administrate/generator_helpers"
require "administrate/namespace"
require "generators/administrate/test_record"

module Administrate
module Generators
Expand Down Expand Up @@ -60,7 +61,10 @@ def valid_dashboard_models
end

def database_models
ActiveRecord::Base.descendants.reject(&:abstract_class?)
ActiveRecord::Base.descendants
.reject(&:abstract_class?)
.reject { |k| k < Administrate::Generators::TestRecord }
.sort_by(&:to_s)
end

def invalid_dashboard_models
Expand Down
22 changes: 22 additions & 0 deletions lib/generators/administrate/test_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Administrate
module Generators
# This class serves only to work around a strange behaviour in Rails 7
# with Ruby 3.
#
# After running the spec for DashboardGenerator, the fake models that
# it generates (eg: Foo, Shipment) linger around despite being removed
# explicitly. This causes RouteGenerator to take them into
# account and generate routes for them, which its spec doesn't expect,
# causing a spec failure.
#
# To avoid this, the spec for DashboardGenerator defines its fake models
# as children of TestRecord. Then RoutesGenerator explicitly filters
# child classes of TestRecord when figuring out what models exist.
#
# Discussion at https://github.com/thoughtbot/administrate/pull/2324
class TestRecord < ApplicationRecord
self.abstract_class = true
end
end
end

39 changes: 20 additions & 19 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "rails_helper"
require "generators/administrate/dashboard/dashboard_generator"
require "generators/administrate/test_record"

describe Administrate::Generators::DashboardGenerator, :generator do
around do |example|
Expand All @@ -25,7 +26,7 @@
create_table(:foos) { |t| t.timestamps null: false }
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -47,7 +48,7 @@ class Foo < ApplicationRecord
create_table(:foos) { |t| t.string :name }
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -72,7 +73,7 @@ class Foo < ApplicationRecord
end
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -92,7 +93,7 @@ class Foo < ApplicationRecord
create_table(:foos) { |t| t.inet :ip_address }
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand Down Expand Up @@ -124,7 +125,7 @@ class Foo < ApplicationRecord
end
end

class InventoryItem < ApplicationRecord
class InventoryItem < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -148,7 +149,7 @@ class InventoryItem < ApplicationRecord
end
end

class Shipment < ApplicationRecord
class Shipment < Administrate::Generators::TestRecord
enum status: %i[ready processing shipped]
reset_column_information
end
Expand All @@ -169,7 +170,7 @@ class Shipment < ApplicationRecord
end
end

class Shipment < ApplicationRecord
class Shipment < Administrate::Generators::TestRecord
enum status: %i[ready processing shipped]
reset_column_information
end
Expand All @@ -195,7 +196,7 @@ class Shipment < ApplicationRecord
create_table(:users) { |t| t.boolean :active }
end

class User < ApplicationRecord
class User < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -220,7 +221,7 @@ class User < ApplicationRecord
end
end

class Event < ApplicationRecord
class Event < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -241,7 +242,7 @@ class Event < ApplicationRecord
ActiveRecord::Schema.define do
create_table(:comments) { |t| t.references :post }
end
class Comment < ApplicationRecord
class Comment < Administrate::Generators::TestRecord
belongs_to :post
end

Expand All @@ -263,7 +264,7 @@ class Comment < ApplicationRecord
t.references :commentable, polymorphic: true
end
end
class Comment < ApplicationRecord
class Comment < Administrate::Generators::TestRecord
belongs_to :commentable, polymorphic: true
end

Expand All @@ -289,12 +290,12 @@ class Comment < ApplicationRecord
end
end

class Account < ApplicationRecord
class Account < Administrate::Generators::TestRecord
reset_column_information
has_one :profile
end

class Ticket < ApplicationRecord
class Ticket < Administrate::Generators::TestRecord
reset_column_information
belongs_to :account
end
Expand All @@ -316,7 +317,7 @@ class Ticket < ApplicationRecord
create_table :accounts
end

class Account < ApplicationRecord
class Account < Administrate::Generators::TestRecord
reset_column_information
attribute :tmp_attribute, :boolean
end
Expand All @@ -342,7 +343,7 @@ class Account < ApplicationRecord
end
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand Down Expand Up @@ -376,7 +377,7 @@ def table_attribute_limit
end
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand All @@ -402,7 +403,7 @@ class Foo < ApplicationRecord
end
end

class Foo < ApplicationRecord
class Foo < Administrate::Generators::TestRecord
reset_column_information
end

Expand Down Expand Up @@ -430,7 +431,7 @@ class Foo < ApplicationRecord
it "subclasses Admin::ApplicationController by default" do
begin
ActiveRecord::Schema.define { create_table :foos }
class Foo < ApplicationRecord; end
class Foo < Administrate::Generators::TestRecord; end

run_generator ["foo"]
load file("app/controllers/admin/foos_controller.rb")
Expand All @@ -446,7 +447,7 @@ class Foo < ApplicationRecord; end
it "uses the given namespace to create controllers" do
begin
ActiveRecord::Schema.define { create_table :foos }
class Foo < ApplicationRecord; end
class Foo < Administrate::Generators::TestRecord; end
module Manager
class ApplicationController < Administrate::ApplicationController; end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/generators/routes_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,6 @@ class ModelWithoutDBTable < ApplicationRecord; end

run_generator

expect(routes).to contain('root to: "customers#index')
expect(routes).to contain('root to: "countries#index')
end
end

0 comments on commit 5467e49

Please sign in to comment.