Class: InertiaRails::Renderer
- Inherits:
-
Object
- Object
- InertiaRails::Renderer
- Defined in:
- lib/inertia_rails/renderer.rb
Instance Attribute Summary collapse
-
#clear_history ⇒ Object
readonly
Returns the value of attribute clear_history.
-
#component ⇒ Object
readonly
Returns the value of attribute component.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#encrypt_history ⇒ Object
readonly
Returns the value of attribute encrypt_history.
-
#props ⇒ Object
readonly
Returns the value of attribute props.
-
#view_data ⇒ Object
readonly
Returns the value of attribute view_data.
Instance Method Summary collapse
-
#initialize(component, controller, request, response, render_method, **options) ⇒ Renderer
constructor
A new instance of Renderer.
- #render ⇒ Object
Constructor Details
#initialize(component, controller, request, response, render_method, **options) ⇒ Renderer
Returns a new instance of Renderer.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/inertia_rails/renderer.rb', line 19 def initialize(component, controller, request, response, render_method, **) if component.is_a?(Hash) && .key?(:props) raise ArgumentError, 'Parameter `props` is not allowed when passing a Hash as the first argument' end @controller = controller @configuration = controller.__send__(:inertia_configuration) @component = resolve_component(component) @request = request @response = response @render_method = render_method @props = .fetch(:props, component.is_a?(Hash) ? component : controller.__send__(:inertia_view_assigns)) @view_data = .fetch(:view_data, {}) @deep_merge = .fetch(:deep_merge, configuration.deep_merge_shared_data) @encrypt_history = .fetch(:encrypt_history, configuration.encrypt_history) @clear_history = .fetch(:clear_history, controller.session[:inertia_clear_history] || false) @controller.instance_variable_set('@_inertia_rendering', true) controller..add([:meta]) if [:meta] end |
Instance Attribute Details
#clear_history ⇒ Object (readonly)
Returns the value of attribute clear_history.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def clear_history @clear_history end |
#component ⇒ Object (readonly)
Returns the value of attribute component.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def component @component end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def configuration @configuration end |
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def controller @controller end |
#encrypt_history ⇒ Object (readonly)
Returns the value of attribute encrypt_history.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def encrypt_history @encrypt_history end |
#props ⇒ Object (readonly)
Returns the value of attribute props.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def props @props end |
#view_data ⇒ Object (readonly)
Returns the value of attribute view_data.
9 10 11 |
# File 'lib/inertia_rails/renderer.rb', line 9 def view_data @view_data end |
Instance Method Details
#render ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/inertia_rails/renderer.rb', line 40 def render @response.headers['Vary'] = if @response.headers['Vary'].blank? 'X-Inertia' else "#{@response.headers['Vary']}, X-Inertia" end if @request.headers['X-Inertia'] @response.set_header('X-Inertia', 'true') @render_method.call json: page.to_json, status: @response.status, content_type: Mime[:json] else begin return render_ssr if configuration.ssr_enabled rescue StandardError nil end controller.instance_variable_set('@_inertia_page', page) @render_method.call template: 'inertia', layout: layout, locals: view_data.merge(page: page) end end |