Class: Matestack::Ui::Core::Base
- Inherits:
-
Object
- Object
- Matestack::Ui::Core::Base
- Includes:
- ActionView::Helpers::TagHelper, Properties, TagHelper
- Defined in:
- lib/matestack/ui/core/base.rb
Constant Summary
Constants included from TagHelper
TagHelper::TAGS, TagHelper::VOID_TAGS
Instance Attribute Summary collapse
-
#bind_to_parent ⇒ Object
Returns the value of attribute bind_to_parent.
-
#escape ⇒ Object
Returns the value of attribute escape.
-
#html_tag ⇒ Object
Returns the value of attribute html_tag.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#text ⇒ Object
Returns the value of attribute text.
Attributes included from Slots
Class Method Summary collapse
Instance Method Summary collapse
- #children ⇒ Object
-
#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).
-
#extract_options(text, options) ⇒ Object
check if text is given and set text and options accordingly.
-
#initialize(html_tag = nil, text = nil, options = {}, &block) ⇒ Base
constructor
A new instance of Base.
- #method_missing(name, *args, **kwargs, &block) ⇒ Object
- #params ⇒ Object
- #prepare ⇒ Object
-
#render? ⇒ Boolean
can be optionally overwritten in subclass in order to conditionally render the component.
- #render_content ⇒ Object
- #to_str ⇒ Object (also: #to_s)
- #view_context ⇒ Object
Methods included from TagHelper
#a, #detached, #detached_to_s, #heading, #img, #plain, #rails_render, #unescape
Methods included from Slots
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, = {}, &block) return unless render? if && [: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..delete(:slots) if self. # extract_options(text, options) is called in properties self.html_tag = html_tag if self.bind_to_parent self.escape = self..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_parent ⇒ Object
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 |
#escape ⇒ Object
Returns the value of attribute escape.
12 13 14 |
# File 'lib/matestack/ui/core/base.rb', line 12 def escape @escape end |
#html_tag ⇒ Object
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 |
#options ⇒ Object
Returns the value of attribute options.
12 13 14 |
# File 'lib/matestack/ui/core/base.rb', line 12 def @options end |
#parent ⇒ Object
Returns the value of attribute parent.
12 13 14 |
# File 'lib/matestack/ui/core/base.rb', line 12 def parent @parent end |
#text ⇒ Object
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, = {}, &block) self.new(nil, text, , &block).render_content end |
Instance Method Details
#children ⇒ Object
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 (text, ) 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. = 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. = .dup || {} end self..symbolize_keys! end |
#params ⇒ Object
95 96 97 |
# File 'lib/matestack/ui/core/base.rb', line 95 def params Matestack::Ui::Core::Context.params || ActionController::Parameters.new({}) end |
#prepare ⇒ Object
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
37 38 39 |
# File 'lib/matestack/ui/core/base.rb', line 37 def render? true end |
#render_content ⇒ Object
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. || {}) elsif child_content result = child_content end result end |
#to_str ⇒ Object Also known as: to_s
118 119 120 |
# File 'lib/matestack/ui/core/base.rb', line 118 def to_str render_content end |