Class: Tag

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/tag.rb

Defined Under Namespace

Classes: MissingTags

Constant Summary collapse

GOVSPEAK_FIELDS =
[]
STATES =
['draft', 'live']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uniquely_namedObject

This doesn’t get set automatically: the code that loads tags should go through them and set this attribute manually



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

def uniquely_named
  @uniquely_named
end

Class Method Details

.by_tag_id(tag_id, tag_type_or_options = nil) ⇒ Object



77
78
79
# File 'app/models/tag.rb', line 77

def self.by_tag_id(tag_id, tag_type_or_options = nil)
  by_tag_ids([tag_id], tag_type_or_options).first
end

.by_tag_ids(tag_id_list, tag_type_or_options = nil) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/tag.rb', line 81

def self.by_tag_ids(tag_id_list, tag_type_or_options = nil)
  if tag_type_or_options.is_a?(String)
    # Providing the type as a string argument is deprecated in favour of providing the type as an option
    options = {type: tag_type_or_options}
  else
    options = tag_type_or_options || {}
  end

  tag_scope = options[:type] ? Tag.where(tag_type: options[:type]) : Tag

  unless options[:draft]
    tag_scope = tag_scope.where(:state.ne => 'draft')
  end

  tag_scope = tag_scope.any_in(tag_id: tag_id_list)

  # Sort by id list because MongoID 2.x doesn't preserve order
  tags_by_id = tag_scope.each_with_object({}) do |tag, hash|
    hash[tag.tag_id] = tag
  end
  tag_id_list.map { |tag_id| tags_by_id[tag_id] }.compact
end

.by_tag_types_and_ids(tag_types_and_ids) ⇒ Object



104
105
106
107
# File 'app/models/tag.rb', line 104

def self.by_tag_types_and_ids(tag_types_and_ids)
  list = tag_types_and_ids.map {|hash| hash.slice(:tag_id, :tag_type) }
  any_of(list)
end

.validate_tag_ids(tag_id_list, tag_type = nil) ⇒ Object

Validate a list of tags by tag ID. Any missing tags raise an exception. Draft tags are considered present for internal validation.

Raises:



111
112
113
114
115
# File 'app/models/tag.rb', line 111

def self.validate_tag_ids(tag_id_list, tag_type = nil)
  found_tags = by_tag_ids(tag_id_list, type: tag_type, draft: true)
  missing_tag_ids = tag_id_list - found_tags.map(&:tag_id)
  raise MissingTags.new(missing_tag_ids) if missing_tag_ids.any?
end

Instance Method Details

#as_json(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/models/tag.rb', line 51

def as_json(options = {})
  {
    id: self.tag_id,
    title: self.title,
    type: self.tag_type,
    description: self.description,
    short_description: self.short_description
  }
end

#has_parent?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/tag.rb', line 61

def has_parent?
  parent_id.present?
end

#parentObject



65
66
67
# File 'app/models/tag.rb', line 65

def parent
  Tag.by_tag_id(parent_id, type: self.tag_type, draft: true) if has_parent?
end

#to_sObject



73
74
75
# File 'app/models/tag.rb', line 73

def to_s
  title
end

#unique_titleObject



69
70
71
# File 'app/models/tag.rb', line 69

def unique_title
  self.uniquely_named ? self.title : "#{self.title} [#{self.tag_id}]"
end