Skip to content

Commit d0b53aa

Browse files
authored
Merge pull request rails#43722 from Shopify/deliberate-railties-ordering
Explicitly order Railties by load order
2 parents 5fa7049 + 510d761 commit d0b53aa

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

railties/lib/rails/railtie.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class << self
146146
delegate :config, to: :instance
147147

148148
def subclasses
149-
super.reject(&:abstract_railtie?)
149+
super.reject(&:abstract_railtie?).sort
150150
end
151151

152152
def rake_tasks(&blk)
@@ -191,6 +191,23 @@ def configure(&block)
191191
instance.configure(&block)
192192
end
193193

194+
def <=>(other) # :nodoc:
195+
load_index <=> other.load_index
196+
end
197+
198+
def inherited(subclass)
199+
subclass.increment_load_index
200+
super
201+
end
202+
203+
protected
204+
attr_reader :load_index
205+
206+
def increment_load_index
207+
@@load_counter ||= 0
208+
@load_index = (@@load_counter += 1)
209+
end
210+
194211
private
195212
def generate_railtie_name(string)
196213
ActiveSupport::Inflector.underscore(string).tr("/", "_")

railties/test/railties/engine_test.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,28 @@ def bar
13121312
get("/assets/bar.js")
13131313
assert_match "// App's bar js", last_response.body.strip
13141314

1315-
# ensure that railties are not added twice
1316-
railties = Rails.application.send(:ordered_railties).map(&:class)
1317-
assert_equal railties, railties.uniq
1315+
assert_equal <<~EXPECTED, Rails.application.send(:ordered_railties).flatten.map(&:class).map(&:name).join("\n") << "\n"
1316+
I18n::Railtie
1317+
ActiveSupport::Railtie
1318+
ActionDispatch::Railtie
1319+
ActiveModel::Railtie
1320+
ActionController::Railtie
1321+
ActiveRecord::Railtie
1322+
GlobalID::Railtie
1323+
ActiveJob::Railtie
1324+
ActionMailer::Railtie
1325+
Rails::TestUnitRailtie
1326+
Sprockets::Railtie
1327+
ActionView::Railtie
1328+
ActiveStorage::Engine
1329+
ActionCable::Engine
1330+
ActionMailbox::Engine
1331+
ActionText::Engine
1332+
Bukkits::Engine
1333+
Importmap::Engine
1334+
AppTemplate::Application
1335+
Blog::Engine
1336+
EXPECTED
13181337
end
13191338

13201339
test "railties_order adds :all with lowest priority if not given" do

0 commit comments

Comments
 (0)