Trim WebHookUserSerializer.

This commit is contained in:
Guo Xiang Tan 2018-05-21 11:25:01 +08:00
parent 60e7b13f59
commit 00c6b078e3
3 changed files with 62 additions and 6 deletions

View File

@ -339,22 +339,36 @@ class UserSerializer < BasicUserSerializer
User.system_avatar_template(object.username)
end
def include_gravatar_avatar_upload_id?
object.user_avatar&.gravatar_upload_id
end
def gravatar_avatar_upload_id
object.user_avatar.try(:gravatar_upload_id)
object.user_avatar.gravatar_upload_id
end
def include_gravatar_avatar_template?
include_gravatar_avatar_upload_id?
end
def gravatar_avatar_template
return unless gravatar_upload_id = object.user_avatar.try(:gravatar_upload_id)
User.avatar_template(object.username, gravatar_upload_id)
User.avatar_template(object.username, object.user_avatar.gravatar_upload_id)
end
def include_custom_avatar_upload_id?
object.user_avatar&.custom_upload_id
end
def custom_avatar_upload_id
object.user_avatar.try(:custom_upload_id)
object.user_avatar.custom_upload_id
end
def include_custom_avatar_template?
include_custom_avatar_upload_id?
end
def custom_avatar_template
return unless custom_upload_id = object.user_avatar.try(:custom_upload_id)
User.avatar_template(object.username, custom_upload_id)
User.avatar_template(object.username, object.user_avatar.custom_upload_id)
end
def has_title_badges

View File

@ -5,6 +5,31 @@ class WebHookUserSerializer < UserSerializer
def staff_attributes(*attrs)
end
%i{
can_edit
can_edit_username
can_edit_email
can_edit_name
can_send_private_messages
can_send_private_message_to_user
uploaded_avatar_id
has_title_badges
bio_cooked
custom_fields
can_be_deleted
can_delete_all_posts
system_avatar_upload_id
gravatar_avatar_upload_id
custom_avatar_upload_id
can_change_bio
user_api_keys
group_users
}.each do |attr|
define_method("include_#{attr}?") do
false
end
end
def include_email?
scope.is_admin?
end

View File

@ -18,4 +18,21 @@ RSpec.describe WebHookUserSerializer do
expect(payload[:email]).to eq(user.email)
expect(payload[:external_id]).to eq('12345')
end
it 'should only include the required keys' do
count = serializer.as_json.keys.count
difference = count - 42
expect(difference).to eq(0), lambda {
message = ""
if difference < 0
message << "#{difference * -1} key(s) have been removed from this serializer."
else
message << "#{difference} key(s) have been added to this serializer."
end
message << "\nPlease verify if those key(s) are required as part of the web hook's payload."
}
end
end