Sha256: 9648324a87b6859fadfb31ec2f74bf7c3bfedf4d3132567ad27c335548e8d45c

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require "react_on_rails/utils"

module ReactOnRails
  module ReactComponent
    class RenderOptions
      include Utils::Required

      attr_accessor :request_digest

      NO_PROPS = {}.freeze

      def initialize(react_component_name: required("react_component_name"), options: required("options"))
        @react_component_name = react_component_name.camelize
        @options = options
      end

      attr_reader :react_component_name

      def props
        options.fetch(:props) { NO_PROPS }
      end

      def random_dom_id
        retrieve_key(:random_dom_id)
      end

      def dom_id
        @dom_id ||= options.fetch(:id) do
          if random_dom_id
            generate_unique_dom_id
          else
            base_dom_id
          end
        end
      end

      def has_random_dom_id?
        return false if options[:id]

        return false unless random_dom_id

        true
      end

      def html_options
        options[:html_options].to_h
      end

      def prerender
        retrieve_key(:prerender)
      end

      def trace
        retrieve_key(:trace)
      end

      def replay_console
        retrieve_key(:replay_console)
      end

      def raise_on_prerender_error
        retrieve_key(:raise_on_prerender_error)
      end

      def logging_on_server
        retrieve_key(:logging_on_server)
      end

      def to_s
        "{ react_component_name = #{react_component_name}, options = #{options}, request_digest = #{request_digest}"
      end

      private

      attr_reader :options

      def base_dom_id
       "#{react_component_name}-react-component"
      end

      def generate_unique_dom_id
        "#{base_dom_id}-#{SecureRandom.uuid}"
      end

      def retrieve_key(key)
        options.fetch(key) do
          ReactOnRails.configuration.public_send(key)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
react_on_rails-11.1.1 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.0 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.0.beta.1 lib/react_on_rails/react_component/render_options.rb