Skip to content

Commit 598750e

Browse files
p8joelhawksley
andauthored
Register stats directories with Rails::CodeStatistics.register_directory (#2081)
* Register stats directories with Rails::CodeStatistics.register_directory. Rails `main` uses Thor for the `bin/rails stats` command instead of Rake. This means stats directories need to be added in the Railtie. The global constant STATS_DIRECTORIES defined by Rails, has been deprecated in favor of Rails::CodeStatistics.register_directory. https://github.com/rails/rails/blob/8c7754dfdf39ed94cc93bbc40ee721c311b6d32c/railties/CHANGELOG.md?plain=1#L1-L11 * add changelog * Update docs/CHANGELOG.md --------- Co-authored-by: Joel Hawksley <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent d2d1247 commit 598750e

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ nav_order: 5
1818

1919
*Tomasz Kowalewski*
2020

21+
* Register stats directories with `Rails::CodeStatistics.register_directory` to support `rails stats` in Rails 8.
22+
23+
*Petrik de Heus*
24+
2125
## 3.14.0
2226

2327
* Defer to built-in caching for language environment setup, rather than manually using `actions/cache` in CI.

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ ViewComponent is built by over a hundred members of the community, including:
183183
<img src="https://avatars.githubusercontent.com/nielsslot?s=64" alt="nshki" width="32" />
184184
<img src="https://avatars.githubusercontent.com/nshki?s=64" alt="nshki" width="32" />
185185
<img src="https://avatars.githubusercontent.com/ozydingo?s=64" alt="ozydingo" width="32" />
186+
<img src="https://avatars.githubusercontent.com/p8?s=64" alt="p8" width="32" />
186187
<img src="https://avatars.githubusercontent.com/patrickarnett?s=64" alt="patrickarnett" width="32" />
187188
<img src="https://avatars.githubusercontent.com/rainerborene?s=64" alt="rainerborene" width="32" />
188189
<img src="https://avatars.githubusercontent.com/rdavid1099?s=64" alt="rdavid1099" width="32" />

lib/view_component/engine.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,16 @@ module ViewComponent
88
class Engine < Rails::Engine # :nodoc:
99
config.view_component = ViewComponent::Config.current
1010

11-
rake_tasks do
12-
load "view_component/rails/tasks/view_component.rake"
11+
if Rails.version.to_f < 8.0
12+
rake_tasks do
13+
load "view_component/rails/tasks/view_component.rake"
14+
end
15+
else
16+
initializer "view_component.stats_directories" do |app|
17+
require "rails/code_statistics"
18+
dir = ViewComponent::Base.view_component_path
19+
Rails::CodeStatistics.register_directory("ViewComponents", dir)
20+
end
1321
end
1422

1523
initializer "view_component.set_configs" do |app|

test/sandbox/test/rake_tasks_test.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
require "test_helper"
44

5-
module ViewComponent
6-
class RakeTasksTest < TestCase
7-
def setup
8-
Kernel.silence_warnings do
9-
Sandbox::Application.load_tasks
5+
if Rails.version.to_f < 8.0
6+
module ViewComponent
7+
class RakeTasksTest < TestCase
8+
def setup
9+
Kernel.silence_warnings do
10+
Sandbox::Application.load_tasks
11+
end
1012
end
11-
end
1213

13-
def test_statsetup_task
14-
Rake::Task["view_component:statsetup"].invoke
15-
assert_includes ::STATS_DIRECTORIES, ["ViewComponents", "app/components"]
14+
def test_statsetup_task
15+
Rake::Task["view_component:statsetup"].invoke
16+
assert_includes ::STATS_DIRECTORIES, ["ViewComponents", "app/components"]
17+
end
1618
end
1719
end
1820
end

0 commit comments

Comments
 (0)