Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop support for Mongoid 6 and Ruby 2.4 #270

Merged
merged 7 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,10 @@ jobs:
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0, jruby, truffleruby]
ruby: [2.5, 2.6, 2.7, 3.0, jruby, truffleruby]
mongoid: [7]
experimental: [false]
include:
- ruby: 2.1
mongoid: 3
experimental: false
- ruby: 2.2
mongoid: 3
experimental: false
- ruby: 2.3
mongoid: 3
experimental: false
# - ruby: 2.3
# mongoid: 4
# experimental: false
- ruby: 2.3
mongoid: 5
experimental: false
- ruby: 2.3
mongoid: 6
experimental: false
- ruby: 2.7
mongoid: HEAD
experimental: true
Expand Down
17 changes: 14 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- vendor/**/*
TargetRubyVersion: 2.5
NewCops: enable
SuggestExtensions: false

Style/Documentation:
Enabled: false

Style/DoubleNegation:
Enabled: false

Style/ModuleFunction:
EnforcedStyle: extend_self

inherit_from: .rubocop_todo.yml
Style/OptionalBooleanParameter:
Enabled: false

Style/Semicolon:
Exclude:
- 'spec/**/*.rb'
68 changes: 24 additions & 44 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,73 +1,53 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-08-16 08:14:07 -0400 using RuboCop version 0.58.2.
# on 2021-08-11 17:20:10 UTC using RuboCop version 1.18.4.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 6
# Configuration parameters: Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/DuplicatedGem:
# Offense count: 5
# Configuration parameters: AllowedMethods.
# AllowedMethods: enums
Lint/ConstantDefinitionInBlock:
Exclude:
- 'Gemfile'
- 'spec/mongoid/slug_spec.rb'
- 'spec/tasks/mongoid_slug_rake_spec.rb'

# Offense count: 5
# Offense count: 4
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
Metrics/AbcSize:
Max: 40
Max: 44

# Offense count: 23
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
# Offense count: 24
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
# IgnoredMethods: refine
Metrics/BlockLength:
Max: 532
Max: 525

# Offense count: 2
# Configuration parameters: IgnoredMethods.
Metrics/CyclomaticComplexity:
Max: 9
Max: 10

# Offense count: 6
# Configuration parameters: CountComments.
# Offense count: 7
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 23

# Offense count: 2
# Configuration parameters: CountComments.
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 959
Max: 937

# Offense count: 2
# Offense count: 1
# Configuration parameters: IgnoredMethods.
Metrics/PerceivedComplexity:
Max: 10
Max: 11

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: braces, no_braces, context_dependent
Style/BracesAroundHashParameters:
Exclude:
- 'lib/mongoid/slug/criteria.rb'

# Offense count: 1
# Configuration parameters: AllowOnConstant.
Style/CaseEquality:
Exclude:
- 'lib/mongoid/slug/unique_slug.rb'

# Offense count: 1
Style/DoubleNegation:
Exclude:
- 'lib/mongoid/slug.rb'

# Offense count: 9
# Cop supports --auto-correct.
# Configuration parameters: AllowAsExpressionSeparator.
Style/Semicolon:
Exclude:
- 'spec/mongoid/criteria_spec.rb'

# Offense count: 151
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 138
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 6.0.2 (Next)
## 7.0.0 (Next)

* Your contribution here.
* [#268](https://github.com/mongoid/mongoid-slug/pull/268): Ensure localized slugs index expected localized slug fields - [@johnnyshields](https://github.com/johnnyshields).

## 6.0.1 (2021/08/12)

Expand Down
2 changes: 2 additions & 0 deletions Dangerfile
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# frozen_string_literal: true

danger.import_dangerfile(gem: 'mongoid-danger')
13 changes: 4 additions & 9 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec name: 'mongoid-slug'
Expand All @@ -7,19 +9,12 @@ when 'HEAD'
gem 'mongoid', github: 'mongodb/mongoid'
when /^7/
gem 'mongoid', '~> 7'
when /^6/
gem 'mongoid', '~> 6'
when /^5/
gem 'mongoid', '~> 5'
when /^4/
gem 'mongoid', '~> 4'
when /^3/
gem 'mongoid', '~> 3'
else
gem 'mongoid', version
end

group :test do
gem 'mongoid-danger', '~> 0.1.0', require: false
gem 'rubocop', '0.57.2'
gem 'rubocop', '~> 1.18.4'
gem 'rubocop-rspec'
end
6 changes: 0 additions & 6 deletions Guardfile

This file was deleted.

17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ It sits idly on top of [stringex](https://github.com/rsl/stringex), supporting n
[![Gem Version](https://badge.fury.io/rb/mongoid-slug.svg)](http://badge.fury.io/rb/mongoid-slug)
[![Code Climate](https://codeclimate.com/github/mongoid/mongoid-slug.svg)](https://codeclimate.com/github/mongoid/mongoid-slug)

### Version Support

Mongoid Slug 7.x requires at least Mongoid 7.0.0 and Ruby 2.5.0. For earlier Mongoid and Ruby version support, please use an earlier version of Mongoid Slug.

Mongoid Slug is compatible with all MongoDB versions which Mongoid supports, however, please see "Slug Max Length" section below for MongoDB 4.0 and earlier.

### Installation

Add to your Gemfile:
Expand Down Expand Up @@ -169,9 +175,16 @@ class Employee
end
```

### Limit Slug Length
### Slug Max Length

MongoDB has a default limit around 1KB to the size of the index keys and will raise error 17280, `key too large to index` when trying to create a record that causes an index key to exceed that limit. By default slugs are of the form `text[-number]` and the text portion is limited in size to `Mongoid::Slug::MONGO_INDEX_KEY_LIMIT_BYTES - 32` bytes. You can change this limit with `max_length` or set it to `nil` if you're running MongoDB with [failIndexKeyTooLong](https://docs.mongodb.org/manual/reference/parameters/#param.failIndexKeyTooLong) set to `false`.
MongoDB [featureCompatibilityVersion](https://docs.mongodb.com/manual/reference/command/setFeatureCompatibilityVersion/#std-label-view-fcv)
"4.0" and earlier applies an [Index Key Limit](https://docs.mongodb.com/manual/reference/limits/#mongodb-limit-Index-Key-Limit)
which limits the total size of an index entry to around 1KB and will raise error,
`17280 - key too large to index` when trying to create a record that causes an index key to exceed that limit.
By default slugs are of the form `text[-number]` and the text portion is limited in size
to `Mongoid::Slug::MONGO_INDEX_KEY_LIMIT_BYTES - 32` bytes.
You can change this limit with `max_length` or set it to `nil` if you're running MongoDB
with [failIndexKeyTooLong](https://docs.mongodb.org/manual/reference/parameters/#param.failIndexKeyTooLong) set to `false`.

```ruby
class Company
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler'
require 'rspec/core/rake_task'

Expand Down
6 changes: 6 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### Upgrading to 7.0.0

#### Support dropped for Mongoid < 7 and Ruby < 2.5

Please upgrade to at least Mongoid 7.0 and Ruby 2.5.

### Upgrading to 6.0.0

#### Support dropped for mongoid_paranoia
Expand Down
21 changes: 12 additions & 9 deletions lib/mongoid/slug.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require 'mongoid'
require 'stringex'
require 'mongoid/slug/criteria'
require 'mongoid/slug/index_builder'
require 'mongoid/slug/unique_slug'
require 'mongoid/slug/slug_id_strategy'
require 'mongoid-compatibility'
require 'mongoid/slug/railtie' if defined?(Rails)

module Mongoid
Expand All @@ -29,6 +30,7 @@ module Slug

class << self
attr_accessor :default_slug

def configure(&block)
instance_eval(&block)
end
Expand Down Expand Up @@ -85,7 +87,10 @@ def slug(*fields, &block)
alias_attribute :slugs, :_slugs

# Set indexes
Mongoid::Slug::IndexBuilder.build_indexes(self, slug_scope_key, slug_by_model_type, options[:localize]) unless embedded?
unless embedded?
Mongoid::Slug::IndexBuilder.build_indexes(self, slug_scope_key, slug_by_model_type,
options[:localize])
end

self.slug_url_builder = block_given? ? block : default_slug_url_builder

Expand All @@ -111,6 +116,7 @@ def look_like_slugs?(*args)
# @return [ Array<Document>, Document ]
def slug_scope_key
return nil unless slug_scope

reflect_on_association(slug_scope).try(:key) || slug_scope
end

Expand Down Expand Up @@ -141,17 +147,13 @@ def queryable

private

if Mongoid::Compatibility::Version.mongoid5_or_newer? && Threaded.method(:current_scope).arity == -1
if Threaded.method(:current_scope).arity == -1
def current_scope
Threaded.current_scope(self)
end
elsif Mongoid::Compatibility::Version.mongoid5_or_newer?
def current_scope
Threaded.current_scope
end
else
def current_scope
scope_stack.last
Threaded.current_scope
end
end
end
Expand Down Expand Up @@ -184,7 +186,7 @@ def apply_slug
return true if new_slug.size.zero?

# avoid duplicate slugs
_slugs.delete(new_slug) if _slugs
_slugs&.delete(new_slug)

if !!slug_history && _slugs.is_a?(Array)
append_slug(new_slug)
Expand Down Expand Up @@ -249,6 +251,7 @@ def to_param
# @return [String] the slug, or nil if the document does not have a slug.
def slug
return _slugs.last if _slugs

_id.to_s
end

Expand Down
16 changes: 10 additions & 6 deletions lib/mongoid/slug/criteria.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Mongoid
module Slug
class Criteria < Mongoid::Criteria
Expand Down Expand Up @@ -50,6 +52,7 @@ def find_by_slug!(*args)

def look_like_slugs?(args)
return false unless args.all? { |id| id.is_a?(String) }

id_field = @klass.fields['_id']
@slug_strategy ||= id_field.options[:slug_id_strategy] || build_slug_strategy(id_field.type)
args.none? { |id| @slug_strategy.call(id) }
Expand All @@ -61,13 +64,13 @@ def look_like_slugs?(args)
# use object_id or string strategy depending on the id_type
# otherwise default for all other id_types
def build_slug_strategy(id_type)
type_method = id_type.to_s.downcase.split('::').last + '_slug_strategy'
type_method = "#{id_type.to_s.downcase.split('::').last}_slug_strategy"
respond_to?(type_method, true) ? method(type_method) : ->(_id) { false }
end

# a string will not look like a slug if it looks like a legal BSON::ObjectId
def objectid_slug_strategy(id)
Mongoid::Compatibility::ObjectId.legal?(id)
BSON::ObjectId.legal?(id)
end

# a string will always look like a slug
Expand All @@ -78,10 +81,10 @@ def string_slug_strategy(_id)
def for_slugs(slugs)
# _translations
localized = (begin
@klass.fields['_slugs'].options[:localize]
rescue StandardError
false
end)
@klass.fields['_slugs'].options[:localize]
rescue StandardError
false
end)
if localized
def_loc = I18n.default_locale
query = { '$in' => slugs }
Expand All @@ -100,6 +103,7 @@ def execute_or_raise_for_slugs(slugs, multi)
def check_for_missing_documents_for_slugs!(result, slugs)
missing_slugs = slugs - result.map(&:slugs).flatten
return unless !missing_slugs.blank? && Mongoid.raise_not_found_error

raise Errors::DocumentNotFound.new(klass, slugs, missing_slugs)
end
end
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/slug/index_builder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Mongoid
module Slug
module IndexBuilder
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/slug/railtie.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Mongoid
module Slug
class Railtie < Rails::Railtie
Expand Down
2 changes: 2 additions & 0 deletions lib/mongoid/slug/slug_id_strategy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

Mongoid::Fields.option(:slug_id_strategy) do |_model, field, value|
field.options[:slug_id_strategy] = value
end
Loading