Forked from
PortalMEC / portalmec
4 commits behind, 77 commits ahead of the upstream repository.
user_serializer.rb 3.14 KiB
# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
class UserSerializer < ActiveModel::Serializer
cache key: 'user', expires_in: 4.hours, except: [:email, :complained, :followed, :level, :level_xp, :experience, :points ]
def complained
object.complained? current_user
end
def followed
current_user.try( :following?, object)
end
def email
object.email if is_current_user?
end
def learning_objects_count
object.learning_objects.where('state = ?', LearningObject.states[:published]).count
end
def role_ids
roles = []
object.roles.each { |role| roles << role.id }
roles
end
def dspace_handle
object.dspace_handle if is_current_user?
end
def dspace_url
object.dspace_url if is_current_user?
end
def dspace_sets
object.dspace_sets if is_current_user?
end
def cpf
object.cpf if is_current_user? || is_supervisor?
end
def school
object.school if is_current_user? || is_supervisor?
end
def is_current_user?
(!current_user.nil?)&&(object.id == current_user.id || current_user.can_edit?)
end
def is_supervisor?
!current_user.nil? && current_user.is_supervisor?
end
def level_xp
object.xp_to_level(object.level + 1)
end
def user_items
items = object.user_items.where(being_used: true)
ActiveModel::SerializableResource.new(items, {scope: current_user, scope_name: :current_user, each_serializer: ::UserItemSerializer}).serializable_hash
end
attributes \
:id,
:email,
:provider,
:cpf,
:name,
:description,
:submitter_request,
:education,
:score,
:cover,
:role_ids,
:institution_ids,
:avatar,
:dspace_url,
:dspace_handle,
:dspace_sets,
:likes_count,
:followed,
:complained,
:follows_count,
:learning_objects_count,
:collections_count,
:created_at,
:updated_at,
:terms_accepted_at,
:state,
:level,
:level_xp,
:experience,
:points,
:last_action_at,
:streak,
:high_streak
attribute \
:times_blocked, if: :is_current_user?
attribute \
:suspended_at, if: :is_current_user?
attribute \
:reactivated_at, if: :is_current_user?
attribute \
:ask_teacher_question, if: :is_current_user?
belongs_to :school
has_many :subjects
has_many :roles
has_many :institutions
has_many :user_items
end