Sha256: 70bff0679afcd061bc7be036282370ddb164416bd4d35c0750f37501017a6e5d

Contents?: true

Size: 1.02 KB

Versions: 106

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

require "webpacker/react/railtie" if defined?(Rails)
require "webpacker/react/helpers"
require "webpacker/react/component"

module Playbook
  module PbKitHelper
    def pb_rails(kit, props: {}, &block)
      previous = prefix_partial_path_with_controller_namespace
      self.prefix_partial_path_with_controller_namespace = false
      kit = build_view_model(kit.to_s, props, &block)
      render(partial: kit, as: :object)
    ensure
      self.prefix_partial_path_with_controller_namespace = previous
    end

    def pb_react(kit, props: {}, options: {})
      ::Webpacker::React::Component.new(kit.camelize).render(props, options)
    end

  private

    def is_subkit?(kit)
      kit.match(%r{[/\\]})
    end

    def build_view_model(kit, props, &block)
      folder = is_subkit?(kit) ? kit.split("/")[0] : kit
      item = is_subkit?(kit) ? kit.split("/")[-1] : kit
      class_name = "Playbook::Pb#{folder.camelize}::#{item.camelize}"
      class_name.safe_constantize.new(props, &block)
    end
  end
end

Version data entries

106 entries across 106 versions & 1 rubygems

Version Path
playbook_ui-6.6.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.6.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.5.2 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.5.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.5.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.4.2 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.4.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.4.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.3.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.3.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.2.1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.2.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.1.0.pre.alpha5 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.1.0.pre.alpha4 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.1.0.pre.alpha3 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.1.0.pre.alpha2 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.1.0.pre.alpha1 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.1.0 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.0.1.pre.alpha6 app/helpers/playbook/pb_kit_helper.rb
playbook_ui-6.0.1.pre.alpha5 app/helpers/playbook/pb_kit_helper.rb