2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-16 15:14:33 -04:00
|
|
|
class DirectoryItemSerializer < ApplicationSerializer
|
2018-12-06 06:18:52 -05:00
|
|
|
class UserSerializer < UserNameSerializer
|
|
|
|
include UserPrimaryGroupMixin
|
2021-06-07 13:34:01 -04:00
|
|
|
|
|
|
|
attributes :user_fields
|
|
|
|
|
|
|
|
def user_fields
|
2021-06-23 11:45:18 -04:00
|
|
|
fields = {}
|
2024-11-06 12:35:30 -05:00
|
|
|
user_custom_field_map = @options[:user_custom_field_map] || {}
|
|
|
|
searchable_fields = @options[:searchable_fields] || []
|
2021-06-23 11:45:18 -04:00
|
|
|
|
2024-11-06 12:35:30 -05:00
|
|
|
object.user_custom_fields.each do |custom_field|
|
|
|
|
user_field_id = user_custom_field_map[custom_field.name]
|
|
|
|
next unless user_field_id
|
|
|
|
|
|
|
|
current_value = fields.dig(user_field_id, :value)
|
|
|
|
|
|
|
|
current_value = Array(current_value) if current_value
|
|
|
|
|
|
|
|
new_value = current_value ? current_value << custom_field.value : custom_field.value
|
|
|
|
|
|
|
|
is_searchable = searchable_fields.any? { |field| field.id == user_field_id }
|
|
|
|
|
|
|
|
fields[user_field_id] = {
|
|
|
|
value: new_value.is_a?(Array) ? new_value : [new_value],
|
|
|
|
searchable: is_searchable,
|
|
|
|
}
|
2021-06-23 11:45:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
fields
|
2021-06-07 13:34:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_user_fields?
|
2021-06-23 11:45:18 -04:00
|
|
|
@options[:user_custom_field_map].present?
|
2021-06-07 13:34:01 -04:00
|
|
|
end
|
2018-12-06 06:18:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
has_one :user, embed: :objects, serializer: UserSerializer
|
2021-06-23 15:55:17 -04:00
|
|
|
|
|
|
|
attributes :id
|
2015-03-16 15:14:33 -04:00
|
|
|
|
|
|
|
def id
|
|
|
|
object.user_id
|
|
|
|
end
|
|
|
|
|
2021-06-23 15:55:17 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
hash = super
|
|
|
|
|
|
|
|
@options[:attributes].each { |attr| hash.merge!("#{attr}": object[attr]) }
|
|
|
|
|
|
|
|
if object.period_type == DirectoryItem.period_types[:all]
|
|
|
|
hash.merge!(time_read: object.user_stat.time_read)
|
|
|
|
end
|
2015-03-16 15:14:33 -04:00
|
|
|
|
2021-06-23 15:55:17 -04:00
|
|
|
hash
|
2015-03-16 15:14:33 -04:00
|
|
|
end
|
|
|
|
end
|