From 11b9fa9cd1525b15dd7a7ecbca8d55511ff5528f Mon Sep 17 00:00:00 2001 From: Alberto Vena Date: Wed, 21 Apr 2021 14:51:24 +0200 Subject: [PATCH] Allow using auth_devise with discard 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. --- app/models/spree/user.rb | 14 +++++++++++--- solidus_auth_devise.gemspec | 1 - spec/models/user_spec.rb | 28 ++++++++++++++++------------ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 3e21c52d..ab0fd927 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'paranoia' - module Spree class User < Spree::Base include UserMethods @@ -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? diff --git a/solidus_auth_devise.gemspec b/solidus_auth_devise.gemspec index 81c852d0..9df26f20 100644 --- a/solidus_auth_devise.gemspec +++ b/solidus_auth_devise.gemspec @@ -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" diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e3ab8936..c3e6e544 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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) @@ -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