-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
837c8ee
commit 4bfd30d
Showing
6 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
class Agent | ||
include Mongoid::Document | ||
include Mongoid::Slug | ||
include Mongoid::Paranoia | ||
|
||
field :name | ||
slug :name, permanent: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
Mongoid::Slug.use_paranoia = true | ||
class SecretAgent | ||
include Mongoid::Document | ||
include Mongoid::Slug | ||
include Mongoid::Paranoia | ||
|
||
field :name | ||
slug :name, permanent: true | ||
end | ||
Mongoid::Slug.use_paranoia = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Mongoid::Slug do | ||
context 'when paranoia support is not enabled' do | ||
let!(:doc) { Agent.create(name: 'Garbo') } | ||
|
||
it 'does not delete the slug when the document is destroyed' do | ||
expect(doc.slug).to eq 'garbo' | ||
doc.destroy | ||
expect(Agent.unscoped.find(doc._id).slug).to eq 'garbo' | ||
end | ||
end | ||
|
||
context 'when paranoia support is enabled' do | ||
let!(:doc) { SecretAgent.create(name: 'Alaric') } | ||
|
||
around do |example| | ||
Mongoid::Slug.use_paranoia = true | ||
example.run | ||
ensure | ||
Mongoid::Slug.use_paranoia = false | ||
end | ||
|
||
it 'deletes the slug when the document is destroyed' do | ||
expect(doc.slug).to eq 'alaric' | ||
doc.destroy | ||
# TODO: This case should be nil. | ||
expect(SecretAgent.unscoped.find(doc._id).slug).to eq doc._id.to_s | ||
doc.restore | ||
expect(SecretAgent.unscoped.find(doc._id).slug).to eq 'alaric' | ||
end | ||
|
||
it 'deletes the slug when deleted document is saved' do | ||
expect(doc.slug).to eq 'alaric' | ||
doc.deleted_at = Time.current | ||
doc.save! | ||
expect(SecretAgent.unscoped.find(doc._id).slug).to eq nil | ||
# TODO: This case should re-set the slug. | ||
doc.deleted_at = nil | ||
doc.save! | ||
expect(SecretAgent.unscoped.find(doc._id).slug).to eq nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters