Class: AdditionalTag

Inherits:
Object
  • Object
show all
Defined in:
app/models/additional_tag.rb

Constant Summary collapse

GROUP_SEP =
':'
SCOPE_SEP =
'::'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, disable_grouping: false, color_theme: nil, bg_color: nil) ⇒ AdditionalTag

NOTE: only use bg_color parameter, if background color should not

calculated by AdditionalTag - if you want to assign manual color


21
22
23
24
25
26
# File 'app/models/additional_tag.rb', line 21

def initialize(name:, disable_grouping: false, color_theme: nil, bg_color: nil)
  @tag_name = name.to_s
  @disable_grouping = disable_grouping
  @color_theme = color_theme.to_s
  @bg_color = bg_color
end

Class Method Details

.valid_mutually_exclusive_tag(tag_list) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'app/models/additional_tag.rb', line 8

def valid_mutually_exclusive_tag(tag_list)
  return true if tag_list.blank?

  tags = tag_list.select { |t| t.include? SCOPE_SEP }
  return true if tags.blank?

  groups = tags.map { |t| new(name: t).group_name }
  groups == groups.uniq
end

Instance Method Details

#group_labelsObject



75
76
77
# File 'app/models/additional_tag.rb', line 75

def group_labels
  @group_labels ||= @tag_name.split(GROUP_SEP).map(&:strip)
end

#group_nameObject



79
80
81
82
83
84
85
# File 'app/models/additional_tag.rb', line 79

def group_name
  if labels.length > 2
    labels[0...-1].join sep
  else
    labels.first
  end
end

#group_valueObject



87
88
89
# File 'app/models/additional_tag.rb', line 87

def group_value
  labels.last
end

#grouped?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/additional_tag.rb', line 95

def grouped?
  !@disable_grouping && group_labels.length > 1
end

#labelsObject



67
68
69
# File 'app/models/additional_tag.rb', line 67

def labels
  @labels ||= scoped? ? scope_labels : group_labels
end

#name_for_colorObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/additional_tag.rb', line 28

def name_for_color
  # different colors for non-grouped, grouped and scoped tag
  name = if scoped? || grouped?
           "#{group_name}#{sep}"
         else
           tag_name
         end

  if @color_theme.present? && @color_theme != '0' && @color_theme != '1'
    "#{name}#{@color_theme}"
  else
    name
  end
end

#scope_labelsObject



71
72
73
# File 'app/models/additional_tag.rb', line 71

def scope_labels
  @scope_labels ||= @tag_name.split(SCOPE_SEP).map(&:strip)
end

#scoped?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/additional_tag.rb', line 91

def scoped?
  !@disable_grouping && scope_labels.length > 1
end

#sepObject



59
60
61
# File 'app/models/additional_tag.rb', line 59

def sep
  scoped? ? SCOPE_SEP : GROUP_SEP
end

#tag_bg_colorObject



43
44
45
# File 'app/models/additional_tag.rb', line 43

def tag_bg_color
  @tag_bg_color ||= @bg_color || "##{Digest::SHA256.hexdigest(name_for_color)[0..5]}"
end

#tag_fg_colorObject



50
51
52
53
54
55
56
57
# File 'app/models/additional_tag.rb', line 50

def tag_fg_color
  @tag_fg_color ||= begin
    r = tag_bg_color[1..2].hex
    g = tag_bg_color[3..4].hex
    b = tag_bg_color[5..6].hex
    (r * 299 + g * 587 + b * 114) >= 128_000 ? 'black' : 'white'
  end
end

#tag_nameObject



63
64
65
# File 'app/models/additional_tag.rb', line 63

def tag_name
  scoped? ? group_name : @tag_name
end