Module: MongoMapper::Plugins::MongoMapperTagger

Extended by:
ActiveSupport::Concern
Defined in:
lib/mongo_mapper/plugins/mongo_mapper_tagger.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_tag!(tag) ⇒ Object

Adds a tag to the object.



74
75
76
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger.rb', line 74

def add_tag!(tag)
  MongoMapper::Tag.add_by_object_and_tag!(self, tag)
end

#remove_tag!(tag) ⇒ Object

Removes a tag from the object.



67
68
69
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger.rb', line 67

def remove_tag!(tag)
  MongoMapper::Tag.remove_by_object_and_tag!(self, tag)
end

#tag_listArray

Returns an array of strings of the current tags on the object.



30
31
32
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger.rb', line 30

def tag_list
  tags.map(&:tag)
end

#tag_list=(new_tag_list) ⇒ Object

Sets the tags on the object using (see #update_tags_from_list!)



38
39
40
41
42
43
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger.rb', line 38

def tag_list=(new_tag_list)
  update_tags_from_list!(new_tag_list)

  # Have to reload the tags, otherwise the same ones get saved again.
  tags.reload
end

#update_tags_from_list!(new_tag_list, delimiter = ',') ⇒ Object

Takes a new list of tags as a delimited string, adds and removes tags on the object as necessary in order to get the object tags to match the new list.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger.rb', line 52

def update_tags_from_list!(new_tag_list, delimiter=',')
  old_tag_list = tag_list.to_set
  new_tag_list = new_tag_list.split(delimiter).to_set

  to_remove = (old_tag_list - new_tag_list).to_a
  to_add = (new_tag_list - old_tag_list).to_a

  to_remove.each { |tag| MongoMapper::Tag.remove_by_object_and_tag!(self, tag) }

  to_add.each { |tag| MongoMapper::Tag.add_by_object_and_tag!(self, tag) }
end