Class: Tag
Defined Under Namespace
Classes: MissingTags
Constant Summary collapse
- GOVSPEAK_FIELDS =
[]
- STATES =
['draft', 'live']
Instance Attribute Summary collapse
-
#uniquely_named ⇒ Object
This doesn’t get set automatically: the code that loads tags should go through them and set this attribute manually.
Class Method Summary collapse
- .by_tag_id(tag_id, tag_type_or_options = nil) ⇒ Object
- .by_tag_ids(tag_id_list, tag_type_or_options = nil) ⇒ Object
- .by_tag_types_and_ids(tag_types_and_ids) ⇒ Object
-
.validate_tag_ids(tag_id_list, tag_type = nil) ⇒ Object
Validate a list of tags by tag ID.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #has_parent? ⇒ Boolean
- #parent ⇒ Object
- #to_s ⇒ Object
- #unique_title ⇒ Object
Instance Attribute Details
#uniquely_named ⇒ Object
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, = nil) by_tag_ids([tag_id], ).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, = nil) if .is_a?(String) # Providing the type as a string argument is deprecated in favour of providing the type as an option = {type: } else = || {} end tag_scope = [:type] ? Tag.where(tag_type: [:type]) : Tag unless [: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 = tag_scope.each_with_object({}) do |tag, hash| hash[tag.tag_id] = tag end tag_id_list.map { |tag_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.
111 112 113 114 115 |
# File 'app/models/tag.rb', line 111 def self.validate_tag_ids(tag_id_list, tag_type = nil) = by_tag_ids(tag_id_list, type: tag_type, draft: true) missing_tag_ids = tag_id_list - .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( = {}) { id: self.tag_id, title: self.title, type: self.tag_type, description: self.description, short_description: self.short_description } end |
#has_parent? ⇒ Boolean
61 62 63 |
# File 'app/models/tag.rb', line 61 def has_parent? parent_id.present? end |
#parent ⇒ Object
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_s ⇒ Object
73 74 75 |
# File 'app/models/tag.rb', line 73 def to_s title end |
#unique_title ⇒ Object
69 70 71 |
# File 'app/models/tag.rb', line 69 def unique_title self.uniquely_named ? self.title : "#{self.title} [#{self.tag_id}]" end |