Sha256: 4cde4ed754d5e22fcb2176a18c2620c747ecef56764bd7f0fb53f2d08eb8946a

Contents?: true

Size: 1.85 KB

Versions: 9

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 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

9 entries across 9 versions & 1 rubygems

Version Path
react_on_rails-11.2.2 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.2.1 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.8 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.7 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.6 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.5 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.4 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.3 lib/react_on_rails/react_component/render_options.rb
react_on_rails-11.1.2 lib/react_on_rails/react_component/render_options.rb