Skip to content
Snippets Groups Projects
Commit 0a77c446 authored by Marcela Ribeiro de Oliveira's avatar Marcela Ribeiro de Oliveira
Browse files

Merge branch 'issue/275' into 'master'

SCRUM#275: fixing on_delete for foreign key on attachments

See merge request portalmec/portalmec!474
parents 975fb34b 4b57f148
No related branches found
No related tags found
No related merge requests found
......@@ -27,7 +27,7 @@ class V1::LearningObjects::AttachmentController < ApplicationController
# GET /learning_objects/:learning_object_id/attachment
def index
@attachment = paginate LearningObject::Attachment.includes(:attachment)
@attachment = paginate @attachment
render json: @attachment
end
......@@ -68,13 +68,18 @@ class V1::LearningObjects::AttachmentController < ApplicationController
end
def update_params
params.permit(:attachment_id, :infohash)
params.permit(:learning_object_attachment_id, :learning_object_attachment_id_son, :infohash)
end
def set_objects
@learning_object = LearningObject.find(attachment_params[:learning_object_id])
if current_user.try(:is_admin?)
@learning_object = LearningObject.unscoped.find(attachment_params[:learning_object_id])
else
@learning_object = LearningObject.find(attachment_params[:learning_object_id])
end
if ( attachment_params[:id].blank? )
@attachment = @learning_object.attachment
@attachment = @learning_object.attachments
else
@attachment = LearningObject::Attachment.find(attachment_params[:id])
end
......
......@@ -45,10 +45,9 @@ 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
has_one :learning_object_attachment, as: :learning_object_attachment_id_son
belongs_to :learning_object_attachment
def retrieve_url
"#{DspaceService.link}/rest#{retrieve_link}"
......
......@@ -34,5 +34,6 @@ class LearningObject::AttachmentSerializer < ActiveModel::Serializer
:id_dspace,
:thumbnail,
:cache_link,
:attachment_id
:learning_object_attachment_id,
:learning_object_attachment_id_son
end
class UpdateLearningObjectAttachmentsForeignKey < ActiveRecord::Migration[5.0]
def change
remove_foreign_key :learning_object_attachments, :learning_object_attachments
add_foreign_key :learning_object_attachments, :learning_object_attachments, column: :attachment_id, on_delete: :cascade
end
end
class RemoveLearningObjectAttachmentsForeignKeyOnCascade < ActiveRecord::Migration[5.0]
def change
remove_foreign_key :learning_object_attachments, :learning_object_attachments
end
def change
remove_foreign_key :learning_object_attachments, :learning_object_attachments
end
end
class RemoveAttachmentIdForLearningObjectAttachments < ActiveRecord::Migration[5.0]
def change
remove_index :learning_object_attachments, :attachment_id
remove_column :learning_object_attachments, :attachment_id
end
end
class AddForeignKeyToLearningObjectAttachments < ActiveRecord::Migration[5.0]
def change
add_column :learning_object_attachments, :learning_object_attachment_id, :integer
add_column :learning_object_attachments, :learning_object_attachment_id_son, :integer
add_foreign_key :learning_object_attachments, :learning_object_attachments, on_delete: :cascade
add_foreign_key :learning_object_attachments, :learning_object_attachments, column: :learning_object_attachment_id_son, on_delete: :nullify
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