serialize can_be_deleted field only when needed

This commit is contained in:
OsamaSayegh 2024-11-21 01:46:10 +03:00
parent 728fcab49d
commit 0369f9a4a5
No known key found for this signature in database
GPG Key ID: 060E5AC82223685F
3 changed files with 25 additions and 1 deletions

View File

@ -32,7 +32,7 @@ class Admin::UsersController < Admin::StaffController
def index
users = ::AdminUserIndexQuery.new(params).find_users
opts = {}
opts = { include_can_be_deleted: true }
if params[:show_emails] == "true"
StaffActionLogger.new(current_user).log_show_emails(users, context: request.path)
opts[:emails_desired] = true

View File

@ -116,4 +116,8 @@ class AdminUserListSerializer < BasicUserSerializer
def can_be_deleted
scope.can_delete_user?(object)
end
def include_can_be_deleted?
@options[:include_can_be_deleted]
end
end

View File

@ -89,4 +89,24 @@ RSpec.describe AdminUserListSerializer do
expect(json[:secondary_emails]).to contain_exactly("first@email.com", "second@email.com")
end
end
describe "#can_be_deleted" do
it "is not included if the include_can_be_deleted option is not present" do
json = AdminUserListSerializer.new(user, scope: guardian, root: false).as_json
expect(json.key?(:can_be_deleted)).to eq(false)
end
it "is included if the include_can_be_deleted option is true" do
json =
AdminUserListSerializer.new(
user,
scope: guardian,
root: false,
include_can_be_deleted: true,
).as_json
expect(json[:can_be_deleted]).to eq(true)
end
end
end