Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using auth_devise with discard #209

Merged
merged 1 commit into from
May 17, 2021
Merged
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
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