Sha256: dc7af865668aa72e0c98fa0b8526bf0f78580e7127e595bb8be8f26c78360769

Contents?: true

Size: 1.51 KB

Versions: 7

Compression:

Stored size: 1.51 KB

Contents

module ReactiveRuby
  module Rails
    class ComponentMount < React::Rails::ComponentMount
      attr_accessor :controller

      def setup(controller)
        self.controller = controller
      end

      def react_component(name, props = {}, options = {}, &block)
        if options[:prerender] || [:on, 'on', true].include?(Hyperloop.prerendering)
          options = context_initializer_options(options, name)
        end
        props = serialized_props(props, name, controller)
        super(top_level_name, props, options, &block).gsub("\n","")
          .gsub(/(<script>.*<\/script>)<\/div>$/,'</div>\1').html_safe +
          footers
      end

      private

      def context_initializer_options(options, name)
        options[:prerender] = {options[:prerender] => true} unless options[:prerender].is_a? Hash
        existing_context_initializer = options[:prerender][:context_initializer]

        options[:prerender][:context_initializer] = lambda do |ctx|
          React::IsomorphicHelpers.load_context(ctx, controller, name)
          existing_context_initializer.call(ctx) if existing_context_initializer
        end

        options
      end

      def serialized_props(props, name, controller)
        { render_params: props, component_name: name,
          controller: controller.class.name.gsub(/Controller$/,"") }.react_serializer
      end

      def top_level_name
        'React.TopLevelRailsComponent'
      end

      def footers
        React::IsomorphicHelpers.prerender_footers(controller)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hyper-react-1.0.0.lap27 lib/reactive-ruby/rails/component_mount.rb
hyper-react-1.0.0.lap26 lib/reactive-ruby/rails/component_mount.rb
hyper-react-1.0.0.lap25 lib/reactive-ruby/rails/component_mount.rb
hyper-react-1.0.0.lap24 lib/reactive-ruby/rails/component_mount.rb
hyper-react-1.0.0.lap23 lib/reactive-ruby/rails/component_mount.rb
hyper-react-1.0.0.lap22 lib/reactive-ruby/rails/component_mount.rb
hyper-react-1.0.0.lap21 lib/reactive-ruby/rails/component_mount.rb