Add disabled_at logic on spree users

This commit is contained in:
François Turbelin
2022-04-26 00:02:34 +02:00
committed by Jean-Baptiste Bellet
parent f6e2cb52e0
commit 39a6b5d20f
7 changed files with 75 additions and 3 deletions

View File

@@ -144,6 +144,32 @@ module Spree
"#{self.class.name};#{id}"
end
def disabled?
disabled_at.present?
end
def disable!
self.disabled_at = Time.zone.now
save!
end
def enable!
self.disabled_at = nil
save!
end
def toggle_disable
disabled?
end
def toggle_disable=(value)
if value == '1'
disable!
else
enable!
end
end
protected
def password_required?

View File

@@ -15,7 +15,7 @@ module PermittedAttributes
private
def permitted_attributes
[:email, :password, :password_confirmation]
[:email, :password, :password_confirmation, :toggle_disable]
end
end
end

View File

@@ -23,4 +23,11 @@
= f.field_container :password do
= f.label :password_confirmation, t(".confirm_password")
= f.password_field :password_confirmation, class: "fullwidth"
= f.error_message_on :password_confirmation
= f.error_message_on :password_confirmation
= f.field_container :disable do
= f.label :disable, t(".disable")
=# check_box_tag "user[toggle_disable]", "1", @user.disabled?, id: "user_toggle_disable"
= f.check_box :toggle_disable
= f.error_message_on :disable
- if @user.disabled?
= label_tag @user.disabled_at

View File

@@ -3965,6 +3965,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using
back_to_users_list: "Back To Users List"
general_settings: "General Settings"
form:
disable: "Disable?"
email: "Email"
roles: "Roles"
enterprise_limit: "Enterprise Limit"

View File

@@ -0,0 +1,5 @@
class AddDisabledAtToSpreeUsers < ActiveRecord::Migration[6.1]
def up
add_column :spree_users, :disabled_at, :datetime
end
end

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_06_02_013938) do
ActiveRecord::Schema.define(version: 2022_06_21_230907) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -1055,6 +1055,7 @@ ActiveRecord::Schema.define(version: 2022_06_02_013938) do
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email", limit: 255
t.datetime "disabled_at"
t.index ["confirmation_token"], name: "index_spree_users_on_confirmation_token", unique: true
t.index ["email"], name: "email_idx_unique", unique: true
t.index ["persistence_token"], name: "index_users_on_persistence_token"

View File

@@ -201,4 +201,36 @@ describe Spree::User do
expect(user.flipper_id).to eq "Spree::User;42"
end
end
describe "#disable!" do
it "sets disabled datetime" do
user = create(:user)
expect(user.disabled_at).to be_nil
user.disable!
expect(user.disabled_at).not_to be_nil
end
end
describe "#enable!" do
it "clears disabled datetime" do
user = create(:user, disabled_at: Time.zone.now)
expect(user.disabled_at).not_to be_nil
user.enable!
expect(user.disabled_at).to be_nil
end
end
describe "#disabled?" do
it "returns true with a disabled datetime" do
user = create(:user)
user.disable!
expect(user.disabled?).to be_truthy
end
it "returns false without a disabled datetime" do
user = create(:user)
user.enable!
expect(user.disabled?).to be_falsey
end
end
end