From 39a6b5d20ff118baa844a98455becf32e762bc5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Tue, 26 Apr 2022 00:02:34 +0200 Subject: [PATCH] Add disabled_at logic on spree users --- app/models/spree/user.rb | 26 +++++++++++++++ app/services/permitted_attributes/user.rb | 2 +- app/views/spree/admin/users/_form.html.haml | 9 +++++- config/locales/en.yml | 1 + ...25230907_add_disabled_at_to_spree_users.rb | 5 +++ db/schema.rb | 3 +- spec/models/spree/user_spec.rb | 32 +++++++++++++++++++ 7 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20220425230907_add_disabled_at_to_spree_users.rb diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index 542e43a0b9..74e3614dd2 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -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? diff --git a/app/services/permitted_attributes/user.rb b/app/services/permitted_attributes/user.rb index 4efbf0e3b5..dd361440ef 100644 --- a/app/services/permitted_attributes/user.rb +++ b/app/services/permitted_attributes/user.rb @@ -15,7 +15,7 @@ module PermittedAttributes private def permitted_attributes - [:email, :password, :password_confirmation] + [:email, :password, :password_confirmation, :toggle_disable] end end end diff --git a/app/views/spree/admin/users/_form.html.haml b/app/views/spree/admin/users/_form.html.haml index 47bf167151..449ff284d0 100644 --- a/app/views/spree/admin/users/_form.html.haml +++ b/app/views/spree/admin/users/_form.html.haml @@ -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 \ No newline at end of file + = 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 \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 3820f0c536..be5ab9ac9c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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" diff --git a/db/migrate/20220425230907_add_disabled_at_to_spree_users.rb b/db/migrate/20220425230907_add_disabled_at_to_spree_users.rb new file mode 100644 index 0000000000..ab7de2c22a --- /dev/null +++ b/db/migrate/20220425230907_add_disabled_at_to_spree_users.rb @@ -0,0 +1,5 @@ +class AddDisabledAtToSpreeUsers < ActiveRecord::Migration[6.1] + def up + add_column :spree_users, :disabled_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index edfd135051..96cab7ce42 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/models/spree/user_spec.rb b/spec/models/spree/user_spec.rb index 0730a050c9..25e87f5bd4 100644 --- a/spec/models/spree/user_spec.rb +++ b/spec/models/spree/user_spec.rb @@ -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