Module: Matestack::Ui::Core::Helper

Defined in:
lib/matestack/ui/core/helper.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/matestack/ui/core/helper.rb', line 6

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#render(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/matestack/ui/core/helper.rb', line 21

def render(*args)
  setup_context
  if args.first.is_a?(Class) && args.first.ancestors.include?(Base)
    raise 'expected a hash as second argument' unless args.second.is_a?(Hash) || args.second.nil?

    begin
      controller_layout = self.class.send(:_layout)
    rescue
      controller_layout = nil
    end

    options = args.second || {}
    layout = options.delete(:matestack_layout) || self.class.matestack_layout
    page = args.first

    if controller_layout == false
      root_layout = layout ? layout.layout : false
    else
      # when using the turbo-rails gem, controller_layout is a Proc
      # https://github.com/hotwired/turbo-rails/blob/v1.0.1/app/controllers/turbo/frames/frame_request.rb#L16
      # and not nil or a string indicating which layout to be used like before
      if controller_layout.nil? || controller_layout.is_a?(Proc)
        root_layout = "application"
      else
        root_layout = controller_layout
      end
    end

    if layout && params[:only_page].nil? && params[:component_key].nil? && params[:component_class].nil?
      render_layout layout, page, options, root_layout
    else
      if params[:component_key] && params[:component_class].nil?
        render_component layout, page, params[:component_key], options
      elsif params[:component_class]
        if params[:component_key]
          render_component nil, params[:component_class].constantize, params[:component_key], JSON.parse(params[:public_options] || '{}')
        else
          render html: params[:component_class].constantize.(public_options: JSON.parse(params[:public_options] || '{}'))
        end
      else
        if params[:only_page]
          render_page page, options, false
        else
          render_page page, options, root_layout
        end
      end
    end
  else
    super
  end
end

#render_component(layout, page, component_key, options) ⇒ Object



81
82
83
84
# File 'lib/matestack/ui/core/helper.rb', line 81

def render_component(layout, page, component_key, options)
  layout ? layout.new(options) { page.new(options) } : page.new(options) # create page structure in order to later access registered async components
  render html: Matestack::Ui::Core::Context.async_components[component_key].render_content.html_safe, layout: false
end

#render_layout(layout, page, options, root_layout) ⇒ Object



73
74
75
# File 'lib/matestack/ui/core/helper.rb', line 73

def render_layout(layout, page, options, root_layout)
  render html: layout.new(options) { page.new(options) }.render_content.html_safe, layout: root_layout
end

#render_page(page, options, root_layout) ⇒ Object



77
78
79
# File 'lib/matestack/ui/core/helper.rb', line 77

def render_page(page, options, root_layout)
  render html: page.new(options).render_content.html_safe, layout: root_layout
end

#setup_contextObject



86
87
88
89
# File 'lib/matestack/ui/core/helper.rb', line 86

def setup_context
  Matestack::Ui::Core::Context.params = self.params
  Matestack::Ui::Core::Context.controller = (self.class <= ActionController::Base) ? self : @_controller
end