Class: InertiaRails::Renderer
- Inherits:
-
Object
- Object
- InertiaRails::Renderer
- Defined in:
- lib/inertia_rails/renderer.rb
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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/inertia_rails/renderer.rb', line 15 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) @request = request @response = response @render_method = render_method @view_data = .fetch(:view_data, {}) @encrypt_history = .fetch(:encrypt_history, @configuration.encrypt_history) @clear_history = .fetch(:clear_history, controller.session[:inertia_clear_history] || false) @preserve_fragment = .fetch(:preserve_fragment, controller.session[:inertia_preserve_fragment] || false) @layout_override = .fetch(:layout) { @configuration.layout } @ssr_cache = [:ssr_cache] deep_merge = .fetch(:deep_merge, @configuration.deep_merge_shared_data) passed_props = .fetch(:props, component.is_a?(Hash) ? component : @controller.__send__(:inertia_view_assigns)) shared = shared_data @shared_keys = @configuration.expose_shared_prop_keys ? extract_shared_keys(shared) : nil @props = merge_props(shared, passed_props, deep_merge) @component = resolve_component(component) @controller.instance_variable_set('@_inertia_rendering', true) controller..add([:meta]) if [:meta] end |
Instance Method Details
#render ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/inertia_rails/renderer.rb', line 46 def render @response.headers['Vary'] = if @response.headers['Vary'].blank? 'X-Inertia' else "#{@response.headers['Vary']}, X-Inertia" end if @request.inertia? @response.set_header('X-Inertia', 'true') @render_method.call json: page.to_json, status: @response.status, content_type: Mime[:json] else ssr = @configuration.ssr_enabled && ssr_render if ssr @controller.instance_variable_set('@_inertia_ssr_head', ssr['head'].join.html_safe) @render_method.call( html: ssr['body'].html_safe, layout: layout, locals: @view_data.merge(page: page), formats: :html ) else @controller.instance_variable_set('@_inertia_page', page) @render_method.call( template: 'inertia', layout: layout, locals: @view_data.merge(page: page), formats: :html ) end end end |