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.

Parameters:

  • object (Object)

    object from which to remove the tag

  • tag_name (String)

    tag to add to the 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.

Parameters:

  • object (Object)

    object on which to add the tag

  • tag_name (String)

    tag to add to the object.


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.

Parameters:

  • taggable_type (String)

    the type (class name) of the object.

  • taggable_id (String)

    the BSON ID of the object.

  • tag_name (String)

    the name of the tag to add.

Returns:

  • (Boolean)

    true if successful, false if not


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.

Parameters:

  • taggable_type (String)

    the type (class name) of the object.

  • taggable_id (String)

    the BSON ID of the object.

  • tag_name (String)

    the name of the tag to add.


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.

Parameters:

  • object (Object)

    object from which to remove the tag

  • tag_name (String)

    tag to remove from the object.

Returns:

  • (Boolean)

    true if successful, false if not


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.

Parameters:

  • object (Object)

    object from which to remove the tag

  • tag_name (String)

    tag to remove from the object.


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.

Parameters:

  • taggable_type (String)

    the type (class name) of the object.

  • taggable_id (String)

    the BSON ID of the object.

  • tag_name (String)

    the name of the tag to remove.

Returns:

  • (Boolean)

    true if successful, false if not


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.

Parameters:

  • taggable_type (String)

    the type (class name) of the object.

  • taggable_id (String)

    the BSON ID of the object.

  • tag_name (String)

    the name of the tag to remove.

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