Module: Taggable
- Included in:
- Artefact, CuratedList
- Defined in:
- app/traits/taggable.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#set_primary_tag_of_type(tag_type, tag_id) ⇒ Object
The primary tag is simply the first one of its type.
- #set_tags_of_type(collection_name, tag_ids) ⇒ Object
- #tags(include_draft = false) ⇒ Object
- #tags=(new_tag_tuples) ⇒ Object
- #tags_of_type(tag_type, include_draft = false) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'app/traits/taggable.rb', line 50 def self.included(klass) klass.extend ClassMethods klass.field :tag_ids, type: Array, default: [] klass.field :tags, type: Array, default: [] klass.index :tag_ids klass.attr_protected :tags, :tag_ids klass.__send__ :private, :tag_ids= end |
Instance Method Details
#set_primary_tag_of_type(tag_type, tag_id) ⇒ Object
The primary tag is simply the first one of its type. If that tag is already applied this method moves it to the start of the list. If it’s not then we add it at the start of the list.
77 78 79 80 81 82 83 84 85 86 |
# File 'app/traits/taggable.rb', line 77 def set_primary_tag_of_type(tag_type, tag_id) Tag.validate_tag_ids([tag_id], tag_type) tag_tuple = {'tag_id' => tag_id, 'tag_type' => tag_type} = attributes['tags'].dup .delete(tag_tuple) self. = .unshift(tag_tuple) end |
#set_tags_of_type(collection_name, tag_ids) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'app/traits/taggable.rb', line 60 def (collection_name, tag_ids) tag_type = collection_name.singularize tag_ids = Array(tag_ids) Tag.validate_tag_ids(tag_ids, tag_type) = attributes['tags'].reject {|t| t['tag_type'] == tag_type } self. = + tag_ids.map {|tag_id| {'tag_id' => tag_id, 'tag_type' => tag_type} } end |
#tags(include_draft = false) ⇒ Object
97 98 99 |
# File 'app/traits/taggable.rb', line 97 def (include_draft = false) Tag.by_tag_ids(tag_ids, draft: include_draft) end |
#tags=(new_tag_tuples) ⇒ Object
92 93 94 95 |
# File 'app/traits/taggable.rb', line 92 def (new_tag_tuples) self.tag_ids = new_tag_tuples.map {|tuple| tuple['tag_id'] } super(new_tag_tuples) end |
#tags_of_type(tag_type, include_draft = false) ⇒ Object
88 89 90 |
# File 'app/traits/taggable.rb', line 88 def (tag_type, include_draft = false) (include_draft).select { |t| t.tag_type == tag_type } end |