Class: AdditionalTag
- Inherits:
-
Object
- Object
- AdditionalTag
- Defined in:
- app/models/additional_tag.rb
Constant Summary collapse
- GROUP_SEP =
':'
- SCOPE_SEP =
'::'
Class Method Summary collapse
Instance Method Summary collapse
- #group_labels ⇒ Object
- #group_name ⇒ Object
- #group_value ⇒ Object
- #grouped? ⇒ Boolean
-
#initialize(name:, disable_grouping: false, color_theme: nil, bg_color: nil) ⇒ AdditionalTag
constructor
NOTE: only use bg_color parameter, if background color should not calculated by AdditionalTag - if you want to assign manual color.
- #labels ⇒ Object
- #name_for_color ⇒ Object
- #scope_labels ⇒ Object
- #scoped? ⇒ Boolean
- #sep ⇒ Object
- #tag_bg_color ⇒ Object
-
#tag_fg_color ⇒ Object
calculate contrast text color according to YIQ method 24ways.org/2010/calculating-color-contrast/ stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color.
- #tag_name ⇒ Object
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? = tag_list.select { |t| t.include? SCOPE_SEP } return true if .blank? groups = .map { |t| new(name: t).group_name } groups == groups.uniq end |
Instance Method Details
#group_labels ⇒ Object
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_name ⇒ Object
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_value ⇒ Object
87 88 89 |
# File 'app/models/additional_tag.rb', line 87 def group_value labels.last end |
#grouped? ⇒ Boolean
95 96 97 |
# File 'app/models/additional_tag.rb', line 95 def grouped? !@disable_grouping && group_labels.length > 1 end |
#labels ⇒ Object
67 68 69 |
# File 'app/models/additional_tag.rb', line 67 def labels @labels ||= scoped? ? scope_labels : group_labels end |
#name_for_color ⇒ Object
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_labels ⇒ Object
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
91 92 93 |
# File 'app/models/additional_tag.rb', line 91 def scoped? !@disable_grouping && scope_labels.length > 1 end |
#sep ⇒ Object
59 60 61 |
# File 'app/models/additional_tag.rb', line 59 def sep scoped? ? SCOPE_SEP : GROUP_SEP end |
#tag_bg_color ⇒ Object
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_color ⇒ Object
calculate contrast text color according to YIQ method 24ways.org/2010/calculating-color-contrast/ stackoverflow.com/questions/3942878/how-to-decide-font-color-in-white-or-black-depending-on-background-color
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_name ⇒ Object
63 64 65 |
# File 'app/models/additional_tag.rb', line 63 def tag_name scoped? ? group_name : @tag_name end |