Class: NicePartials::Partial::Section
- Defined in:
- lib/nice_partials/partial/section.rb
Defined Under Namespace
Classes: Empty, RequiredError
Instance Attribute Summary
Attributes inherited from Content
Instance Method Summary collapse
-
#method_missing(meth, *arguments, **keywords, &block) ⇒ Object
Implements our proxying to the ‘@view_context` or `@view_context.tag`.
-
#optional ⇒ Object
Returns self if ‘present?`, or returns a Null object that won’t output any content.
- #present? ⇒ Boolean
-
#required ⇒ Object
Returns self if ‘present?`, or raises.
- #respond_to_missing? ⇒ Boolean
- #yield(*arguments) ⇒ Object
Methods inherited from Content
Constructor Details
This class inherits a constructor from NicePartials::Partial::Content
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *arguments, **keywords, &block) ⇒ Object
Implements our proxying to the ‘@view_context` or `@view_context.tag`.
‘@view_context` proxying forwards the message and automatically appends any content, so you can do things like:
<% partial.body.render "form", tangible_thing: @tangible_thing %>
<% partial.body.link_to @document.name, @document %>
<% partial.body.t ".body" %>
‘@view_context.tag` proxy lets you build bespoke elements based on content and options provided:
<% partial.title "Some title content", class: "xl" %> # Write the content and options to the `title`
<% partial.title.h2 ", appended" %> # => <h2 class="xl">Some title content, appended</h2>
Note that NicePartials don’t support deep merging attributes out of the box.
62 63 64 65 66 67 68 |
# File 'lib/nice_partials/partial/section.rb', line 62 def method_missing(meth, *arguments, **keywords, &block) if meth != :p && @view_context.respond_to?(meth) append @view_context.public_send(meth, *arguments, **keywords, &block) else @view_context.tag.public_send(meth, @content + arguments.first.to_s, **.merge(keywords), &block) end end |
Instance Method Details
#optional ⇒ Object
Returns self if ‘present?`, or returns a Null object that won’t output any content. Useful to declare optional content sections, that you also don’t want to print any HTML elements for.
<%= partial.title.optional.div class: "text-xl" %> # => "" # Won't output an empty `<div>` that can mess with HTML markups.
33 34 35 |
# File 'lib/nice_partials/partial/section.rb', line 33 def optional present? ? self : Empty.new(self) end |
#present? ⇒ Boolean
42 43 44 |
# File 'lib/nice_partials/partial/section.rb', line 42 def present? chunks.present? || super end |
#required ⇒ Object
Returns self if ‘present?`, or raises. Useful to declare content that you require to be supplied during a `render` call.
<%= partial.title.required.div class: "text-xl" %>
25 26 27 |
# File 'lib/nice_partials/partial/section.rb', line 25 def required present? ? self : raise(RequiredError, "Section expected to have content, but wasn't supplied during render") end |
#respond_to_missing? ⇒ Boolean
70 71 72 |
# File 'lib/nice_partials/partial/section.rb', line 70 def respond_to_missing?(...) @view_context.respond_to?(...) end |
#yield(*arguments) ⇒ Object
37 38 39 40 |
# File 'lib/nice_partials/partial/section.rb', line 37 def yield(*arguments) chunks.each { append @view_context.capture(*arguments, &_1) } self end |