Module: ReactOnRailsPro::Cache::Revalidates

Extended by:
ActiveSupport::Concern
Defined in:
lib/react_on_rails_pro/cache/revalidates.rb,
sig/react_on_rails_pro/cache.rbs

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
old_author_id = previous_changes["author_id"]&.first
ReactOnRailsPro.revalidate_tag("author:#{old_author_id}") if old_author_id
end

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.includedvoid

This method returns an undefined value.

Parameters:

  • base (Module)


60
# File 'sig/react_on_rails_pro/cache.rbs', line 60

def self.included: (Module base) -> void

Instance Method Details

#revalidate_react_on_rails_cache_tagsvoid

This method returns an undefined value.



83
84
85
86
87
88
89
90
# File 'lib/react_on_rails_pro/cache/revalidates.rb', line 83

def revalidate_react_on_rails_cache_tags
  resolver = self.class._react_on_rails_cache_tags_resolver
  tags = resolver ? resolver.call(self) : self
  # Do not replace with Array(tags): it calls to_ary, which would expand
  # array-like cache-key objects (for example Relation-like tags) into
  # their elements before TagIndex can use the object's cache_key.
  ReactOnRailsPro.revalidate_tags(*(tags.is_a?(Array) ? tags : [tags]))
end