Module: ReactOnRailsPro::Cache::Revalidates
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/react_on_rails_pro/cache/revalidates.rb
Overview
ActiveRecord concern that revalidates React on Rails Pro cache tags from the model write path, so the model that owns the data also owns cache invalidation.
class Post < ApplicationRecord
include ReactOnRailsPro::Cache::Revalidates
revalidates_react_cache # default tag: record.cache_key, e.g. "posts/42"
# or custom / additional tags:
# revalidates_react_cache { |post| ["post:#{post.id}", "author:#{post.author_id}"] }
end
Revalidation runs in an after_commit callback, so it never fires for a rolled-back transaction and fires only after the new data is visible to the request that re-renders. It covers create/update/destroy and touch (including ‘belongs_to …, touch: true` on associated records).
Callback caveat (the standard Rails one): update_column, update_all, delete_all, and other callback-skipping writes do not trigger revalidation. Call ReactOnRailsPro.revalidate_tags yourself after such writes.
Mutable custom tags caveat: the block runs in after_commit and sees only the record’s NEW values. If a custom tag derives from a mutable attribute (e.g. “author:#postpost.author_id” and the post changes author), the OLD grouping’s entries are not revalidated — they expire via :expires_in. Prefer tags derived from the record’s own immutable identity, or revalidate the old grouping explicitly (previous_changes in after_commit has the prior value):
after_commit do
= previous_changes["author_id"]&.first
ReactOnRailsPro.revalidate_tag("author:#{}") if
end