Skip to content
Snippets Groups Projects
Forked from PortalMEC / portalmec
381 commits behind the upstream repository.
publisher_controller.rb 2.88 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/>.

module PublisherController
  extend ActiveSupport::Concern
  include Paginator

  included do
    before_action :authenticate, only: [:show_all_drafts, :show_liked_learning_objects, :show_submitted_learning_objects, :show_liked_collections]
    before_action :set_publisher, only: [:show_all_drafts, :show_all_learning_objects, :show_submitted_learning_objects, :show_all_collections, :show_liked_learning_objects, :show_liked_collections]
    before_action -> { authorize @publisher }, only: [:show_all_drafts, :show_submitted_learning_objects, :show_liked_learning_objects, :show_liked_collections]
  end

  def show_all_drafts
    render json: LearningObject.where(publisher: @publisher, state: LearningObject.states[:draft])
  end

  # GET /v1/users/1/learning_objects
  def show_all_learning_objects
    learning_objects = paginate LearningObject.where(publisher: @publisher, state: LearningObject.states[:published])
    render json: learning_objects
  end

  def show_submitted_learning_objects
    learning_objects = paginate LearningObject.where(publisher: @publisher, state: LearningObject.states[:submitted])
    render json: learning_objects
  end

  def show_all_collections
    collections = paginate ::UserPolicy::Scope.new(current_user, @publisher, Collection).resolve.where(owner: @publisher)
    render json: collections
  end

  def show_liked_learning_objects
    includes = [:taggings, :tags, :subject_relations, :subjects, :stage_relations, :educational_stages, :publisher, :language, :license]
    render json: LearningObject.includes(includes).find(
      Like.where(user: @publisher, likeable_type: 'LearningObject').pluck(:likeable_id)
    )
  end

  def show_liked_collections
    render json: Collection.find(
      Like.where(user: @publisher, likeable_type: 'Collection').pluck(:likeable_id)
    )
  end

  protected

  def authenticate
    authenticate_user!
  end

  def set_publisher
    user, id = request.path.split('/')[2, 2]
    return nil unless %w(users institutions).include? user
    publisher_model = user.singularize.classify.constantize
    @publisher = publisher_model.find(id)
  end
end