mongo_mapper_tagger 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f18a52122328c024458d2942c397475ff023fe3
4
+ data.tar.gz: 9ef72345a064a047f51433b8235e22e538fbc290
5
+ SHA512:
6
+ metadata.gz: 5bdeff914b6bb0b2dc6127a176e82968d063b66170059d35a94a2eedaa94f570029fd198f6484d4f2b7eaffe8255518542c34e8e576bed761d8fd88b45c22751
7
+ data.tar.gz: b130fe1ce030aab75836c0592f067494d90e43d0f3c0bf049475c573eadf81743921aa41af6d708d98510abc4167e01e1e5b1d3d2ca0004e08b8c10363b9f11c
@@ -0,0 +1,76 @@
1
+ # @author Dan Guilak
2
+
3
+ require 'mongo_mapper'
4
+ require 'set'
5
+
6
+ module MongoMapper
7
+ module Plugins
8
+ module MongoMapperTagger
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ has_many :tags, as: :mongo_taggable, class_name: "MongoMapper::Tag"
13
+ end
14
+
15
+ module ClassMethods
16
+ # Returns a query of all objects with a particular tag.
17
+ #
18
+ # @param tag [String] the tag to find
19
+ # @return [Plucky] the query of objects with that tag.
20
+ def by_tag(tag)
21
+ ids = MongoMapper::Tag.where(mongo_taggable_type: self.name, tag: tag).distinct(:mongo_taggable_id)
22
+
23
+ where(_id: { '$in': ids })
24
+ end
25
+ end
26
+
27
+ # Returns an array of strings of the current tags on the object.
28
+ #
29
+ # @return [Array] an array of strings representing the tags on an object.
30
+ def tag_list
31
+ tags.map(&:tag)
32
+ end
33
+
34
+ # Sets the tags on the object using (see #update_tags_from_list!)
35
+ #
36
+ # @param new_tag_list [String] the new delimiter-separated list of tags with which
37
+ # to update the object.
38
+ def tag_list=(new_tag_list)
39
+ update_tags_from_list!(new_tag_list)
40
+ end
41
+
42
+ # Takes a new list of tags as a delimited string, adds and removes
43
+ # tags on the object as necessary in order to get the object tags to
44
+ # match the new list.
45
+ #
46
+ # @param new_tag_list [String] the new delimiter-separated list of tags with which
47
+ # to update the object
48
+ # @param delimiter [String] the delimiter for new_tag_list (default comma)
49
+ def update_tags_from_list!(new_tag_list, delimiter=',')
50
+ old_tag_list = tag_list.to_set
51
+ new_tag_list = new_tag_list.split(delimiter).to_set
52
+
53
+ to_remove = (old_tag_list - new_tag_list).to_a
54
+ to_add = (new_tag_list - old_tag_list).to_a
55
+
56
+ to_remove.each { |tag| MongoMapper::Tag.remove_by_object_and_tag!(self, tag) }
57
+
58
+ to_add.each { |tag| MongoMapper::Tag.add_by_object_and_tag!(self, tag) }
59
+ end
60
+
61
+ # Removes a tag from the object.
62
+ #
63
+ # @param tag [String] the tag to remove from the object.
64
+ def remove_tag!(tag)
65
+ MongoMapper::Tag.remove_by_object_and_tag!(self, tag)
66
+ end
67
+
68
+ # Adds a tag to the object.
69
+ #
70
+ # @param tag [String] the tag to add to the object.
71
+ def add_tag!(tag)
72
+ MongoMapper::Tag.add_by_object_and_tag!(self, tag)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,114 @@
1
+ # @author Dan Guilak
2
+
3
+ class MongoMapper::Tag
4
+ class TagNotFound < StandardError; end
5
+
6
+ include MongoMapper::Document
7
+
8
+ key :tag, String, required: true
9
+
10
+ ensure_index :tag
11
+ ensure_index :mongo_taggable_type
12
+ ensure_index :mongo_taggable_id
13
+
14
+ belongs_to :mongo_taggable, polymorphic: true
15
+
16
+ validates_uniqueness_of :tag, scope: [ :mongo_taggable_id, :mongo_taggable_type ]
17
+
18
+ class << self
19
+ # Removes a tag by the type of the object, the id of the object, and the tag name.
20
+ # Raises an error if the tag wasn't found.
21
+ #
22
+ # @param taggable_type [String] the type (class name) of the object.
23
+ # @param taggable_id [String] the BSON ID of the object.
24
+ # @param tag_name [String] the name of the tag to remove.
25
+ def remove_by_type_and_id_and_tag!(taggable_type, taggable_id, tag_name)
26
+ success = remove_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name)
27
+
28
+ raise TagNotFound unless success
29
+ end
30
+
31
+ # Removes a tag by the type of the object, the id of the object, and the tag name.
32
+ #
33
+ # @param taggable_type [String] the type (class name) of the object.
34
+ # @param taggable_id [String] the BSON ID of the object.
35
+ # @param tag_name [String] the name of the tag to remove.
36
+ # @return [Boolean] true if successful, false if not
37
+ def remove_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name)
38
+ tag = first(mongo_taggable_type: taggable_type, mongo_taggable_id: taggable_id, tag: tag_name)
39
+
40
+ return false unless tag.present?
41
+
42
+ success = tag.destroy
43
+ success && success["ok"] == 1
44
+ end
45
+
46
+ # Adds a tag by the type of the object, the id of the object, and the tag name.
47
+ # Raises an error if the tag wasn't created successfully.
48
+ #
49
+ # @param taggable_type [String] the type (class name) of the object.
50
+ # @param taggable_id [String] the BSON ID of the object.
51
+ # @param tag_name [String] the name of the tag to add.
52
+ def add_by_type_and_id_and_tag!(taggable_type, taggable_id, tag_name)
53
+ create!(mongo_taggable_type: taggable_type, mongo_taggable_id: taggable_id, tag: tag_name)
54
+ end
55
+
56
+ # Adds a tag by the type of the object, the id of the object, and the tag name.
57
+ #
58
+ # @param taggable_type [String] the type (class name) of the object.
59
+ # @param taggable_id [String] the BSON ID of the object.
60
+ # @param tag_name [String] the name of the tag to add.
61
+ # @return [Boolean] true if successful, false if not
62
+ def add_by_type_and_id_and_tag(taggable_type, taggable_id, tag_name)
63
+ find_args = {
64
+ mongo_taggable_type: taggable_type,
65
+ mongo_taggable_id: taggable_id,
66
+ tag: tag_name,
67
+ }
68
+
69
+ # `create` will return the object if it finds it, rather than returning false.
70
+ if first(find_args).present?
71
+ return false
72
+ end
73
+
74
+ success = create(find_args)
75
+
76
+ success.present?
77
+ end
78
+
79
+ # Removes a tag on an object.
80
+ # Raises if the tag isn't found.
81
+ #
82
+ # @param object [Object] object from which to remove the tag
83
+ # @param tag_name [String] tag to remove from the object.
84
+ def remove_by_object_and_tag!(object, tag_name)
85
+ remove_by_type_and_id_and_tag!(object.class.name, object.id, tag_name)
86
+ end
87
+
88
+ # Removes a tag on an object.
89
+ #
90
+ # @param object [Object] object from which to remove the tag
91
+ # @param tag_name [String] tag to remove from the object.
92
+ # @return [Boolean] true if successful, false if not
93
+ def remove_by_object_and_tag(object, tag_name)
94
+ remove_by_type_and_id_and_tag(object.class.name, object.id, tag_name)
95
+ end
96
+
97
+ # Adds a tag on an object.
98
+ #
99
+ # @param object [Object] object from which to remove the tag
100
+ # @param tag_name [String] tag to add to the object.
101
+ def add_by_object_and_tag(object, tag_name)
102
+ add_by_type_and_id_and_tag(object.class.name, object.id, tag_name)
103
+ end
104
+
105
+ # Adds a tag on an object.
106
+ # Raises if the tag wasn't successfully created.
107
+ #
108
+ # @param object [Object] object on which to add the tag
109
+ # @param tag_name [String] tag to add to the object.
110
+ def add_by_object_and_tag!(object, tag_name)
111
+ add_by_type_and_id_and_tag!(object.class.name, object.id, tag_name)
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongo_mapper'
2
+
3
+ module MongoMapper
4
+ autoload :Tag, 'mongo_mapper/plugins/mongo_mapper_tagger/tag'
5
+
6
+ module Plugins
7
+ autoload :MongoMapperTagger, 'mongo_mapper/plugins/mongo_mapper_tagger'
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo_mapper_tagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Guilak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Dead simple tagging for MongoMapper
14
+ email: dan@learnup.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/mongo_mapper/plugins/mongo_mapper_tagger.rb
20
+ - lib/mongo_mapper/plugins/mongo_mapper_tagger/tag.rb
21
+ - lib/mongo_mapper_tagger.rb
22
+ homepage: http://github.com/golearnup/mongo_mapper_tagger
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Dead simple tagging for MongoMapper
46
+ test_files: []
47
+ has_rdoc: