Forked from
PortalMEC / portalmec
1821 commits behind the upstream repository.
-
Mateus Rambo Strey authoredMateus Rambo Strey authored
collections_controller.rb 5.84 KiB
class CollectionsController < ApplicationController
include Pundit
before_action :set_collection, only: [:show, :update, :destroy, :like, :change_privacy]
before_action :set_collections, only: [:list, :add_learning_object, :remove_learning_object]
before_action :authenticate_user!, only: [:update, :destroy, :like, :list, :me, :add_learning_object, :remove_learning_object, :change_privacy]
before_action :authorize_action, only: [:show, :create, :update, :destroy, :add_learning_object, :remove_learning_object, :change ]
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
# GET /collections
# GET /collections.json
def index
respond_to do |format|
# for HTML page, returns institutional collections
format.html do
@collections = Collection.all.where(owner: Institution.all)
end
# returns all collection of logged user as JSON for UI actions
if user_signed_in?
format.json do
@collections = current_user.collections
end
end
end
end
# GET /collections/1
# GET /collections/1.json
def show
if @collection.class == User
# bookmark
@user = @collection
@collection = collection_repository.bookmarks(@user)
@own = true
else
check_collection_privacy! @collection
@user = @collection.owner
@own = user_signed_in? ? @collection.owner?(current_user) : false
end
@reviews = Review.includes(:user).where(reviewable: @collection)
render partial: 'list' if params[:list] == 'true'
end
# POST /collections/1/like
def like
Collection.like @collection
end
# POST /collections
# POST /collections.json
def create
@collection = Collection.new(collection_params)
@collection.owner = params[:collection][:owner].blank? ? current_user : Institution.find(params[:collection][:owner])
respond_to do |format|
if @collection.save
format.html { redirect_to :back, notice: t('activerecord.attributes.collection.create.notice.successfully_created') }
else
format.html { render :new }
end
end
end
# PATCH/PUT /collections/1
# PATCH/PUT /collections/1.json
def update
respond_to do |format|
if Collection.update(collection_params)
format.html { redirect_to @collection, notice: t('activerecord.attributes.collection.update.notice.successfully_updated') }
else
format.html { render :edit }
end
end
end
# DELETE /collections/1
# DELETE /collections/1.json
def destroy
Collection.destroy @collection
respond_to do |format|
format.html { redirect_to user_path(current_user), notice: t('activerecord.attributes.collection.destroy.notice.successfully_destroy') }
end
end
def list
@collection = @collections.first
# list all
@collection = nil if @collection == 'all'
@collections = Collection.from_user(current_user)
@collections.select! { |c| c.id != @collection.id } unless @collection.blank?
unless params[:type].blank?
@type = params[:type]
@send = case @type
when 'add' then 'Adicionar'
when 'copy' then 'Copiar'
when 'move' then 'Mover'
else 'Enviar'
end
@title = (@send == 'Enviar') ? 'Coleções' : @send + ' objeto(s) às coleções'
end
render layout: false
end
def me
@new_collection = Collection.new
@publishers = current_user.institutions
@bookmark = (current_user.bookmarks.nil? || current_user.bookmarks.first.nil?) ? [] : [current_user.bookmarks.first]
@groups = [
CollectionsGroup.new(title: 'Coleções Automáticas',
collections: @bookmark ),
CollectionsGroup.new(title: 'Coleções Adicionadas',
collections: current_user.associated_collections)
]
end
# POST /collections/1/learning_object
def add_learning_object
@collections.each do |collection|
next unless collection.owner?(current_user)
@learning_objects.each do |learning_object|
collection.learning_objects << learning_object
end
collection.save
end
render json: { status: true } if request.xhr?
end
# DELETE /collections/1/learning_object
def remove_learning_object
@collections.each do |collection|
next unless collection.owner?(current_user)
@learning_objects.each do |learning_object|
collection.learning_objects.destroy(learning_object)
end
collection.save
end
render json: { status: true } if request.xhr?
end
# change collection privacy
def change_privacy
@collection.privacy = params[:privacy]
response = @collection.save
render json: { status: response } if request.xhr?
end
private
def check_collection_privacy!(collection)
if collection.private?
redirect_to :root, notice: 'Está é uma coleção privada.' unless collection.owner?(current_user)
end
end
def set_collection
@collection = Collection.find params[:id]
end
def set_collections
if params[:id] == "all" || params[:id].blank?
@collections = ['all']
else
@collections = (params[:id].class == String) ? [Collection.find(params[:id])] : params[:id].map{|id| Collection.find id}
end
unless params[:learning_objects_ids].blank?
@learning_objects = []
params[:learning_objects_ids].split(',').each do |id|
object = LearningObject.find id
@learning_objects << object unless object.blank?
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def collection_params
params.require(:collection).permit(:name, :description, learning_objects: [])
end
def user_not_authorized
flash[:notice] = "Esta coleção é privada!"
redirect_to (root_path)
end
def authorize_action
@collection ||= Collection.new
authorize @collection
end
end