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



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/rails_pulse/tags_helper.rb', line 8

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, class: "badge") }, " ")
end