Sha256: 0b58e053383259f752e61de5156a64cc46631c9243fe2200d58cd3e15551a363
Contents?: true
Size: 1.67 KB
Versions: 11
Compression:
Stored size: 1.67 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) render_rails(kit, props, &block) end def pb_react(kit, props: {}, options: {}) render_react(kit, props, options) end private def render_rails(kit, props, &block) props = defined?(props) && !props.nil? ? props : {} kit_class_obj = get_class_name(kit) original_value = ActionView::Base.prefix_partial_path_with_controller_namespace ActionView::Base.prefix_partial_path_with_controller_namespace = true render(partial: kit_class_obj.new(**props, &block), as: :object) rescue ActionView::MissingTemplate ActionView::Base.prefix_partial_path_with_controller_namespace = false render(partial: kit_class_obj.new(**props, &block), as: :object) ensure ActionView::Base.prefix_partial_path_with_controller_namespace = original_value end def render_react(kit, props, options) if defined?(props[:props]) && !props[:props].nil? props = props[:props] if props.keys[0] === :props end ::Webpacker::React::Component.new(kit.camelize).render(props, options) end def is_subkit?(kit) kit.match(%r{[/\\]}) end def pb_camelize(string) string.to_s.tr(" ", "_").camelize end def get_class_name(kit) folder = is_subkit?(kit) ? pb_camelize(kit.split("/")[0]) : pb_camelize(kit) item = is_subkit?(kit) ? pb_camelize(kit.split("/")[-1]) : pb_camelize(kit) "Playbook::Pb#{folder}::#{item}".safe_constantize end end end
Version data entries
11 entries across 11 versions & 1 rubygems