class Matestack::Ui::Core::Base

Attributes

bind_to_parent[RW]
escape[RW]
html_tag[RW]
options[RW]
parent[RW]
text[RW]

Public Class Methods

call(text = nil, options = {}, &block) click to toggle source
# File lib/matestack/ui/core/base.rb, line 67
def self.call(text = nil, options = {}, &block)
  self.new(nil, text, options, &block).render_content
end
new(html_tag = nil, text = nil, options = {}, &block) click to toggle source
# File lib/matestack/ui/core/base.rb, line 14
def initialize(html_tag = nil, text = nil, options = {}, &block)
  return unless render?

  self.bind_to_parent = ([:without_parent].include?(html_tag) ? false : true)
  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

Public Instance Methods

children() click to toggle source
# File lib/matestack/ui/core/base.rb, line 71
def children
  @children ||= []
end
create_children(&block) click to toggle source

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

# File lib/matestack/ui/core/base.rb, line 59
def create_children(&block)
  if respond_to?(:response)
    self.response &block
  else
    block.call if block_given?
  end
end
extract_options(text, options) click to toggle source

check if text is given and set text and options accordingly

# File lib/matestack/ui/core/base.rb, line 38
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
method_missing(name, *args, &block) click to toggle source
# File lib/matestack/ui/core/base.rb, line 103
def method_missing(name, *args, &block)
  return view_context.send(name, *args, &block) if view_context && view_context.respond_to?(name, true)
  return Rails.application.routes.url_helpers.send(name, *args, &block) if Rails.application.routes.url_helpers.respond_to?(name, true)
  return raise NameError, "#{name} is not defined for #{self.class}", caller
end
params() click to toggle source
# File lib/matestack/ui/core/base.rb, line 91
def params
  Matestack::Ui::Core::Context.params || ActionController::Parameters.new({})
end
prepare() click to toggle source
# File lib/matestack/ui/core/base.rb, line 52
def prepare
  # can be optionally overwritten in subclass in order to set
  # instance vars for example, might get deprecated in the future
end
render?() click to toggle source

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

# File lib/matestack/ui/core/base.rb, line 33
def render?
  true
end
render_content() click to toggle source
# File lib/matestack/ui/core/base.rb, line 75
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
view_context() click to toggle source
# File lib/matestack/ui/core/base.rb, line 95
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
vue() click to toggle source

give easy access to vue data attributes

# File lib/matestack/ui/core/base.rb, line 110
def vue
  Matestack::Ui::Core::VueAttributes
end