mongoid-slug 5.3.0 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +20 -20
  3. data/README.md +361 -360
  4. data/lib/mongoid/slug.rb +328 -366
  5. data/lib/mongoid/slug/criteria.rb +107 -107
  6. data/lib/mongoid/slug/{index.rb → index_builder.rb} +67 -51
  7. data/lib/mongoid/slug/railtie.rb +9 -9
  8. data/lib/mongoid/slug/slug_id_strategy.rb +3 -3
  9. data/lib/mongoid/slug/unique_slug.rb +173 -154
  10. data/lib/mongoid/slug/version.rb +5 -5
  11. data/lib/mongoid_slug.rb +2 -2
  12. data/lib/tasks/mongoid_slug.rake +15 -19
  13. data/spec/models/alias.rb +6 -6
  14. data/spec/models/article.rb +9 -9
  15. data/spec/models/artist.rb +8 -8
  16. data/spec/models/artwork.rb +10 -10
  17. data/spec/models/author.rb +15 -15
  18. data/spec/models/author_polymorphic.rb +15 -15
  19. data/spec/models/book.rb +12 -12
  20. data/spec/models/book_polymorphic.rb +12 -12
  21. data/spec/models/caption.rb +17 -17
  22. data/spec/models/entity.rb +11 -12
  23. data/spec/models/friend.rb +7 -7
  24. data/spec/models/incorrect_slug_persistence.rb +9 -9
  25. data/spec/models/integer_id.rb +9 -9
  26. data/spec/models/magazine.rb +7 -7
  27. data/spec/models/page.rb +9 -9
  28. data/spec/models/page_localize.rb +9 -9
  29. data/spec/models/page_slug_localized.rb +9 -9
  30. data/spec/models/page_slug_localized_custom.rb +10 -10
  31. data/spec/models/page_slug_localized_history.rb +9 -9
  32. data/spec/models/partner.rb +7 -7
  33. data/spec/models/person.rb +12 -12
  34. data/spec/models/relationship.rb +8 -8
  35. data/spec/models/string_id.rb +9 -9
  36. data/spec/models/subject.rb +7 -7
  37. data/spec/models/without_slug.rb +5 -5
  38. data/spec/mongoid/criteria_spec.rb +207 -208
  39. data/spec/mongoid/index_builder_spec.rb +105 -0
  40. data/spec/mongoid/slug_spec.rb +1175 -1170
  41. data/spec/shared/indexes.rb +41 -41
  42. data/spec/spec_helper.rb +61 -67
  43. data/spec/tasks/mongoid_slug_rake_spec.rb +73 -73
  44. metadata +14 -24
  45. data/lib/mongoid/slug/paranoia.rb +0 -20
  46. data/spec/models/document_paranoid.rb +0 -9
  47. data/spec/models/paranoid_document.rb +0 -8
  48. data/spec/models/paranoid_permanent.rb +0 -10
  49. data/spec/mongoid/index_spec.rb +0 -34
  50. data/spec/mongoid/paranoia_spec.rb +0 -230
@@ -1,5 +1,5 @@
1
- module Mongoid #:nodoc:
2
- module Slug
3
- VERSION = '5.3.0'.freeze
4
- end
5
- end
1
+ module Mongoid #:nodoc:
2
+ module Slug
3
+ VERSION = '6.0.1'.freeze
4
+ end
5
+ end
data/lib/mongoid_slug.rb CHANGED
@@ -1,2 +1,2 @@
1
- # To be removed
2
- require 'mongoid/slug'
1
+ # To be removed
2
+ require 'mongoid/slug'
@@ -1,19 +1,15 @@
1
- namespace :mongoid_slug do
2
- desc 'Goes though all documents and sets slug if not already set'
3
- task set: :environment do |_, args|
4
- ::Rails.application.eager_load! if defined?(Rails)
5
- klasses = Module.constants.find_all do |const|
6
- next if const == :MissingSourceFile
7
- const != const.upcase ? Mongoid::Slug > (Object.const_get const) : nil
8
- end
9
- klasses.map! { |klass| klass.to_s.constantize }
10
- unless klasses.blank?
11
- models = args.extras
12
- klasses = (klasses.map(&:to_s) & models.map(&:classify)).map(&:constantize) if models.any?
13
- klasses.each do |klass|
14
- # set slug for objects having blank slug
15
- klass.each { |object| object.set_slug! unless object.slugs? && object.slugs.any? }
16
- end
17
- end
18
- end
19
- end
1
+ namespace :mongoid_slug do
2
+ desc 'Goes though all documents and sets slug if not already set'
3
+ task set: :environment do |_, args|
4
+ ::Rails.application.eager_load! if defined?(Rails)
5
+ klasses = Mongoid::Config.models.find_all { |c| c.ancestors.include?(Mongoid::Slug) }
6
+ unless klasses.blank?
7
+ models = args.extras
8
+ klasses = (klasses.map(&:to_s) & models.map(&:classify)).map(&:constantize) if models.any?
9
+ klasses.each do |klass|
10
+ # set slug for objects having blank slug
11
+ klass.each { |object| object.set_slug! unless object.slugs? && object.slugs.any? }
12
+ end
13
+ end
14
+ end
15
+ end
data/spec/models/alias.rb CHANGED
@@ -1,6 +1,6 @@
1
- class Alias
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :name, as: :author_name
5
- slug :author_name
6
- end
1
+ class Alias
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :name, as: :author_name
5
+ slug :author_name
6
+ end
@@ -1,9 +1,9 @@
1
- class Article
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :brief
5
- field :title
6
- slug :title, :brief do |doc|
7
- [doc.title, doc.brief].reject(&:blank?).first
8
- end
9
- end
1
+ class Article
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :brief
5
+ field :title
6
+ slug :title, :brief do |doc|
7
+ [doc.title, doc.brief].reject(&:blank?).first
8
+ end
9
+ end
@@ -1,8 +1,8 @@
1
- class Artist
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
-
5
- slug :name
6
- field :name
7
- has_and_belongs_to_many :artworks
8
- end
1
+ class Artist
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ slug :name
6
+ field :name
7
+ has_and_belongs_to_many :artworks
8
+ end
@@ -1,10 +1,10 @@
1
- class Artwork
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
-
5
- slug :title
6
- field :title
7
- field :published, type: Boolean, default: true
8
- scope :published, -> { where(published: true) }
9
- has_and_belongs_to_many :artists
10
- end
1
+ class Artwork
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ slug :title
6
+ field :title
7
+ field :published, type: Boolean, default: true
8
+ scope :published, -> { where(published: true) }
9
+ has_and_belongs_to_many :artists
10
+ end
@@ -1,15 +1,15 @@
1
- class Author
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :first_name
5
- field :last_name
6
- if Mongoid::Compatibility::Version.mongoid6?
7
- belongs_to :book, required: false
8
- else
9
- belongs_to :book
10
- end
11
- has_many :characters,
12
- class_name: 'Person',
13
- foreign_key: :author_id
14
- slug :first_name, :last_name, scope: :book, history: false, max_length: 256
15
- end
1
+ class Author
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :first_name
5
+ field :last_name
6
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
7
+ belongs_to :book, required: false
8
+ else
9
+ belongs_to :book
10
+ end
11
+ has_many :characters,
12
+ class_name: 'Person',
13
+ foreign_key: :author_id
14
+ slug :first_name, :last_name, scope: :book, history: false, max_length: 256
15
+ end
@@ -1,15 +1,15 @@
1
- class AuthorPolymorphic
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :first_name
5
- field :last_name
6
- slug :first_name, :last_name, scope: :book_polymorphic
7
- if Mongoid::Compatibility::Version.mongoid6?
8
- belongs_to :book_polymorphic, required: false
9
- else
10
- belongs_to :book_polymorphic
11
- end
12
- has_many :characters,
13
- class_name: 'Person',
14
- foreign_key: :author_id
15
- end
1
+ class AuthorPolymorphic
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :first_name
5
+ field :last_name
6
+ slug :first_name, :last_name, scope: :book_polymorphic
7
+ if Mongoid::Compatibility::Version.mongoid6_or_newer?
8
+ belongs_to :book_polymorphic, required: false
9
+ else
10
+ belongs_to :book_polymorphic
11
+ end
12
+ has_many :characters,
13
+ class_name: 'Person',
14
+ foreign_key: :author_id
15
+ end
data/spec/models/book.rb CHANGED
@@ -1,12 +1,12 @@
1
- class Book
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :title
5
-
6
- slug :title, history: true, max_length: nil
7
- embeds_many :subjects
8
- has_many :authors
9
- end
10
-
11
- class ComicBook < Book
12
- end
1
+ class Book
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title
5
+
6
+ slug :title, history: true, max_length: nil
7
+ embeds_many :subjects
8
+ has_many :authors
9
+ end
10
+
11
+ class ComicBook < Book
12
+ end
@@ -1,12 +1,12 @@
1
- class BookPolymorphic
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :title
5
-
6
- slug :title, history: true, by_model_type: true
7
- embeds_many :subjects
8
- has_many :author_polymorphics
9
- end
10
-
11
- class ComicBookPolymorphic < BookPolymorphic
12
- end
1
+ class BookPolymorphic
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title
5
+
6
+ slug :title, history: true, by_model_type: true
7
+ embeds_many :subjects
8
+ has_many :author_polymorphics
9
+ end
10
+
11
+ class ComicBookPolymorphic < BookPolymorphic
12
+ end
@@ -1,17 +1,17 @@
1
- class Caption
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :my_identity, type: String
5
- field :title
6
- field :medium
7
-
8
- # A fairly complex scenario, where we want to create a slug out of an
9
- # my_identity field, which comprises name of artist and some more bibliographic
10
- # info in parantheses, and the title of the work.
11
- #
12
- # We are only interested in the name of the artist so we remove the
13
- # paranthesized details.
14
- slug :my_identity, :title do |cur_object|
15
- cur_object.slug_builder.gsub(/\s*\([^)]+\)/, '').to_url
16
- end
17
- end
1
+ class Caption
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :my_identity, type: String
5
+ field :title
6
+ field :medium
7
+
8
+ # A fairly complex scenario, where we want to create a slug out of an
9
+ # my_identity field, which comprises name of artist and some more bibliographic
10
+ # info in parantheses, and the title of the work.
11
+ #
12
+ # We are only interested in the name of the artist so we remove the
13
+ # paranthesized details.
14
+ slug :my_identity, :title do |cur_object|
15
+ cur_object.slug_builder.gsub(/\s*\([^)]+\)/, '').to_url
16
+ end
17
+ end
@@ -1,12 +1,11 @@
1
- # encoding: utf-8
2
- class Entity
3
- include Mongoid::Document
4
- include Mongoid::Slug
5
-
6
- field :_id, type: String, slug_id_strategy: UuidIdStrategy
7
-
8
- field :name
9
- field :user_edited_variation
10
-
11
- slug :user_edited_variation, history: true
12
- end
1
+ class Entity
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ field :_id, type: String, slug_id_strategy: UuidIdStrategy
6
+
7
+ field :name
8
+ field :user_edited_variation
9
+
10
+ slug :user_edited_variation, history: true
11
+ end
@@ -1,7 +1,7 @@
1
- class Friend
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :name
5
- field :slug_history, type: Array
6
- slug :name, reserve: ['foo', 'bar', /^[a-z]{2}$/i], history: true
7
- end
1
+ class Friend
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :name
5
+ field :slug_history, type: Array
6
+ slug :name, reserve: ['foo', 'bar', /^[a-z]{2}$/i], history: true
7
+ end
@@ -1,9 +1,9 @@
1
- class IncorrectSlugPersistence
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
-
5
- field :name
6
- slug :name, history: true
7
-
8
- validates_length_of :name, minimum: 4, maximum: 5, allow_blank: true
9
- end
1
+ class IncorrectSlugPersistence
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ field :name
6
+ slug :name, history: true
7
+
8
+ validates_length_of :name, minimum: 4, maximum: 5, allow_blank: true
9
+ end
@@ -1,9 +1,9 @@
1
- class IntegerId
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
-
5
- field :_id, type: Integer
6
- field :name, type: String
7
-
8
- slug :name, history: true
9
- end
1
+ class IntegerId
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ field :_id, type: Integer
6
+ field :name, type: String
7
+
8
+ slug :name, history: true
9
+ end
@@ -1,7 +1,7 @@
1
- class Magazine
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :title
5
- field :publisher_id
6
- slug :title, scope: :publisher_id
7
- end
1
+ class Magazine
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title
5
+ field :publisher_id
6
+ slug :title, scope: :publisher_id
7
+ end
data/spec/models/page.rb CHANGED
@@ -1,9 +1,9 @@
1
- class Page
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :title
5
- field :content
6
- field :order, type: Integer
7
- slug :title
8
- default_scope -> { asc(:order) }
9
- end
1
+ class Page
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title
5
+ field :content
6
+ field :order, type: Integer
7
+ slug :title
8
+ default_scope -> { asc(:order) }
9
+ end
@@ -1,9 +1,9 @@
1
- class PageLocalize
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :title, localize: true
5
- field :content
6
- field :order, type: Integer
7
- slug :title
8
- default_scope -> { asc(:order) }
9
- end
1
+ class PageLocalize
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title, localize: true
5
+ field :content
6
+ field :order, type: Integer
7
+ slug :title
8
+ default_scope -> { asc(:order) }
9
+ end
@@ -1,9 +1,9 @@
1
- class PageSlugLocalized
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
- field :title, localize: true
5
- field :content
6
- field :order, type: Integer
7
- slug :title, localize: true
8
- default_scope -> { asc(:order) }
9
- end
1
+ class PageSlugLocalized
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title, localize: true
5
+ field :content
6
+ field :order, type: Integer
7
+ slug :title, localize: true
8
+ default_scope -> { asc(:order) }
9
+ end
@@ -1,10 +1,10 @@
1
- class PageSlugLocalizedCustom
2
- include Mongoid::Document
3
- include Mongoid::Slug
4
-
5
- attr_accessor :title
6
-
7
- slug :title, localize: true do |obj|
8
- obj.title.to_url
9
- end
10
- end
1
+ class PageSlugLocalizedCustom
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+
5
+ attr_accessor :title
6
+
7
+ slug :title, localize: true do |obj|
8
+ obj.title.to_url
9
+ end
10
+ end