Class: Inertia::RequestContext

Inherits:
Object
  • Object
show all
Defined in:
lib/inertia/request_context.rb

Overview

Encapsulates request-specific context for Inertia partial reloads.

Instance Method Summary collapse

Constructor Details

#initialize(request, component:) ⇒ RequestContext

Creates a new request context.

Parameters:

  • request (Rage::Request)

    the HTTP request object

  • component (String)

    the Inertia component name being rendered



14
15
16
17
# File 'lib/inertia/request_context.rb', line 14

def initialize(request, component:)
  @request = request
  @component = component
end

Instance Method Details

#once_prop_excluded?(prop_name) ⇒ Boolean

Checks if a once prop should be excluded based on the client's cached props.

Parameters:

  • prop_name (String)

    the cache key of the once prop

Returns:

  • (Boolean)

    true if the client already has this prop cached



57
58
59
# File 'lib/inertia/request_context.rb', line 57

def once_prop_excluded?(prop_name)
  except_once.include?(prop_name)
end

#partial_render?Boolean

Checks if this is a partial reload request for the current component.

Returns:

  • (Boolean)

    true if the client requested a partial reload for this component



63
64
65
# File 'lib/inertia/request_context.rb', line 63

def partial_render?
  partial_component == @component
end

#prop_status(prop_name) ⇒ Symbol

Determines the inclusion status of a prop based on partial reload headers.

Parameters:

  • prop_name (String)

    the full path of the prop (e.g., "user.profile")

Returns:

  • (Symbol)

    the prop status:

    • :unspecified - no partial reload filtering applies
    • :requested - prop is explicitly requested in partial reload
    • :excluded - prop is excluded from partial reload


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/inertia/request_context.rb', line 32

def prop_status(prop_name)
  return :unspecified unless partial_render?

  is_excluded = partial_except.any? do |except_prop_name|
    prop_name == except_prop_name ||
      (prop_name.start_with?(except_prop_name) && prop_name.start_with?("#{except_prop_name}."))
  end

  return :excluded if is_excluded
  return :requested if partial_only.empty? && partial_except.any?
  return :unspecified if partial_only.empty?

  is_included_in_partial_reload = partial_only.any? do |only_prop_name|
    prop_name == only_prop_name ||
      (only_prop_name.start_with?(prop_name) && only_prop_name.start_with?("#{prop_name}.")) ||
      (prop_name.start_with?(only_prop_name) && prop_name.start_with?("#{only_prop_name}."))
  end

  is_included_in_partial_reload ? :requested : :excluded
end

#urlString

Returns the full request path including query string.

Returns:

  • (String)

    the request URL path



21
22
23
# File 'lib/inertia/request_context.rb', line 21

def url
  @request.fullpath
end