Module: InertiaRails::FlashExtension

Defined in:
lib/inertia_rails/flash_extension.rb

Overview

Provides a scoped interface for Inertia flash data within Rails’ flash. Uses native hash storage: flash = { key: value } Tracks .now keys separately in @inertia_now_keys for session filtering.

Defined Under Namespace

Classes: InertiaFlashScope

Constant Summary collapse

INERTIA_KEY =
'inertia'

Instance Method Summary collapse

Instance Method Details

#inertiaObject



10
11
12
# File 'lib/inertia_rails/flash_extension.rb', line 10

def inertia
  @inertia ||= InertiaFlashScope.new(self)
end

#inertia_now_keysObject

Keys set via flash.now.inertia that should not persist to session



15
16
17
# File 'lib/inertia_rails/flash_extension.rb', line 15

def inertia_now_keys
  @inertia_now_keys ||= Set.new
end

#keep(key = nil) ⇒ Object

Clear .now tracking when user explicitly keeps :inertia or all flash



20
21
22
23
# File 'lib/inertia_rails/flash_extension.rb', line 20

def keep(key = nil)
  @inertia_now_keys&.clear if key.nil? || key.to_s == INERTIA_KEY
  super
end

#to_session_valueObject

Override to filter .now keys from nested inertia hash before session persistence



26
27
28
29
30
31
32
33
34
# File 'lib/inertia_rails/flash_extension.rb', line 26

def to_session_value
  inertia_hash = self[INERTIA_KEY]
  if inertia_hash.is_a?(Hash) && @inertia_now_keys&.any?
    @inertia_now_keys.each { |k| inertia_hash.delete(k.to_s) }
    delete(INERTIA_KEY) if inertia_hash.empty?
  end

  super
end