Module: TypedViewModel

Defined in:
lib/typed_view_model/with_cache_key.rb,
lib/typed_view_model.rb,
lib/typed_view_model/base.rb,
lib/typed_view_model/trait.rb,
lib/typed_view_model/version.rb,
lib/typed_view_model/hashed_key.rb,
lib/typed_view_model/job_helpers.rb,
lib/typed_view_model/cache_key_error.rb,
lib/typed_view_model/controller_helpers.rb,
lib/typed_view_model/helpers/tag_helpers.rb,
lib/typed_view_model/helpers/url_helpers.rb,
lib/typed_view_model/helpers/i18n_helpers.rb,
lib/typed_view_model/helpers/link_helpers.rb,
lib/typed_view_model/helpers/path_helpers.rb,
lib/typed_view_model/helpers/text_helpers.rb,
lib/typed_view_model/helpers/format_helpers.rb,
lib/typed_view_model/test_support/mock_object.rb,
lib/typed_view_model/job_helpers/active_storage_urls.rb,
lib/typed_view_model/test_support/trait_test_harness.rb,
lib/typed_view_model/test_support/trait_test_helpers.rb,
lib/generators/typed_view_model/install/install_generator.rb

Overview

Rails fragment caching works by either expecting the cached key object to respond to ‘cache_key` or for that object to be an array or hash. Here we add a default `cache_key` implementation for classes like presenters that opt in via `with_cache_key`.

Defined Under Namespace

Modules: ControllerHelpers, Generators, HashedKey, Helpers, JobHelpers, TestSupport, Trait, WithCacheKey Classes: Base, CacheKeyError

Constant Summary collapse

HELPERS_STORAGE_KEY =

Storage backend for the per-request view-context stash. Uses ActiveSupport::IsolatedExecutionState when available (Rails 7+) so the stash is fiber-safe and follows Rails’ own request-isolation model. Falls back to Thread.current otherwise.

:typed_view_model_helpers
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.current_helpersObject



26
27
28
# File 'lib/typed_view_model.rb', line 26

def current_helpers
  helpers_storage[HELPERS_STORAGE_KEY]
end

.current_helpers=(value) ⇒ Object



30
31
32
# File 'lib/typed_view_model.rb', line 30

def current_helpers=(value)
  helpers_storage[HELPERS_STORAGE_KEY] = value
end

.helper_namespacesObject

Set of namespaces searched by ‘Base.helpers(*names)`. Accepts Module references or String constant names; Strings are resolved lazily so Zeitwerk can autoload them.

TypedViewModel.helper_namespaces << MyApp::ViewModelHelpers
TypedViewModel.helper_namespaces << "MyApp::ViewModelHelpers"


22
23
24
# File 'lib/typed_view_model.rb', line 22

def helper_namespaces
  @helper_namespaces ||= ::Set.new([TypedViewModel::Helpers])
end

.helpers_storageObject



10
11
12
13
14
15
16
# File 'lib/typed_view_model/base.rb', line 10

def self.helpers_storage
  if defined?(::ActiveSupport::IsolatedExecutionState)
    ::ActiveSupport::IsolatedExecutionState
  else
    ::Thread.current
  end
end

.with_current_helpers(value) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/typed_view_model.rb', line 34

def with_current_helpers(value)
  previous = current_helpers
  self.current_helpers = value
  yield
ensure
  self.current_helpers = previous
end