feat(views): replace chapters sidebar with ViewComponent#2523
feat(views): replace chapters sidebar with ViewComponent#2523mroderick merged 2 commits intocodebar:masterfrom
Conversation
|
|
||
| class ChaptersSidebarComponent < ViewComponent::Base | ||
| def initialize(chapters:, title: nil) | ||
| super() |
There was a problem hiding this comment.
didn't it work without this one?
| require 'rails_helper' | ||
| require 'view_component/test_helpers' | ||
|
|
||
| RSpec.describe ChaptersSidebarComponent, type: :component do |
There was a problem hiding this comment.
# Connect ViewComponent to RSpec, adding RSpec metadata type: :component.
#
# This extends Rails' own built-in (e.g. "type: :controller") metadata system.
RSpec::Rails::DIRECTORY_MAPPINGS[:component] = %w[ spec components ]Something like this, in the rails_helper would auto-add the type: :component by file location.
in RSpec.config we'd need:
# Automatically tag specs in conventional directories with matching type
# metadata so that they have relevant helpers available to them.
# See RSpec::Rails::DIRECTORY_MAPPINGS for details on which metadata is
# applied to each directory.
config.infer_spec_type_from_file_location!- Add ChaptersSidebarComponent with Rails fragment caching - Add cache expiration callbacks to Chapter model - Create component and view specs - Fix broken use_transactional_fixtures check in spec_helper.rb
|
Thank you for the review @olleolleolle! I've addressed both of your suggestions:
Appreciate the careful review! |
985d5e3 to
15b191e
Compare
…ping for components Based on review feedback from olleolleolle: - Remove unnecessary super() call from ViewComponent initialize - Add RSpec::Rails::DIRECTORY_MAPPINGS to auto-infer type: :component - Remove explicit type: :component from component spec (now inferred)
15b191e to
1070aed
Compare
olleolleolle
left a comment
There was a problem hiding this comment.
Thank you for introducing and using ViewComponent, it can be pretty handy.
Summary
cache "chapters-sidebar") for performanceafter_create_commit,after_update_commit,after_destroy_commit)use_transactional_fixturescheck in spec_helper.rbCaching Strategy
How it works
The component uses Rails fragment caching to cache the rendered HTML for the chapters sidebar:
This caches the entire sidebar HTML fragment under the key
"chapters-sidebar". On subsequent requests, Rails serves the cached fragment directly from the cache store (typically Redis in production), bypassing view rendering entirely.Cache Invalidation
The cache is automatically invalidated when Chapters are created, updated, or deleted via Active Record callbacks in
Chaptermodel:Using
*_commitcallbacks (rather than*_save/*_destroy) ensures the cache is only expired after the database transaction successfully commits, preventing stale reads.Trade-offs
Pros:
Cons:
Alternatives considered
For the current scale of ~10 chapters, a single cached fragment is the simplest effective solution.
Changes
app/components/chapters_sidebar_component.rb- New ViewComponentapp/components/chapters_sidebar_component.html.erb- ERB template with cachingapp/models/chapter.rb- Added cache expiration callbacksapp/views/dashboard/show.html.haml- Use component instead of inline HAMLspec/components/chapters_sidebar_component_spec.rb- Component testsspec/views/dashboard/show.html.haml_spec.rb- View integration testsspec/models/chapter_spec.rb- Cache expiration testsspec/rails_helper.rb- ViewComponent test configurationGemfile- Added view_component gem