Module: Tailmix::ViewHelpers

Defined in:
lib/tailmix/view_helpers.rb

Overview

Provides helper methods for rendering Tailmix component definitions and managing Tailmix-related data attributes in view templates.

Instance Method Summary collapse

Instance Method Details

#tailmix_definitions_tagObject

Renders a script tag containing the definitions for all unique Tailmix components used on the current page.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tailmix/view_helpers.rb', line 10

def tailmix_definitions_tag
  definitions = Tailmix::Registry.instance.definitions
  return if definitions.empty?

  json_payload = definitions.to_json

  tag.script(
    type: "application/json",
    "data-tailmix-definitions": "true"
  ) do
    json_payload.html_safe
  end
end

#tailmix_trigger_for(target_id, action_name, options = {}) ⇒ Hash

Generates a hash of attributes for an external trigger that will control a named component instance.

Parameters:

  • target_id (String, Symbol)

    Target component ID.

  • action_name (String, Symbol)

    The name of the action to be called.

  • options (Hash) (defaults to: {})

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tailmix/view_helpers.rb', line 30

def tailmix_trigger_for(target_id, action_name, options = {})
  # target_id = options.fetch(:target_id)
  # action_name = options.fetch(:action_name)

  event_name = options.fetch(:event_name, :click)
  with = options.fetch(:with, nil)

  attributes = {
    "data-tailmix-trigger-for" => target_id.to_s,
    "data-tailmix-action" => "#{event_name}->#{action_name}"
  }

  if with
    attributes["data-tailmix-action-payload"] = with.to_json
  end

  attributes
end