Module: CategoryBadge

Defined in:
lib/category_badge.rb

Class Method Summary collapse

Class Method Details

.fetch_parent_category(category) ⇒ Object



28
29
30
# File 'lib/category_badge.rb', line 28

def self.fetch_parent_category(category)
  Category.find_by(id: category.parent_category_id) if category.parent_category_id
end

.html_for(category, opts = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/category_badge.rb', line 4

def self.html_for(category, opts = nil)
  opts ||= {}

  # Bail if there is no category, hide uncategorized by default
  return "" if category.blank? || (category.uncategorized? && !opts[:show_uncategorized])

  if opts[:inline_style]
    # Inline styles for email
    style_for_email(category, opts)
  else
    # Browser styles
    style_for_browser(category, opts)
  end
end

.map_styles_to_string(styles) ⇒ Object



32
33
34
# File 'lib/category_badge.rb', line 32

def self.map_styles_to_string(styles)
  styles.map { |k, v| "#{k}: #{ERB::Util.html_escape(v)};" }.join(" ")
end

.shared_data(category, opts) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/category_badge.rb', line 19

def self.shared_data(category, opts)
  {
    parent_category: fetch_parent_category(category),
    category_url:
      opts[:absolute_url] ? "#{Discourse.base_url_no_prefix}#{category.url}" : category.url,
    extra_classes: opts[:extra_classes].to_s,
  }
end

.style_for_browser(category, opts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/category_badge.rb', line 41

def self.style_for_browser(category, opts)
  data = shared_data(category, opts)

  class_names = "badge-category #{data[:parent_category] ? "--has-parent" : ""}"
  description = category.description_text ? "title='#{category.description_text}'" : ""

  badge_styles = {
    "--category-badge-color": "##{category.color}",
    "--category-badge-text-color": "##{category.text_color}",
  }
  badge_styles["--parent-category-badge-color"] = "##{data[:parent_category].color}" if data[
    :parent_category
  ]

  result = +""
  result << "<span data-category-id='#{category.id}'"
  result << " style='#{map_styles_to_string(badge_styles)}'"
  result << " data-parent-category-id='#{data[:parent_category].id}'" if data[:parent_category]
  result << " data-drop-close='true' class='#{class_names}' #{description}>"
  result << "<span class='badge-category__name'>"
  result << ERB::Util.html_escape(category.name)
  result << "</span></span>"

  wrap_in_link(result, data[:category_url], data[:extra_classes])
end

.style_for_email(category, opts) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/category_badge.rb', line 67

def self.style_for_email(category, opts)
  data = shared_data(category, opts)

  badge_styles = {
    display: "inline-block",
    width: "0.72em",
    height: "0.72em",
    "margin-right": "0.33em",
    "background-color": "##{category.color}",
  }

  result = +""
  result << "<span data-category-id='#{category.id}'"
  result << " data-parent-category-id='#{data[:parent_category].id}'" if data[:parent_category]
  result << " data-drop-close='true'>"
  result << "<span>"
  result << "<span style='#{map_styles_to_string(badge_styles)}'>"
  if data[:parent_category]
    parent_badge_styles = { display: "block", width: "0.36em", height: "0.72em" }
    parent_badge_styles["background-color"] = "##{data[:parent_category].color}"
    parent_badge_style_value = map_styles_to_string(parent_badge_styles)
    result << "<span style='#{parent_badge_style_value}'></span>"
  end
  result << "</span>"
  result << ERB::Util.html_escape(category.name)
  result << "</span></span>"

  wrap_in_link(result, data[:category_url])
end


36
37
38
39
# File 'lib/category_badge.rb', line 36

def self.wrap_in_link(content, url, extra_classes = "", style_value = nil)
  style_attr = style_value ? " style='#{style_value}'" : ""
  "<a class='badge-category__wrapper #{extra_classes}' href='#{url}'#{style_attr}>#{content}</a>".html_safe
end