Class: Matestack::Ui::Core::Base

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, Properties, TagHelper
Defined in:
lib/matestack/ui/core/base.rb

Direct Known Subclasses

Component, Layout, Page

Constant Summary

Constants included from TagHelper

TagHelper::TAGS, TagHelper::VOID_TAGS

Instance Attribute Summary collapse

Attributes included from Slots

#slots

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TagHelper

#a, #detached, #detached_to_s, #heading, #img, #plain, #rails_render, #unescape

Methods included from Slots

#slot

Methods included from Properties

#context, #create_context, #create_context_for_properties, included, #optional_property_keys, #required_property_keys, #set_text

Constructor Details

#initialize(html_tag = nil, text = nil, options = {}, &block) ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/matestack/ui/core/base.rb', line 14

def initialize(html_tag = nil, text = nil, options = {}, &block)
  return unless render?

  if options && options[:detach_from_parent] == true
    self.bind_to_parent = false
  else
    self.bind_to_parent = ([:without_parent].include?(html_tag) ? false : true)
  end
  self.slots = self.options.delete(:slots) if self.options
  # extract_options(text, options) is called in properties
  self.html_tag = html_tag if self.bind_to_parent
  self.escape = self.options.delete(:escape) || true
  self.parent = Matestack::Ui::Core::Context.parent
  self.parent.children << self if self.parent if self.bind_to_parent
  self.prepare
  Matestack::Ui::Core::Context.parent = self
  create_children(&block)
  Matestack::Ui::Core::Context.parent = self.parent
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/matestack/ui/core/base.rb', line 107

def method_missing(name, *args, **kwargs, &block)
  if view_context && view_context.respond_to?(name, true)
    view_context_response = view_context.send(name, *args, **kwargs, &block)
    return view_context_response
  end
  if Rails.application.routes.url_helpers.respond_to?(name, true)
    return Rails.application.routes.url_helpers.send(name, *args, &block)
  end
  return raise NameError, "#{name} is not defined for #{self.class}", caller
end

Instance Attribute Details

#bind_to_parentObject

Returns the value of attribute bind_to_parent.



12
13
14
# File 'lib/matestack/ui/core/base.rb', line 12

def bind_to_parent
  @bind_to_parent
end

#escapeObject

Returns the value of attribute escape.



12
13
14
# File 'lib/matestack/ui/core/base.rb', line 12

def escape
  @escape
end

#html_tagObject

Returns the value of attribute html_tag.



12
13
14
# File 'lib/matestack/ui/core/base.rb', line 12

def html_tag
  @html_tag
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/matestack/ui/core/base.rb', line 12

def options
  @options
end

#parentObject

Returns the value of attribute parent.



12
13
14
# File 'lib/matestack/ui/core/base.rb', line 12

def parent
  @parent
end

#textObject

Returns the value of attribute text.



12
13
14
# File 'lib/matestack/ui/core/base.rb', line 12

def text
  @text
end

Class Method Details

.call(text = nil, options = {}, &block) ⇒ Object



71
72
73
# File 'lib/matestack/ui/core/base.rb', line 71

def self.call(text = nil, options = {}, &block)
  self.new(nil, text, options, &block).render_content
end

Instance Method Details

#childrenObject



75
76
77
# File 'lib/matestack/ui/core/base.rb', line 75

def children
  @children ||= []
end

#create_children(&block) ⇒ Object

create child items by either running the response method if exists or executing the block overwrite if needed (like in pages or apps)



63
64
65
66
67
68
69
# File 'lib/matestack/ui/core/base.rb', line 63

def create_children(&block)
  if respond_to?(:response)
    self.response &block
  else
    block.call if block_given?
  end
end

#extract_options(text, options) ⇒ Object

check if text is given and set text and options accordingly



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/matestack/ui/core/base.rb', line 42

def extract_options(text, options)
  if text.is_a? Hash
    # we need to dup the text object because we're deleting keys from this object which manipulates the object passed in here
    # if this object is reused after beeing injected into this component, the keys would be missing
    self.options = text.dup
  else
    self.text = text
    # we need to dup the options object because we're deleting keys from this object which manipulates the object passed in here
    # if this object is reused after beeing injected into this component, the keys would be missing
    self.options = options.dup || {}
  end
  self.options.symbolize_keys!
end

#paramsObject



95
96
97
# File 'lib/matestack/ui/core/base.rb', line 95

def params
  Matestack::Ui::Core::Context.params || ActionController::Parameters.new({})
end

#prepareObject



56
57
58
59
# File 'lib/matestack/ui/core/base.rb', line 56

def prepare
  # can be optionally overwritten in subclass in order to set
  # instance vars for example, might get deprecated in the future
end

#render?Boolean

can be optionally overwritten in subclass in order to conditionally render the component

Returns:

  • (Boolean)


37
38
39
# File 'lib/matestack/ui/core/base.rb', line 37

def render?
  true
end

#render_contentObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/matestack/ui/core/base.rb', line 79

def render_content
  if children.empty?
    child_content = self.escape ? ERB::Util.html_escape(text) : text if text
  else
    # using "\n" in order to preserve the 1.x rendering behavior which impacts appearance in browser
    child_content = (children.map { |child| " \n " + child.render_content }.join + " \n ").html_safe
  end
  result = ''
  if self.html_tag
    result = tag.public_send(self.html_tag, child_content, **self.options || {})
  elsif child_content
    result = child_content
  end
  result
end

#to_strObject Also known as: to_s



118
119
120
# File 'lib/matestack/ui/core/base.rb', line 118

def to_str
  render_content
end

#view_contextObject



99
100
101
102
103
104
105
# File 'lib/matestack/ui/core/base.rb', line 99

def view_context
  if Matestack::Ui::Core::Context.controller.nil?
    Matestack::Ui::Core::Context.controller = ActionController::Base.new
  else
    Matestack::Ui::Core::Context.controller&.view_context
  end
end