Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 7.1.1 (Next)

* [#184](https://github.com/mongoid/mongoid-slug/issues/184): Preserve default-locale slug history when another locale uses the same title through I18n fallbacks - [@dblock](https://github.com/dblock).
* [#284](https://github.com/mongoid/mongoid-slug/pull/284): Cover saved slug mutation, suggestions, history-state reporting, require aliases and real Rails task registration; combine supported-version coverage - [@dblock](https://github.com/dblock).
* Removed Code Climate badge from README - [@dblock](https://github.com/dblock).
* Your contribution here.
Expand Down
7 changes: 3 additions & 4 deletions lib/mongoid/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ def apply_slug
# to find document instead
return true if new_slug.empty?

# avoid duplicate slugs
_slugs&.delete(new_slug)

if !!slug_history && _slugs.is_a?(Array)
append_slug(new_slug)
else
Expand Down Expand Up @@ -284,10 +281,12 @@ def append_slug(value)
# This is necessary for the scenario in which the slugged locale is not yet present
# but the default locale is. In this situation, self._slugs falls back to the default
# which is undesired
current_slugs = _slugs_translations.fetch(I18n.locale.to_s, [])
current_slugs = _slugs_translations.fetch(I18n.locale.to_s, []).dup
current_slugs.delete(value)
current_slugs << value
self._slugs_translations = _slugs_translations.merge(I18n.locale.to_s => current_slugs)
else
_slugs.delete(value)
_slugs << value
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/mongoid/localized_fallback_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require 'spec_helper'
require 'i18n/backend/fallbacks'

describe 'Localized slug history with fallbacks' do
around do |example|
original_fallbacks = I18n.fallbacks
I18n.fallbacks = I18n::Locale::Fallbacks.new(nl: :en)
I18n.with_locale(:en) { example.run }
ensure
I18n.fallbacks = original_fallbacks
end

it 'preserves the default locale when adding the same title in another locale' do
page = PageSlugLocalizedHistory.create!(title: 'Shared Title')

I18n.with_locale(:nl) { page.update!(title: 'Shared Title') }

expect(page.reload._slugs_translations).to eq(
'en' => ['shared-title'], 'nl' => ['shared-title']
)
end

it 'keeps independent histories without duplicate current slugs' do
page = PageSlugLocalizedHistory.create!(title: 'Original Title')
I18n.with_locale(:nl) { page.update!(title: 'Original Title') }
I18n.with_locale(:nl) { page.update!(title: 'Dutch Title') }
I18n.with_locale(:nl) { page.update!(title: 'Original Title') }

expect(page.reload._slugs_translations).to eq(
'en' => ['original-title'], 'nl' => %w[dutch-title original-title]
)
end
end
Loading