Skip to content

Commit

Permalink
Allow using auth_devise with discard
Browse files Browse the repository at this point in the history
paranoia is not required anymore since 3.0 ships without it.
Who is using older versions of Solidus will see the deprecation
as the rest of the paranoia code in core.
  • Loading branch information
kennyadsl committed Apr 21, 2021
1 parent 09841c1 commit 11b9fa9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
14 changes: 11 additions & 3 deletions app/models/spree/user.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'paranoia'

module Spree
class User < Spree::Base
include UserMethods
Expand All @@ -10,8 +8,18 @@ class User < Spree::Base
:rememberable, :trackable, :validatable, :encryptable
devise :confirmable if Spree::Auth::Config[:confirmable]

acts_as_paranoid
if defined?(Spree::SoftDeletable)
include Spree::SoftDeletable
else
acts_as_paranoid
include Spree::ParanoiaDeprecations

include Discard::Model
self.discard_column = :deleted_at
end

after_destroy :scramble_email_and_password
after_discard :scramble_email_and_password

def password=(new_password)
generate_spree_api_key if new_password.present? && spree_api_key.present?
Expand Down
1 change: 0 additions & 1 deletion solidus_auth_devise.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ Gem::Specification.new do |s|
s.add_dependency "deface", "~> 1.0"
s.add_dependency "devise", '~> 4.1'
s.add_dependency "devise-encryptable", "0.2.0"
s.add_dependency "paranoia", "~> 2.4"
s.add_dependency "solidus_core", solidus_version
s.add_dependency "solidus_support", "~> 0.5"

Expand Down
28 changes: 16 additions & 12 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,8 @@
end

describe '#destroy' do
# Users with orders are not deletable in Solidus core
# therefore we cannot test this behaviour here.
# Also there are already sufficient specs in core.
let(:user) { create(:user) }

it 'acts_as_paranoid' do
# Instead of testing implementation details of `acts_as_paranoid`
# we are testing that we are using `acts_as_paranoid` by using duck typing
expect(described_class).to respond_to(:with_deleted)
expect(user).to respond_to(:deleted_at)
end

context 'with same email address as previously deleted account' do
it 'will allow users to register later' do
user1 = build(:user)
Expand All @@ -72,13 +62,27 @@
expect(user2.save).to be false
expect(user2.errors.messages[:email].first).to eq "has already been taken"

user1.destroy
user1.discard
expect(user2.save).to be true
end
end
end

describe '#really_destroy!' do
describe '#destroy' do
let(:user) { create(:user) }

it 'removes the record from the database' do
user.destroy

if defined?(Spree::ParanoiaDeprecations)
expect(Spree::User.with_discarded.exists?(id: user.id)).to eql true
else
expect(Spree::User.with_discarded.exists?(id: user.id)).to eql false
end
end
end

describe '#really_destroy!', if: defined?(Spree::ParanoiaDeprecations) do
let(:user) { create(:user) }

it 'removes the record from the database' do
Expand Down

0 comments on commit 11b9fa9

Please sign in to comment.