diff --git a/lib/generators/administrate/routes/routes_generator.rb b/lib/generators/administrate/routes/routes_generator.rb index e352f0d0d5..b467f49eb7 100644 --- a/lib/generators/administrate/routes/routes_generator.rb +++ b/lib/generators/administrate/routes/routes_generator.rb @@ -7,6 +7,7 @@ require "rails/generators/base" require "administrate/generator_helpers" require "administrate/namespace" +require "generators/administrate/test_record" module Administrate module Generators @@ -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 diff --git a/lib/generators/administrate/test_record.rb b/lib/generators/administrate/test_record.rb new file mode 100644 index 0000000000..e8aa497ae2 --- /dev/null +++ b/lib/generators/administrate/test_record.rb @@ -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 + diff --git a/spec/generators/dashboard_generator_spec.rb b/spec/generators/dashboard_generator_spec.rb index f9cc1933bc..924c939e09 100644 --- a/spec/generators/dashboard_generator_spec.rb +++ b/spec/generators/dashboard_generator_spec.rb @@ -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| @@ -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 @@ -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 @@ -72,7 +73,7 @@ class Foo < ApplicationRecord end end - class Foo < ApplicationRecord + class Foo < Administrate::Generators::TestRecord reset_column_information end @@ -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 @@ -124,7 +125,7 @@ class Foo < ApplicationRecord end end - class InventoryItem < ApplicationRecord + class InventoryItem < Administrate::Generators::TestRecord reset_column_information end @@ -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 @@ -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 @@ -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 @@ -220,7 +221,7 @@ class User < ApplicationRecord end end - class Event < ApplicationRecord + class Event < Administrate::Generators::TestRecord reset_column_information end @@ -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 @@ -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 @@ -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 @@ -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 @@ -342,7 +343,7 @@ class Account < ApplicationRecord end end - class Foo < ApplicationRecord + class Foo < Administrate::Generators::TestRecord reset_column_information end @@ -376,7 +377,7 @@ def table_attribute_limit end end - class Foo < ApplicationRecord + class Foo < Administrate::Generators::TestRecord reset_column_information end @@ -402,7 +403,7 @@ class Foo < ApplicationRecord end end - class Foo < ApplicationRecord + class Foo < Administrate::Generators::TestRecord reset_column_information end @@ -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") @@ -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 diff --git a/spec/generators/routes_generator_spec.rb b/spec/generators/routes_generator_spec.rb index baa74e4cd3..ef0b5fe019 100644 --- a/spec/generators/routes_generator_spec.rb +++ b/spec/generators/routes_generator_spec.rb @@ -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