Class: ReactOnRails::RenderRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails/render_request.rb

Overview

Structured data object encapsulating everything needed for a server render. Replaces the ad-hoc parameter passing of js_code strings and render_options through the delegation chain.

Part of the strategy pattern refactoring (see issue #2905). Currently additive — not yet wired into the main rendering path.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component_name:, props:, rails_context:, store_initializations:, render_options:) ⇒ RenderRequest

Returns a new instance of RenderRequest.

Parameters:

  • component_name (String)

    React component name

  • props (Hash, String)

    Component props (Hash or JSON string)

  • rails_context (Hash)

    Rails context data for the render

  • store_initializations (String)

    JavaScript code for Redux store initialization

  • render_options (ReactOnRails::ReactComponent::RenderOptions)

    Render configuration



19
20
21
22
23
24
25
# File 'lib/react_on_rails/render_request.rb', line 19

def initialize(component_name:, props:, rails_context:, store_initializations:, render_options:)
  @component_name = component_name
  @props = props
  @rails_context = rails_context
  @store_initializations = store_initializations
  @render_options = render_options
end

Instance Attribute Details

#component_nameObject (readonly)

Returns the value of attribute component_name.



11
12
13
# File 'lib/react_on_rails/render_request.rb', line 11

def component_name
  @component_name
end

#propsObject (readonly)

Returns the value of attribute props.



11
12
13
# File 'lib/react_on_rails/render_request.rb', line 11

def props
  @props
end

#rails_contextObject (readonly)

Returns the value of attribute rails_context.



11
12
13
# File 'lib/react_on_rails/render_request.rb', line 11

def rails_context
  @rails_context
end

#render_optionsObject (readonly)

Returns the value of attribute render_options.



11
12
13
# File 'lib/react_on_rails/render_request.rb', line 11

def render_options
  @render_options
end

#store_initializationsObject (readonly)

Returns the value of attribute store_initializations.



11
12
13
# File 'lib/react_on_rails/render_request.rb', line 11

def store_initializations
  @store_initializations
end

Instance Method Details

#dom_idObject



47
48
49
# File 'lib/react_on_rails/render_request.rb', line 47

def dom_id
  render_options.dom_id
end

#props_stringObject

Returns props as a JSON string, with unicode line/paragraph separators escaped for safe embedding in JavaScript.



36
37
38
39
# File 'lib/react_on_rails/render_request.rb', line 36

def props_string
  json = props.is_a?(String) ? props : props.to_json
  json.gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')
end

#rails_context_jsonObject

Raises:

  • (ArgumentError)


41
42
43
44
45
# File 'lib/react_on_rails/render_request.rb', line 41

def rails_context_json
  raise ArgumentError, "rails_context must be a Hash, got #{rails_context.class}" unless rails_context.is_a?(Hash)

  rails_context.to_json.gsub("\u2028", '\u2028').gsub("\u2029", '\u2029')
end

#rsc_payload_streaming?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/react_on_rails/render_request.rb', line 55

def rsc_payload_streaming?
  render_options.rsc_payload_streaming?
end

#streaming?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/react_on_rails/render_request.rb', line 51

def streaming?
  render_options.streaming?
end

#to_jsObject

Serialize to JS code via configured builder. Raises if server_bundle_js_file is not configured when prerender is true.



29
30
31
32
# File 'lib/react_on_rails/render_request.rb', line 29

def to_js
  validate_server_bundle_configured!
  ReactOnRails.js_code_builder.build(self)
end