Skip to content

Commit

Permalink
Add specs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields committed Sep 18, 2023
1 parent 837c8ee commit 4bfd30d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ else
gem 'mongoid', version
end

gem 'mongoid_paranoia', '>= 0.6'
gem 'rake'
gem 'rspec'
gem 'rspec-its'
Expand Down
3 changes: 1 addition & 2 deletions lib/mongoid/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def slug(*fields, &block)
if __slug_paranoid_doc? # rubocop:disable Style/GuardClause
set_callback :destroy, :after, :unset_slug!
set_callback :restore, :before, :set_slug!
set_callback :save, :before, :reset_slug!, if: :slug_paranoid_deleted?
set_callback :save, :after, :clear_slug!, if: :slug_paranoid_deleted?
set_callback :save, :before, :clear_slug!, if: :slug_paranoid_deleted?
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/models/agent.rb
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
12 changes: 12 additions & 0 deletions spec/models/secret_agent.rb
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
46 changes: 46 additions & 0 deletions spec/mongoid/paranoia_spec.rb
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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'active_support'
require 'active_support/deprecation'
require 'mongoid'
require 'mongoid/paranoia'

require File.expand_path '../lib/mongoid/slug', __dir__

Expand Down

0 comments on commit 4bfd30d

Please sign in to comment.