Module: Line::Message::Builder::Flex::HasPartial

Included in:
Box, Bubble, Carousel
Defined in:
lib/line/message/builder/flex/partial.rb

Overview

The HasPartial module provides functionality for Flex Message components (like Box, Bubble) to render reusable "partials". Partials are defined by creating classes that inherit from Partial.

Including this module into a component class gives it the partial! method.

Example

# Define a reusable partial
class MyButtonPartial < Line::Message::Builder::Flex::Partial
def call
  # +button+ method is available from the context (e.g., a Box)
  button style: :primary do
    # +data+ and +label+ are available from <code>context.assigns</code>
    postback data, label: label
  end
end
end

# Use the partial within a Box component
Line::Message::Builder.with do
flex do
  bubble do
    body do
      partial! MyButtonPartial, label: "Action", data: "action=do_something"
    end
  end
end
end

See also:

  • Partial

Instance Method Summary collapse

Instance Method Details

#partial!(partial_class, **assigns) ⇒ Object

Renders a given Partial class within the current component's context.

This method temporarily makes the provided assigns available to the partial through the context.assigns mechanism. After the partial's call method completes, the original context.assigns are restored.

[partial_class] The class of the partial to render. Must be a subclass of Partial. [assigns] A hash of key-value pairs that will be made available as assigns within the partial's call method.

Raises ArgumentError if partial_class is not a subclass of Partial.



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/line/message/builder/flex/partial.rb', line 53

def partial!(partial_class, **assigns)
  unless partial_class < Partial
    raise ArgumentError,
          "Argument must be a Line::Message::Builder::Flex::Partial class"
  end

  original_assigns = context.assigns
  context.assigns = assigns
  # `self` here is the component instance (e.g., Box, Bubble) that includes HasPartial.
  # This component instance becomes the `@context` for the Partial instance.
  partial_class.new(context: self).call
  context.assigns = original_assigns
end