Skip to content

Commit

Permalink
Register stats directories with Rails::CodeStatistics.register_direct…
Browse files Browse the repository at this point in the history
…ory (#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 <joel@hawksley.org>
Co-authored-by: Joel Hawksley <joelhawksley@github.com>
  • Loading branch information
3 people committed Aug 27, 2024
1 parent d2d1247 commit 598750e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://github.com/avatars/nielsslot?s=64" alt="nshki" width="32" />
<img src="https://github.com/avatars/nshki?s=64" alt="nshki" width="32" />
<img src="https://github.com/avatars/ozydingo?s=64" alt="ozydingo" width="32" />
<img src="https://github.com/avatars/p8?s=64" alt="p8" width="32" />
<img src="https://github.com/avatars/patrickarnett?s=64" alt="patrickarnett" width="32" />
<img src="https://github.com/avatars/rainerborene?s=64" alt="rainerborene" width="32" />
<img src="https://github.com/avatars/rdavid1099?s=64" alt="rdavid1099" width="32" />
Expand Down
12 changes: 10 additions & 2 deletions lib/view_component/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
20 changes: 11 additions & 9 deletions test/sandbox/test/rake_tasks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 598750e

Please sign in to comment.