acts-as-taggable-on-mongoid 6.0.1.2 → 6.0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +4 -4
- data/lib/acts-as-taggable-on-mongoid.rb +16 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tag_associations.rb +15 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tag_fields.rb +23 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tag_methods.rb +67 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tag_model.rb +17 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tag_scopes.rb +23 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tag_validations.rb +18 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tagging_associations.rb +15 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tagging_fields.rb +22 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tagging_methods.rb +51 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tagging_model.rb +25 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tagging_scopes.rb +21 -0
- data/lib/acts_as_taggable_on_mongoid/models/concerns/tagging_validations.rb +22 -0
- data/lib/acts_as_taggable_on_mongoid/models/tag.rb +1 -94
- data/lib/acts_as_taggable_on_mongoid/models/tagging.rb +1 -71
- data/lib/acts_as_taggable_on_mongoid/version.rb +1 -1
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c13fda8fcc653c6f63e06b5cede43f5981330550
|
4
|
+
data.tar.gz: 0e29ec9543e00254f4b495b40ff6f2499c8cb0cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb149028e0ba4adc52119f84059fbb8afd5090f14d1812895b69b01697df8603c93b392982dd1c766adc34da443059820c22ce34ef392e3b04906f21242d5fbf
|
7
|
+
data.tar.gz: 2e07b06e928dacc5d88a409f5fb8985b3a5f032a5422ba2ec4e30930baf2bfb03ee6bffedce2f6625edd4281f2811ffb71cb9af7b204cd4c9f35aef9c52f8cf2
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
acts-as-taggable-on-mongoid (6.0.1.
|
4
|
+
acts-as-taggable-on-mongoid (6.0.1.3)
|
5
5
|
activesupport (~> 4.2)
|
6
6
|
mongoid (~> 5.2)
|
7
7
|
|
@@ -38,7 +38,7 @@ GEM
|
|
38
38
|
descendants_tracker (~> 0.0.1)
|
39
39
|
colorize (0.8.1)
|
40
40
|
concurrent-ruby (1.0.5)
|
41
|
-
cornucopia (0.1.
|
41
|
+
cornucopia (0.1.55)
|
42
42
|
activesupport (< 5.0)
|
43
43
|
database_cleaner (1.7.0)
|
44
44
|
descendants_tracker (0.0.4)
|
@@ -74,7 +74,7 @@ GEM
|
|
74
74
|
tzinfo (>= 0.3.37)
|
75
75
|
multi_xml (0.6.0)
|
76
76
|
multipart-post (2.0.0)
|
77
|
-
octokit (4.
|
77
|
+
octokit (4.12.0)
|
78
78
|
sawyer (~> 0.8.0, >= 0.5.3)
|
79
79
|
origin (2.3.1)
|
80
80
|
parallel (1.12.1)
|
@@ -138,7 +138,7 @@ GEM
|
|
138
138
|
rspec-support (3.8.0)
|
139
139
|
rspec_junit_formatter (0.3.0)
|
140
140
|
rspec-core (>= 2, < 4, != 2.12.0)
|
141
|
-
rubocop (0.59.
|
141
|
+
rubocop (0.59.1)
|
142
142
|
jaro_winkler (~> 1.5.1)
|
143
143
|
parallel (~> 1.10)
|
144
144
|
parser (>= 2.5, != 2.5.1.1)
|
@@ -48,6 +48,22 @@ module ActsAsTaggableOnMongoid
|
|
48
48
|
|
49
49
|
autoload :Taggable
|
50
50
|
|
51
|
+
autoload_under "models/concerns" do
|
52
|
+
autoload :TagFields
|
53
|
+
autoload :TagAssociations
|
54
|
+
autoload :TagValidations
|
55
|
+
autoload :TagScopes
|
56
|
+
autoload :TagMethods
|
57
|
+
autoload :TagModel
|
58
|
+
|
59
|
+
autoload :TaggingFields
|
60
|
+
autoload :TaggingAssociations
|
61
|
+
autoload :TaggingValidations
|
62
|
+
autoload :TaggingScopes
|
63
|
+
autoload :TaggingMethods
|
64
|
+
autoload :TaggingModel
|
65
|
+
end
|
66
|
+
|
51
67
|
autoload_under :Models do
|
52
68
|
autoload :Tag
|
53
69
|
autoload :Tagging
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TagAssociations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
### ASSOCIATIONS:
|
9
|
+
|
10
|
+
has_many :taggings, dependent: :destroy, class_name: "ActsAsTaggableOnMongoid::Models::Tagging"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TagFields
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Mongoid::Document
|
9
|
+
include Mongoid::Timestamps
|
10
|
+
|
11
|
+
field :name, type: String
|
12
|
+
field :taggings_count, type: Integer, default: 0
|
13
|
+
field :context, type: String
|
14
|
+
field :taggable_type, type: String
|
15
|
+
|
16
|
+
# field :type, type: String
|
17
|
+
|
18
|
+
index({ name: 1, taggable_type: 1, context: 1 }, unique: true)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TagMethods
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
### CLASS METHODS:
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def find_or_create_all_with_like_by_name(tag_definition, *list)
|
11
|
+
list = ActsAsTaggableOnMongoid::TagList.new(tag_definition, *Array.wrap(list).flatten)
|
12
|
+
|
13
|
+
return [] if list.empty?
|
14
|
+
|
15
|
+
list.map do |tag_name|
|
16
|
+
begin
|
17
|
+
tries ||= 3
|
18
|
+
|
19
|
+
existing_tag = tag_definition.tags_table.for_tag(tag_definition).named(tag_name).first
|
20
|
+
|
21
|
+
existing_tag || create_tag(tag_definition, tag_name)
|
22
|
+
rescue Mongoid::Errors::Validations
|
23
|
+
# :nocov:
|
24
|
+
if (tries -= 1).positive?
|
25
|
+
retry
|
26
|
+
end
|
27
|
+
|
28
|
+
raise ActsAsTaggableOnMongoid::Errors::DuplicateTagError.new, "'#{tag_name}' has already been taken"
|
29
|
+
# :nocov:
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_tag(tag_definition, name)
|
35
|
+
tag_definition.tags_table.create(name: name,
|
36
|
+
context: tag_definition.tag_type,
|
37
|
+
taggable_type: tag_definition.owner.name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def as_8bit_ascii(string)
|
41
|
+
string = string.to_s
|
42
|
+
if defined?(Encoding)
|
43
|
+
string.dup.force_encoding("BINARY")
|
44
|
+
else
|
45
|
+
# :nocov:
|
46
|
+
string.mb_chars
|
47
|
+
# :nocov:
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
### INSTANCE METHODS:
|
53
|
+
|
54
|
+
def ==(other)
|
55
|
+
super || (other.class == self.class &&
|
56
|
+
name == other.name &&
|
57
|
+
context == other.context &&
|
58
|
+
taggable_type == other.taggable_type)
|
59
|
+
end
|
60
|
+
|
61
|
+
def to_s
|
62
|
+
name
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TagModel
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TagFields
|
9
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TagMethods
|
10
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TagAssociations
|
11
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TagValidations
|
12
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TagScopes
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TagScopes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
### SCOPES:
|
9
|
+
scope :most_used, ->(limit = 20) { order("taggings_count desc").limit(limit) }
|
10
|
+
scope :least_used, ->(limit = 20) { order("taggings_count asc").limit(limit) }
|
11
|
+
|
12
|
+
scope :named, ->(name) { where(name: as_8bit_ascii(name)) }
|
13
|
+
scope :named_any, ->(*names) { where(:name.in => names.map { |name| as_8bit_ascii(name) }) }
|
14
|
+
scope :named_like, ->(name) { where(name: /#{as_8bit_ascii(name)}/i) }
|
15
|
+
scope :named_like_any, ->(*names) { where(:name.in => names.map { |name| /#{as_8bit_ascii(name)}/i }) }
|
16
|
+
scope :for_context, ->(context) { where(context: context) }
|
17
|
+
scope :for_taggable_class, ->(taggable_type) { where(taggable_type: taggable_type.name) }
|
18
|
+
scope :for_tag, ->(tag_definition) { for_taggable_class(tag_definition.owner).for_context(tag_definition.tag_type) }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TagValidations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
### VALIDATIONS:
|
9
|
+
|
10
|
+
validates :name, presence: true
|
11
|
+
validates :context, presence: true
|
12
|
+
validates :taggable_type, presence: true
|
13
|
+
validates :name, uniqueness: { scope: %i[context taggable_type] }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TaggingAssociations
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
belongs_to :tag, counter_cache: true, inverse_of: :taggings
|
9
|
+
belongs_to :taggable, polymorphic: true
|
10
|
+
# belongs_to :tagger, { polymorphic: true, optional: true }
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
module TaggingFields
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include Mongoid::Document
|
9
|
+
include Mongoid::Timestamps
|
10
|
+
|
11
|
+
field :tag_name, type: String
|
12
|
+
field :context, type: String
|
13
|
+
|
14
|
+
# If/when adding the concept of a tagger, this index will need to be changed.
|
15
|
+
index({ taggable_id: 1, taggable_type: 1, context: 1, tag_name: 1 }, unique: true, name: "tagging_taggable_context_tag_name")
|
16
|
+
index(tag_name: 1)
|
17
|
+
index(tag_id: 1, tag_type: 1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
# This module includes the methods and callbacks needed/used by a Tagging model
|
5
|
+
module TaggingMethods
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
after_save :tagging_saved
|
10
|
+
after_destroy :tagging_destroyed
|
11
|
+
after_destroy :remove_unused_tags
|
12
|
+
end
|
13
|
+
|
14
|
+
### INSTANCE METHODS:
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def remove_unused_tags
|
19
|
+
return nil unless taggable
|
20
|
+
|
21
|
+
tag_definition = taggable.tag_types[context]
|
22
|
+
|
23
|
+
return unless tag_definition&.remove_unused_tags?
|
24
|
+
|
25
|
+
tag.destroy if tag.reload.taggings_count.zero?
|
26
|
+
end
|
27
|
+
|
28
|
+
def tagging_saved
|
29
|
+
tag_definition = taggable.tag_types[context]
|
30
|
+
|
31
|
+
return unless tag_definition
|
32
|
+
|
33
|
+
tag_list = taggable.public_send(tag_definition.tag_list_name)
|
34
|
+
tag_list.add_tagging(self)
|
35
|
+
end
|
36
|
+
|
37
|
+
def tagging_destroyed
|
38
|
+
taggable_was = taggable_type_was.constantize.where(id: taggable_id_was).first
|
39
|
+
|
40
|
+
return unless taggable_was
|
41
|
+
|
42
|
+
tag_definition = taggable_was.tag_types[context_was]
|
43
|
+
|
44
|
+
return unless tag_definition
|
45
|
+
|
46
|
+
taggable_was.public_send(tag_definition.tag_list_name).remove(tag_name_was)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
# This module includes all methods and defintions needed/used by a Tagging model.
|
5
|
+
#
|
6
|
+
# Tagging model definitions are done via including this or the sub-modules so that
|
7
|
+
# the definitions of the core attributes for a Tagging model can be defined in each
|
8
|
+
# class separately.
|
9
|
+
#
|
10
|
+
# The primary reason for doing this is validations, which call the superclass validations
|
11
|
+
# for classes that inherit causing problems with independent inherted classes.
|
12
|
+
module TaggingModel
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
included do
|
16
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TaggingFields
|
17
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TaggingMethods
|
18
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TaggingAssociations
|
19
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TaggingValidations
|
20
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TaggingScopes
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
# This module includes the scopes needed/used by a Tagging model
|
5
|
+
module TaggingScopes
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
DEFAULT_CONTEXT = "tags"
|
9
|
+
|
10
|
+
included do
|
11
|
+
# scope :owned_by, ->(owner) { where(tagger: owner) }
|
12
|
+
# scope :not_owned, -> { where(tagger_id: nil, tagger_type: nil) }
|
13
|
+
|
14
|
+
scope :by_contexts, ->(*contexts) { where(:context.in => Array.wrap(contexts.presence || DEFAULT_CONTEXT)) }
|
15
|
+
scope :by_context, ->(context = DEFAULT_CONTEXT) { by_contexts(context.to_s) }
|
16
|
+
scope :for_tag, ->(tag_definition) { where(taggable_type: tag_definition.owner.name).by_context(tag_definition.tag_type) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActsAsTaggableOnMongoid
|
2
|
+
module Models
|
3
|
+
module Concerns
|
4
|
+
# This module includes the validations needed/used by a Tagging model
|
5
|
+
module TaggingValidations
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
validates :tag_name, presence: true
|
10
|
+
validates :context, presence: true
|
11
|
+
validates :tag, presence: true
|
12
|
+
validates :taggable, presence: true
|
13
|
+
|
14
|
+
# validates :tag_id, uniqueness: {scope: [:taggable_type, :taggable_id, :context, :tagger_id, :tagger_type]}
|
15
|
+
validates :tag_name, uniqueness: { scope: %i[taggable_type taggable_id context] }
|
16
|
+
# validates :tag_id, uniqueness: {scope: [:taggable_type, :taggable_id, :context, :tagger_id, :tagger_type]}
|
17
|
+
validates :tag_id, uniqueness: { scope: %i[taggable_type taggable_id context] }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -4,100 +4,7 @@ module ActsAsTaggableOnMongoid
|
|
4
4
|
module Models
|
5
5
|
# A class representing all tags that have ever been set on a model.
|
6
6
|
class Tag
|
7
|
-
include
|
8
|
-
include Mongoid::Timestamps
|
9
|
-
|
10
|
-
field :name, type: String
|
11
|
-
field :taggings_count, type: Integer, default: 0
|
12
|
-
field :context, type: String
|
13
|
-
field :taggable_type, type: String
|
14
|
-
|
15
|
-
# field :type, type: String
|
16
|
-
|
17
|
-
index({ name: 1, taggable_type: 1, context: 1 }, unique: true)
|
18
|
-
|
19
|
-
### ASSOCIATIONS:
|
20
|
-
|
21
|
-
has_many :taggings, dependent: :destroy, class_name: "ActsAsTaggableOnMongoid::Models::Tagging"
|
22
|
-
|
23
|
-
### VALIDATIONS:
|
24
|
-
|
25
|
-
validates :name, presence: true
|
26
|
-
validates :context, presence: true
|
27
|
-
validates :taggable_type, presence: true
|
28
|
-
validates :name, uniqueness: { scope: %i[context taggable_type] }
|
29
|
-
|
30
|
-
### SCOPES:
|
31
|
-
scope :most_used, ->(limit = 20) { order("taggings_count desc").limit(limit) }
|
32
|
-
scope :least_used, ->(limit = 20) { order("taggings_count asc").limit(limit) }
|
33
|
-
|
34
|
-
scope :named, ->(name) { where(name: as_8bit_ascii(name)) }
|
35
|
-
scope :named_any, ->(*names) { where(:name.in => names.map { |name| as_8bit_ascii(name) }) }
|
36
|
-
scope :named_like, ->(name) { where(name: /#{as_8bit_ascii(name)}/i) }
|
37
|
-
scope :named_like_any, ->(*names) { where(:name.in => names.map { |name| /#{as_8bit_ascii(name)}/i }) }
|
38
|
-
scope :for_context, ->(context) { where(context: context) }
|
39
|
-
scope :for_taggable_class, ->(taggable_type) { where(taggable_type: taggable_type.name) }
|
40
|
-
scope :for_tag, ->(tag_definition) { for_taggable_class(tag_definition.owner).for_context(tag_definition.tag_type) }
|
41
|
-
|
42
|
-
### CLASS METHODS:
|
43
|
-
|
44
|
-
class << self
|
45
|
-
def find_or_create_all_with_like_by_name(tag_definition, *list)
|
46
|
-
list = ActsAsTaggableOnMongoid::TagList.new(tag_definition, *Array.wrap(list).flatten)
|
47
|
-
|
48
|
-
return [] if list.empty?
|
49
|
-
|
50
|
-
list.map do |tag_name|
|
51
|
-
begin
|
52
|
-
tries ||= 3
|
53
|
-
|
54
|
-
existing_tag = tag_definition.tags_table.for_tag(tag_definition).named(tag_name).first
|
55
|
-
|
56
|
-
existing_tag || create_tag(tag_definition, tag_name)
|
57
|
-
rescue Mongoid::Errors::Validations
|
58
|
-
# :nocov:
|
59
|
-
if (tries -= 1).positive?
|
60
|
-
retry
|
61
|
-
end
|
62
|
-
|
63
|
-
raise ActsAsTaggableOnMongoid::Errors::DuplicateTagError.new, "'#{tag_name}' has already been taken"
|
64
|
-
# :nocov:
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
private
|
70
|
-
|
71
|
-
def create_tag(tag_definition, name)
|
72
|
-
tag_definition.tags_table.create(name: name,
|
73
|
-
context: tag_definition.tag_type,
|
74
|
-
taggable_type: tag_definition.owner.name)
|
75
|
-
end
|
76
|
-
|
77
|
-
def as_8bit_ascii(string)
|
78
|
-
string = string.to_s
|
79
|
-
if defined?(Encoding)
|
80
|
-
string.dup.force_encoding("BINARY")
|
81
|
-
else
|
82
|
-
# :nocov:
|
83
|
-
string.mb_chars
|
84
|
-
# :nocov:
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
### INSTANCE METHODS:
|
90
|
-
|
91
|
-
def ==(other)
|
92
|
-
super || (other.class == self.class &&
|
93
|
-
name == other.name &&
|
94
|
-
context == other.context &&
|
95
|
-
taggable_type == other.taggable_type)
|
96
|
-
end
|
97
|
-
|
98
|
-
def to_s
|
99
|
-
name
|
100
|
-
end
|
7
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TagModel
|
101
8
|
end
|
102
9
|
end
|
103
10
|
end
|
@@ -4,77 +4,7 @@ module ActsAsTaggableOnMongoid
|
|
4
4
|
module Models
|
5
5
|
# A class representing the actual tags assigned to a particular model object
|
6
6
|
class Tagging
|
7
|
-
include
|
8
|
-
include Mongoid::Timestamps
|
9
|
-
|
10
|
-
DEFAULT_CONTEXT = "tags"
|
11
|
-
|
12
|
-
after_save :tagging_saved
|
13
|
-
after_destroy :tagging_destroyed
|
14
|
-
|
15
|
-
field :tag_name, type: String
|
16
|
-
field :context, type: String
|
17
|
-
|
18
|
-
belongs_to :tag, counter_cache: true, inverse_of: :taggings
|
19
|
-
belongs_to :taggable, polymorphic: true
|
20
|
-
# belongs_to :tagger, { polymorphic: true, optional: true }
|
21
|
-
|
22
|
-
# If/when adding the concept of a tagger, this index will need to be changed.
|
23
|
-
index({ taggable_id: 1, taggable_type: 1, context: 1, tag_name: 1 }, unique: true, name: "tagging_taggable_context_tag_name")
|
24
|
-
index(tag_name: 1)
|
25
|
-
index(tag_id: 1, tag_type: 1)
|
26
|
-
|
27
|
-
# scope :owned_by, ->(owner) { where(tagger: owner) }
|
28
|
-
# scope :not_owned, -> { where(tagger_id: nil, tagger_type: nil) }
|
29
|
-
|
30
|
-
scope :by_contexts, ->(*contexts) { where(:context.in => Array.wrap(contexts.presence || DEFAULT_CONTEXT)) }
|
31
|
-
scope :by_context, ->(context = DEFAULT_CONTEXT) { by_contexts(context.to_s) }
|
32
|
-
scope :for_tag, ->(tag_definition) { where(taggable_type: tag_definition.owner.name).by_context(tag_definition.tag_type) }
|
33
|
-
|
34
|
-
validates :tag_name, presence: true
|
35
|
-
validates :context, presence: true
|
36
|
-
validates :tag, presence: true
|
37
|
-
validates :taggable, presence: true
|
38
|
-
|
39
|
-
# validates :tag_id, uniqueness: {scope: [:taggable_type, :taggable_id, :context, :tagger_id, :tagger_type]}
|
40
|
-
validates :tag_name, uniqueness: { scope: %i[taggable_type taggable_id context] }
|
41
|
-
# validates :tag_id, uniqueness: {scope: [:taggable_type, :taggable_id, :context, :tagger_id, :tagger_type]}
|
42
|
-
validates :tag_id, uniqueness: { scope: %i[taggable_type taggable_id context] }
|
43
|
-
|
44
|
-
after_destroy :remove_unused_tags
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def remove_unused_tags
|
49
|
-
return nil unless taggable
|
50
|
-
|
51
|
-
tag_definition = taggable.tag_types[context]
|
52
|
-
|
53
|
-
return unless tag_definition&.remove_unused_tags?
|
54
|
-
|
55
|
-
tag.destroy if tag.reload.taggings_count.zero?
|
56
|
-
end
|
57
|
-
|
58
|
-
def tagging_saved
|
59
|
-
tag_definition = taggable.tag_types[context]
|
60
|
-
|
61
|
-
return unless tag_definition
|
62
|
-
|
63
|
-
tag_list = taggable.public_send(tag_definition.tag_list_name)
|
64
|
-
tag_list.add_tagging(self)
|
65
|
-
end
|
66
|
-
|
67
|
-
def tagging_destroyed
|
68
|
-
taggable_was = taggable_type_was.constantize.where(id: taggable_id_was).first
|
69
|
-
|
70
|
-
return unless taggable_was
|
71
|
-
|
72
|
-
tag_definition = taggable_was.tag_types[context_was]
|
73
|
-
|
74
|
-
return unless tag_definition
|
75
|
-
|
76
|
-
taggable_was.public_send(tag_definition.tag_list_name).remove(tag_name_was)
|
77
|
-
end
|
7
|
+
include ActsAsTaggableOnMongoid::Models::Concerns::TaggingModel
|
78
8
|
end
|
79
9
|
end
|
80
10
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts-as-taggable-on-mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.0.1.
|
4
|
+
version: 6.0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- RealNobody
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -326,6 +326,18 @@ files:
|
|
326
326
|
- lib/acts_as_taggable_on_mongoid/default_parser.rb
|
327
327
|
- lib/acts_as_taggable_on_mongoid/errors/duplicate_tag_error.rb
|
328
328
|
- lib/acts_as_taggable_on_mongoid/generic_parser.rb
|
329
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tag_associations.rb
|
330
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tag_fields.rb
|
331
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tag_methods.rb
|
332
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tag_model.rb
|
333
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tag_scopes.rb
|
334
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tag_validations.rb
|
335
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tagging_associations.rb
|
336
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tagging_fields.rb
|
337
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tagging_methods.rb
|
338
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tagging_model.rb
|
339
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tagging_scopes.rb
|
340
|
+
- lib/acts_as_taggable_on_mongoid/models/concerns/tagging_validations.rb
|
329
341
|
- lib/acts_as_taggable_on_mongoid/models/tag.rb
|
330
342
|
- lib/acts_as_taggable_on_mongoid/models/tagging.rb
|
331
343
|
- lib/acts_as_taggable_on_mongoid/tag_list.rb
|