Sha256: f9366bf181aec63c560b93f4b47e914e838e7788591713f3ed1bd2d0c56fec97

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "jbuilder/jbuilder_template"

module InertiaFlow
  class Renderer
    attr_accessor :component, :options, :controller, :request, :response

    def initialize(component, options, controller, request, response)
      @component = component
      @options = options
      @controller = controller
      @request = request
      @response = response
    end

    def render
      props = jbuilder_attributes

      InertiaRails::Renderer.new(
        component,
        controller,
        request,
        response,
        render_method,
        props: props,
        view_data: options[:view_data]
      ).render
    end

    private

    def jbuilder_attributes
      jbuilder = JbuilderTemplate.new(view_context)

      # Render the Jbuilder template into the Jbuilder object
      view_context.render(template: template,
                          formats: [:inertia],
                          handlers: [:jbuilder],
                          locals: { json: jbuilder })

      # Get the Ruby hash from the Jbuilder object
      jbuilder.attributes!
    end

    def template
      "#{controller_path}/#{action_name}"
    end

    def render_method
      controller.method(:render)
    end

    def view_context
      controller.view_context
    end

    def controller_path
      controller.controller_path
    end

    def action_name
      controller.action_name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inertia_flow-0.1.0 lib/inertia_flow/renderer.rb