Skip to content
Snippets Groups Projects
Commit 83c9fa45 authored by Israel Barreto Sant'Anna's avatar Israel Barreto Sant'Anna
Browse files

Merge branch 'issue/275' into 'master'

Issue/275

See merge request !453
parents 01a13858 070cc735
No related branches found
No related tags found
No related merge requests found
......@@ -18,11 +18,19 @@
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
class V1::LearningObjects::AttachmentController < ApplicationController
include ::Paginator
before_action :set_objects
before_action :authenticate_user!, only: :update
before_action :authenticate_user!, only: [:update, :index]
before_action :authorize!, only: :destroy
before_action :authorize_update_attachment!, only: :update
# GET /learning_objects/:learning_object_id/attachment
def index
@attachment = paginate LearningObject::Attachment.includes(:attachment)
render json: @attachment
end
# DELETE /learning_objects/:learning_object_id/attachments/:id
def destroy
return render status: :not_found if @learning_object.nil? || @attachment.nil?
......@@ -32,12 +40,12 @@ class V1::LearningObjects::AttachmentController < ApplicationController
render status: :ok
end
# PUT /learning_objects/:learning_object_id/attachments/:id
def update
return render status: :not_found if @learning_object.nil? || @attachment.nil?
if @attachment.update(infohash: infohash_params[:infohash])
render status: :ok
if @attachment.update(update_params)
render json: @attachment, status: :ok
else
render status: :unprocessable_entity
end
......@@ -49,13 +57,17 @@ class V1::LearningObjects::AttachmentController < ApplicationController
params.permit(:learning_object_id, :id)
end
def infohash_params
params.permit(:infohash)
def update_params
params.permit(:attachment_id, :infohash)
end
def set_objects
@learning_object = LearningObject.find(attachment_params[:learning_object_id])
@attachment = LearningObject::Attachment.find(attachment_params[:id])
if ( attachment_params[:id].blank? )
@attachment = @learning_object.attachment
else
@attachment = LearningObject::Attachment.find(attachment_params[:id])
end
end
def authorize!
......@@ -68,6 +80,7 @@ class V1::LearningObjects::AttachmentController < ApplicationController
return render status: :unauthorized unless @learning_object.attachments.include? @attachment
authorize(@learning_object, :add_infohash?)
authorize(@learning_object, :add_attachment_id?)
end
end
......@@ -45,6 +45,10 @@ class LearningObject::Attachment < ApplicationRecord
include ::Thumbnailable
belongs_to :learning_object
scope :unknown_mime_type, ->() { where(format: 'Unknown') }
has_one :attachment, class_name: 'LearningObject::Attachment'
belongs_to :attachment, class_name: 'LearningObject::Attachment'
# has_and_belongs_to_many
# belongs_to
def retrieve_url
"#{DspaceService.link}/rest#{retrieve_link}"
......
......@@ -65,6 +65,10 @@ class LearningObjectPolicy < ApplicationPolicy
record if user.is_admin?
end
def add_attachment_id?
record if user.is_admin?
end
def show?
return record if record.published? || ( !user.nil? && user_can_edit? )
return record if user == record.publisher
......
......@@ -19,5 +19,20 @@
class LearningObject::AttachmentSerializer < ActiveModel::Serializer
cache key: 'attachment', expires_in: 24.hours
attributes :id, :name, :retrieve_link, :description, :format, :mime_type, :size, :bundle_name, :learning_object_id, :created_at, :updated_at, :id_dspace, :thumbnail, :cache_link
attributes \
:id,
:name,
:retrieve_link,
:description,
:format,
:mime_type,
:size,
:bundle_name,
:learning_object_id,
:created_at,
:updated_at,
:id_dspace,
:thumbnail,
:cache_link,
:attachment_id
end
......@@ -168,7 +168,7 @@ Rails.application.routes.draw do
collection do
get 'validate'
end
resources :attachment, module: 'learning_objects', only: [:destroy, :update], on: :member
resources :attachment, module: 'learning_objects', only: [:destroy, :update, :index], on: :member
end
resources :institutions, concerns: :deletable do
......
# 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 AddSubmitterRequestToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :submitter_request, :integer, default: 0
......
# 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 AddInfoHashToLearningObjectAttachments < ActiveRecord::Migration[5.0]
def change
add_column :learning_object_attachments, :infohash, :string
......
# 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 AddAttachmentForLearningObjectAttachments < ActiveRecord::Migration[5.0]
def change
add_column :learning_object_attachments, :attachment_id, :integer
add_foreign_key :learning_object_attachments, :learning_object_attachments, column: :attachment_id
add_index :learning_object_attachments, :attachment_id, unique: true
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment