Module: Pundit::Cache

Defined in:
lib/pundit/cache.rb,
lib/pundit/cache/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Instance Method Details

#cache(method_name) ⇒ Object

the policy result for a given user and record should not change during a request



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/pundit/cache.rb', line 7

def cache(method_name)
  wrapped_name = "_uncached_#{method_name}"
  alias_method wrapped_name, method_name
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{method_name}
      cache_key = "\#{user&.to_global_id}|\#{record&.to_global_id}|#{method_name}"
      if RequestStore.store[cache_key].nil?
        RequestStore.store[cache_key] = #{wrapped_name}
      else
        RequestStore.store[cache_key]
      end
    end
  RUBY
end