diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index 3f3411017..469c26066 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -18,6 +18,10 @@ nav_order: 5
*Tomasz Kowalewski*
+* Register stats directories with `Rails::CodeStatistics.register_directory` to support `rails stats` in Rails 8.
+
+ *Petrik de Heus*
+
## 3.14.0
* Defer to built-in caching for language environment setup, rather than manually using `actions/cache` in CI.
diff --git a/docs/index.md b/docs/index.md
index 7ee5b460b..309d7e564 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -183,6 +183,7 @@ ViewComponent is built by over a hundred members of the community, including:
+
diff --git a/lib/view_component/engine.rb b/lib/view_component/engine.rb
index 8932ad800..2e822f767 100644
--- a/lib/view_component/engine.rb
+++ b/lib/view_component/engine.rb
@@ -8,8 +8,16 @@ module ViewComponent
class Engine < Rails::Engine # :nodoc:
config.view_component = ViewComponent::Config.current
- rake_tasks do
- load "view_component/rails/tasks/view_component.rake"
+ if Rails.version.to_f < 8.0
+ rake_tasks do
+ load "view_component/rails/tasks/view_component.rake"
+ end
+ else
+ initializer "view_component.stats_directories" do |app|
+ require "rails/code_statistics"
+ dir = ViewComponent::Base.view_component_path
+ Rails::CodeStatistics.register_directory("ViewComponents", dir)
+ end
end
initializer "view_component.set_configs" do |app|
diff --git a/test/sandbox/test/rake_tasks_test.rb b/test/sandbox/test/rake_tasks_test.rb
index f486eff18..35d493226 100644
--- a/test/sandbox/test/rake_tasks_test.rb
+++ b/test/sandbox/test/rake_tasks_test.rb
@@ -2,17 +2,19 @@
require "test_helper"
-module ViewComponent
- class RakeTasksTest < TestCase
- def setup
- Kernel.silence_warnings do
- Sandbox::Application.load_tasks
+if Rails.version.to_f < 8.0
+ module ViewComponent
+ class RakeTasksTest < TestCase
+ def setup
+ Kernel.silence_warnings do
+ Sandbox::Application.load_tasks
+ end
end
- end
- def test_statsetup_task
- Rake::Task["view_component:statsetup"].invoke
- assert_includes ::STATS_DIRECTORIES, ["ViewComponents", "app/components"]
+ def test_statsetup_task
+ Rake::Task["view_component:statsetup"].invoke
+ assert_includes ::STATS_DIRECTORIES, ["ViewComponents", "app/components"]
+ end
end
end
end