Module: RailsPulse::TagsHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/rails_pulse/tags_helper.rb

Instance Method Summary collapse

Instance Method Details

#display_tag_badges(tags) ⇒ Object

Display tags as badge elements Accepts:

  • Taggable objects (with tag_list method)

  • Raw JSON strings from aggregated queries

  • Arrays of tags



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/helpers/rails_pulse/tags_helper.rb', line 46

def display_tag_badges(tags)
  tag_array = case tags
  when String
    # Parse JSON string from database
    begin
      JSON.parse(tags)
    rescue JSON::ParserError
      []
    end
  when Array
    tags
  else
    # Handle Taggable objects
    tags.respond_to?(:tag_list) ? tags.tag_list : []
  end

  return (:span, "-", class: "text-subtle") if tag_array.empty?

  safe_join(tag_array.map { |tag| (:div, tag.humanize, class: "badge") }, " ")
end

#render_tag_badge(tag, variant: :default, removable: false, taggable_type: nil, taggable_id: nil) ⇒ Object

Render a single tag badge Options:

  • variant: :default (no class), :secondary, :positive

  • removable: boolean - whether to include a remove button

  • taggable_type: string - type of taggable object (for remove button)

  • taggable_id: integer - id of taggable object (for remove button)



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/rails_pulse/tags_helper.rb', line 9

def render_tag_badge(tag, variant: :default, removable: false, taggable_type: nil, taggable_id: nil)
  badge_class = case variant
  when :secondary
    "badge badge--secondary font-normal"
  when :positive
    "badge badge--positive font-normal"
  else
    "badge font-normal"
  end

  if removable && taggable_type && taggable_id
    # For removable tags, render the full structure with button_to
    (:span, class: badge_class) do
      concat tag.humanize
      concat " "
      concat(
        button_to(
          remove_tag_path(taggable_type, taggable_id, tag: tag),
          method: :delete,
          class: "tag-remove",
          data: { turbo_frame: "_top" }
        ) do
          (:span, "×", "aria-hidden": "true")
        end
      )
    end
  else
    # For non-removable tags, just render the badge
    (:span, tag, class: badge_class)
  end
end