mongoid-slug 6.0.1 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +20 -20
  3. data/README.md +408 -361
  4. data/lib/mongoid/slug/criteria.rb +120 -107
  5. data/lib/mongoid/slug/index_builder.rb +70 -67
  6. data/lib/mongoid/slug/railtie.rb +11 -9
  7. data/lib/mongoid/slug/slug_id_strategy.rb +5 -3
  8. data/lib/mongoid/slug/unique_slug.rb +204 -173
  9. data/lib/mongoid/slug/version.rb +7 -5
  10. data/lib/mongoid/slug.rb +343 -328
  11. data/lib/mongoid_slug.rb +4 -2
  12. data/lib/tasks/mongoid_slug.rake +17 -15
  13. metadata +9 -168
  14. data/spec/models/alias.rb +0 -6
  15. data/spec/models/article.rb +0 -9
  16. data/spec/models/artist.rb +0 -8
  17. data/spec/models/artwork.rb +0 -10
  18. data/spec/models/author.rb +0 -15
  19. data/spec/models/author_polymorphic.rb +0 -15
  20. data/spec/models/book.rb +0 -12
  21. data/spec/models/book_polymorphic.rb +0 -12
  22. data/spec/models/caption.rb +0 -17
  23. data/spec/models/entity.rb +0 -11
  24. data/spec/models/friend.rb +0 -7
  25. data/spec/models/incorrect_slug_persistence.rb +0 -9
  26. data/spec/models/integer_id.rb +0 -9
  27. data/spec/models/magazine.rb +0 -7
  28. data/spec/models/page.rb +0 -9
  29. data/spec/models/page_localize.rb +0 -9
  30. data/spec/models/page_slug_localized.rb +0 -9
  31. data/spec/models/page_slug_localized_custom.rb +0 -10
  32. data/spec/models/page_slug_localized_history.rb +0 -9
  33. data/spec/models/partner.rb +0 -7
  34. data/spec/models/person.rb +0 -12
  35. data/spec/models/relationship.rb +0 -8
  36. data/spec/models/string_id.rb +0 -9
  37. data/spec/models/subject.rb +0 -7
  38. data/spec/models/without_slug.rb +0 -5
  39. data/spec/mongoid/criteria_spec.rb +0 -207
  40. data/spec/mongoid/index_builder_spec.rb +0 -105
  41. data/spec/mongoid/slug_spec.rb +0 -1175
  42. data/spec/shared/indexes.rb +0 -41
  43. data/spec/spec_helper.rb +0 -61
  44. data/spec/tasks/mongoid_slug_rake_spec.rb +0 -73
@@ -1,107 +1,120 @@
1
- module Mongoid
2
- module Slug
3
- class Criteria < Mongoid::Criteria
4
- # Find the matching document(s) in the criteria for the provided ids or slugs.
5
- #
6
- # If the document _ids are of the type BSON::ObjectId, and all the supplied parameters are
7
- # convertible to BSON::ObjectId (via BSON::ObjectId#from_string), finding will be
8
- # performed via _ids.
9
- #
10
- # If the document has any other type of _id field, and all the supplied parameters are of the same
11
- # type, finding will be performed via _ids.
12
- #
13
- # Otherwise finding will be performed via slugs.
14
- #
15
- # @example Find by an id.
16
- # criteria.find(BSON::ObjectId.new)
17
- #
18
- # @example Find by multiple ids.
19
- # criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
20
- #
21
- # @example Find by a slug.
22
- # criteria.find('some-slug')
23
- #
24
- # @example Find by multiple slugs.
25
- # criteria.find([ 'some-slug', 'some-other-slug' ])
26
- #
27
- # @param [ Array<Object> ] args The ids or slugs to search for.
28
- #
29
- # @return [ Array<Document>, Document ] The matching document(s).
30
- def find(*args)
31
- look_like_slugs?(args.__find_args__) ? find_by_slug!(*args) : super
32
- end
33
-
34
- # Find the matchind document(s) in the criteria for the provided slugs.
35
- #
36
- # @example Find by a slug.
37
- # criteria.find('some-slug')
38
- #
39
- # @example Find by multiple slugs.
40
- # criteria.find([ 'some-slug', 'some-other-slug' ])
41
- #
42
- # @param [ Array<Object> ] args The slugs to search for.
43
- #
44
- # @return [ Array<Document>, Document ] The matching document(s).
45
- def find_by_slug!(*args)
46
- slugs = args.__find_args__
47
- raise_invalid if slugs.any?(&:nil?)
48
- for_slugs(slugs).execute_or_raise_for_slugs(slugs, args.multi_arged?)
49
- end
50
-
51
- def look_like_slugs?(args)
52
- return false unless args.all? { |id| id.is_a?(String) }
53
- id_field = @klass.fields['_id']
54
- @slug_strategy ||= id_field.options[:slug_id_strategy] || build_slug_strategy(id_field.type)
55
- args.none? { |id| @slug_strategy.call(id) }
56
- end
57
-
58
- protected
59
-
60
- # unless a :slug_id_strategy option is defined on the id field,
61
- # use object_id or string strategy depending on the id_type
62
- # otherwise default for all other id_types
63
- def build_slug_strategy(id_type)
64
- type_method = id_type.to_s.downcase.split('::').last + '_slug_strategy'
65
- respond_to?(type_method, true) ? method(type_method) : ->(_id) { false }
66
- end
67
-
68
- # a string will not look like a slug if it looks like a legal BSON::ObjectId
69
- def objectid_slug_strategy(id)
70
- Mongoid::Compatibility::ObjectId.legal?(id)
71
- end
72
-
73
- # a string will always look like a slug
74
- def string_slug_strategy(_id)
75
- true
76
- end
77
-
78
- def for_slugs(slugs)
79
- # _translations
80
- localized = (begin
81
- @klass.fields['_slugs'].options[:localize]
82
- rescue StandardError
83
- false
84
- end)
85
- if localized
86
- def_loc = I18n.default_locale
87
- query = { '$in' => slugs }
88
- where({ '$or' => [{ _slugs: query }, { "_slugs.#{def_loc}" => query }] }).limit(slugs.length)
89
- else
90
- where(_slugs: { '$in' => slugs }).limit(slugs.length)
91
- end
92
- end
93
-
94
- def execute_or_raise_for_slugs(slugs, multi)
95
- result = uniq
96
- check_for_missing_documents_for_slugs!(result, slugs)
97
- multi ? result : result.first
98
- end
99
-
100
- def check_for_missing_documents_for_slugs!(result, slugs)
101
- missing_slugs = slugs - result.map(&:slugs).flatten
102
- return unless !missing_slugs.blank? && Mongoid.raise_not_found_error
103
- raise Errors::DocumentNotFound.new(klass, slugs, missing_slugs)
104
- end
105
- end
106
- end
107
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Slug
5
+ class Criteria < Mongoid::Criteria
6
+ # Find the matching document(s) in the criteria for the provided ids or slugs.
7
+ #
8
+ # If the document _ids are of the type BSON::ObjectId, and all the supplied parameters are
9
+ # convertible to BSON::ObjectId (via BSON::ObjectId#from_string), finding will be
10
+ # performed via _ids.
11
+ #
12
+ # If the document has any other type of _id field, and all the supplied parameters are of the same
13
+ # type, finding will be performed via _ids.
14
+ #
15
+ # Otherwise finding will be performed via slugs.
16
+ #
17
+ # @example Find by an id.
18
+ # criteria.find(BSON::ObjectId.new)
19
+ #
20
+ # @example Find by multiple ids.
21
+ # criteria.find([ BSON::ObjectId.new, BSON::ObjectId.new ])
22
+ #
23
+ # @example Find by a slug.
24
+ # criteria.find('some-slug')
25
+ #
26
+ # @example Find by multiple slugs.
27
+ # criteria.find([ 'some-slug', 'some-other-slug' ])
28
+ #
29
+ # @param [ Array<Object> ] slugs The ids or slugs to search for.
30
+ #
31
+ # @return [ Array<Document>, Document ] The matching document(s).
32
+ def find(*slugs)
33
+ return find_by_slug!(*slugs) if look_like_slugs?(*slugs)
34
+
35
+ super
36
+ end
37
+
38
+ # Find the matchind document(s) in the criteria for the provided slugs.
39
+ #
40
+ # @example Find by a slug.
41
+ # criteria.find('some-slug')
42
+ #
43
+ # @example Find by multiple slugs.
44
+ # criteria.find([ 'some-slug', 'some-other-slug' ])
45
+ #
46
+ # @param [ Array<Object>... ] slugs The slugs to search for.
47
+ #
48
+ # @return [ Array<Document>, Document ] The matching document(s).
49
+ def find_by_slug!(*slugs)
50
+ slugs = find_args(slugs)
51
+ raise_invalid if slugs.any?(&:nil?)
52
+ for_slugs(slugs).execute_or_raise_for_slugs(slugs)
53
+ end
54
+
55
+ def look_like_slugs?(*slugs)
56
+ slugs = find_args(slugs)
57
+ return false unless slugs.all?(String)
58
+
59
+ id_field = @klass.fields['_id']
60
+ @slug_strategy ||= id_field.options[:slug_id_strategy] || build_slug_strategy(id_field.type)
61
+ slugs.none? { |slug| @slug_strategy.call(slug) }
62
+ end
63
+
64
+ protected
65
+
66
+ # unless a :slug_id_strategy option is defined on the id field,
67
+ # use object_id or string strategy depending on the id_type
68
+ # otherwise default for all other id_types
69
+ def build_slug_strategy(id_type)
70
+ type_method = "#{id_type.to_s.downcase.split('::').last}_slug_strategy"
71
+ respond_to?(type_method, true) ? method(type_method) : ->(_id) { false }
72
+ end
73
+
74
+ # a string will not look like a slug if it looks like a legal BSON::ObjectId
75
+ def objectid_slug_strategy(id)
76
+ BSON::ObjectId.legal?(id)
77
+ end
78
+
79
+ # a string will always look like a slug
80
+ def string_slug_strategy(_id)
81
+ true
82
+ end
83
+
84
+ def for_slugs(slugs)
85
+ # _translations
86
+ localized = (begin
87
+ @klass.fields['_slugs'].options[:localize]
88
+ rescue StandardError
89
+ false
90
+ end)
91
+ if localized
92
+ def_loc = I18n.default_locale
93
+ query = { '$in' => slugs }
94
+ where({ '$or' => [{ _slugs: query }, { "_slugs.#{def_loc}" => query }] }).limit(slugs.length)
95
+ else
96
+ where(_slugs: { '$in' => slugs }).limit(slugs.length)
97
+ end
98
+ end
99
+
100
+ def find_args(args)
101
+ args = args.flatten
102
+ args.uniq!(&:to_s)
103
+ args
104
+ end
105
+
106
+ def execute_or_raise_for_slugs(slugs)
107
+ result = uniq
108
+ check_for_missing_documents_for_slugs!(result, slugs)
109
+ slugs.size == 1 ? result.first : result
110
+ end
111
+
112
+ def check_for_missing_documents_for_slugs!(result, slugs)
113
+ missing_slugs = slugs - result.map(&:slugs).flatten
114
+ return unless !missing_slugs.blank? && Mongoid.raise_not_found_error
115
+
116
+ raise Errors::DocumentNotFound.new(klass, slugs, missing_slugs)
117
+ end
118
+ end
119
+ end
120
+ end
@@ -1,67 +1,70 @@
1
- module Mongoid
2
- module Slug
3
- module IndexBuilder
4
- extend self
5
-
6
- # Creates indexes on a document for a given slug scope
7
- #
8
- # @param [ Mongoid::Document ] doc The document on which to create the index(es)
9
- # @param [ String or Symbol ] scope_key The optional scope key for the index(es)
10
- # @param [ Boolean ] by_model_type Whether or not to use single table inheritance
11
- # @param [ Boolean or Array ] localize The locale for localized index field
12
- #
13
- # @return [ Array(Hash, Hash) ] the indexable fields and index options.
14
- def build_indexes(doc, scope_key = nil, by_model_type = false, locales = nil)
15
- if locales.is_a?(Array)
16
- locales.each { |locale| build_index(doc, scope_key, by_model_type, locale) }
17
- else
18
- build_index(doc, scope_key, by_model_type, locales)
19
- end
20
- end
21
-
22
- private
23
-
24
- def build_index(doc, scope_key = nil, by_model_type = false, locale = nil)
25
- # The order of field keys is intentional.
26
- # See: http://docs.mongodb.org/manual/core/index-compound/
27
- fields = {}
28
- fields[:_type] = 1 if by_model_type
29
- fields[scope_key] = 1 if scope_key
30
-
31
- locale = ::I18n.default_locale if locale.is_a?(TrueClass)
32
- if locale
33
- fields[:"_slugs.#{locale}"] = 1
34
- else
35
- fields[:_slugs] = 1
36
- end
37
-
38
- # By design, we use the unique index constraint when possible to enforce slug uniqueness.
39
- # When migrating legacy data to Mongoid slug, the _slugs field may be null on many records,
40
- # hence we set the sparse index option to ignore these from the unique index.
41
- # See: http://docs.mongodb.org/manual/core/index-sparse/
42
- #
43
- # There are three edge cases where the index must not be unique:
44
- #
45
- # 1) Legacy tables with `scope_key`. The sparse indexes on compound keys (scope + _slugs) are
46
- # whenever ANY of the key values are present (e.g. when scope is set and _slugs is unset),
47
- # and collisions will occur when multiple records have the same scope but null slugs.
48
- #
49
- # 2) Single Table Inheritance (`by_model_type`). MongoDB creates indexes on the parent collection,
50
- # irrespective of how STI is defined in Mongoid, i.e. ANY child index will be applied to EVERY child.
51
- # This can cause collisions using various combinations of scopes.
52
- #
53
- # In the future, MongoDB may implement partial indexes or improve sparse index behavior.
54
- # See: https://jira.mongodb.org/browse/SERVER-785
55
- # https://jira.mongodb.org/browse/SERVER-13780
56
- # https://jira.mongodb.org/browse/SERVER-10403
57
- options = {}
58
- unless scope_key || by_model_type
59
- options[:unique] = true
60
- options[:sparse] = true
61
- end
62
-
63
- doc.index(fields, options)
64
- end
65
- end
66
- end
67
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Slug
5
+ module IndexBuilder
6
+ extend self
7
+
8
+ # Creates indexes on a document for a given slug scope
9
+ #
10
+ # @param [ Mongoid::Document ] doc The document on which to create the index(es)
11
+ # @param [ String or Symbol or Array<String, Symbol> ] scope_key The optional scope key for the index(es)
12
+ # @param [ Boolean ] by_model_type Whether or not to use single table inheritance
13
+ # @param [ Boolean or Array ] localize The locale for localized index field
14
+ #
15
+ # @return [ Array(Hash, Hash) ] the indexable fields and index options.
16
+ def build_indexes(doc, scope_key = nil, by_model_type = false, locales = nil)
17
+ if locales.is_a?(Array)
18
+ locales.each { |locale| build_index(doc, scope_key, by_model_type, locale) }
19
+ else
20
+ build_index(doc, scope_key, by_model_type, locales)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def build_index(doc, scope_key = nil, by_model_type = false, locale = nil)
27
+ # The order of field keys is intentional.
28
+ # See: http://docs.mongodb.org/manual/core/index-compound/
29
+ fields = {}
30
+ fields[:_type] = 1 if by_model_type
31
+
32
+ Array(scope_key).each { |key| fields[key] = 1 }
33
+
34
+ locale = ::I18n.default_locale if locale.is_a?(TrueClass)
35
+ if locale
36
+ fields[:"_slugs.#{locale}"] = 1
37
+ else
38
+ fields[:_slugs] = 1
39
+ end
40
+
41
+ # By design, we use the unique index constraint when possible to enforce slug uniqueness.
42
+ # When migrating legacy data to Mongoid slug, the _slugs field may be null on many records,
43
+ # hence we set the sparse index option to ignore these from the unique index.
44
+ # See: http://docs.mongodb.org/manual/core/index-sparse/
45
+ #
46
+ # There are three edge cases where the index must not be unique:
47
+ #
48
+ # 1) Legacy tables with `scope_key`. The sparse indexes on compound keys (scope + _slugs) are
49
+ # whenever ANY of the key values are present (e.g. when scope is set and _slugs is unset),
50
+ # and collisions will occur when multiple records have the same scope but null slugs.
51
+ #
52
+ # 2) Single Table Inheritance (`by_model_type`). MongoDB creates indexes on the parent collection,
53
+ # irrespective of how STI is defined in Mongoid, i.e. ANY child index will be applied to EVERY child.
54
+ # This can cause collisions using various combinations of scopes.
55
+ #
56
+ # In the future, MongoDB may implement partial indexes or improve sparse index behavior.
57
+ # See: https://jira.mongodb.org/browse/SERVER-785
58
+ # https://jira.mongodb.org/browse/SERVER-13780
59
+ # https://jira.mongodb.org/browse/SERVER-10403
60
+ options = {}
61
+ unless scope_key || by_model_type
62
+ options[:unique] = true
63
+ options[:sparse] = true
64
+ end
65
+
66
+ doc.index(fields, options)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -1,9 +1,11 @@
1
- module Mongoid
2
- module Slug
3
- class Railtie < Rails::Railtie
4
- rake_tasks do
5
- Dir[File.join(File.dirname(__FILE__), '../../tasks/*.rake')].each { |f| load f }
6
- end
7
- end
8
- end
9
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Slug
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ Dir[File.join(File.dirname(__FILE__), '../../tasks/*.rake')].each { |f| load f }
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,5 @@
1
- Mongoid::Fields.option(:slug_id_strategy) do |_model, field, value|
2
- field.options[:slug_id_strategy] = value
3
- end
1
+ # frozen_string_literal: true
2
+
3
+ Mongoid::Fields.option(:slug_id_strategy) do |_model, field, value|
4
+ field.options[:slug_id_strategy] = value
5
+ end