Class: MongoMapper::Tag

Inherits:
Object
  • Object
show all
Includes:
Document
Defined in:
lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb

Overview

Author:

  • Dan Guilak

Defined Under Namespace

Classes: TagNotFound

Class Method Summary collapse

Class Method Details

.add_by_object_and_tag(object, tag_name) ⇒ Object

Adds a tag on an object.


101
102
103
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 101

def add_by_object_and_tag(object, tag_name)
  add_by_type_and_id_and_tag(object.class.name, object.id, tag_name)
end

.add_by_object_and_tag!(object, tag_name) ⇒ Object

Adds a tag on an object. Raises if the tag wasn't successfully created.


110
111
112
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 110

def add_by_object_and_tag!(object, tag_name)
  add_by_type_and_id_and_tag!(object.class.name, object.id, tag_name)
end

.add_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name) ⇒ Boolean

Adds a tag by the type of the object, the id of the object, and the tag name.


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 62

def add_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name)
  find_args = {
    mongo_taggable_type: taggable_type,
    mongo_taggable_id: taggable_id,
    tag: tag_name,
  }

  # `create` will return the object if it finds it, rather than returning false.
  if first(find_args).present?
    return false
  end

  success = create(find_args)

  success.present?
end

.add_by_type_and_id_and_tag!(taggable_type, taggable_id, tag_name) ⇒ Object

Adds a tag by the type of the object, the id of the object, and the tag name. Raises an error if the tag wasn't created successfully.


52
53
54
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 52

def add_by_type_and_id_and_tag!(taggable_type, taggable_id, tag_name)
  create!(mongo_taggable_type: taggable_type, mongo_taggable_id: taggable_id, tag: tag_name)
end

.remove_by_object_and_tag(object, tag_name) ⇒ Boolean

Removes a tag on an object.


93
94
95
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 93

def remove_by_object_and_tag(object, tag_name)
  remove_by_type_and_id_and_tag(object.class.name, object.id, tag_name)
end

.remove_by_object_and_tag!(object, tag_name) ⇒ Object

Removes a tag on an object. Raises if the tag isn't found.


84
85
86
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 84

def remove_by_object_and_tag!(object, tag_name)
  remove_by_type_and_id_and_tag!(object.class.name, object.id, tag_name)
end

.remove_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name) ⇒ Boolean

Removes a tag by the type of the object, the id of the object, and the tag name.


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

def remove_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name)
  tag = first(mongo_taggable_type: taggable_type, mongo_taggable_id: taggable_id, tag: tag_name)

  return false unless tag.present?

  success = tag.destroy
  success && success["ok"] == 1
end

.remove_by_type_and_id_and_tag!(taggable_type, taggable_id, tag_name) ⇒ Object

Removes a tag by the type of the object, the id of the object, and the tag name. Raises an error if the tag wasn't found.

Raises:


25
26
27
28
29
# File 'lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb', line 25

def remove_by_type_and_id_and_tag!(taggable_type, taggable_id, tag_name)
  success = remove_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name)

  raise TagNotFound unless success
end