From 736a9296ae6f39a3402545a9e269fb1e9377a48b Mon Sep 17 00:00:00 2001
From: Clarissa <cdp13@inf.ufpr.br>
Date: Thu, 18 May 2017 11:20:22 -0300
Subject: [PATCH] #215: added deleted_at for collection_items; destroy all
 collection_items when collection is deleted

---
 app/controllers/v1/collections_controller.rb                | 5 +++++
 app/models/collection.rb                                    | 3 ++-
 app/models/collection_item.rb                               | 2 ++
 .../20170516111857_add_deleted_at_to_collection_items.rb    | 6 ++++++
 4 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 db/migrate/20170516111857_add_deleted_at_to_collection_items.rb

diff --git a/app/controllers/v1/collections_controller.rb b/app/controllers/v1/collections_controller.rb
index 21fc1f9c..2dd40185 100644
--- a/app/controllers/v1/collections_controller.rb
+++ b/app/controllers/v1/collections_controller.rb
@@ -56,6 +56,11 @@ class V1::CollectionsController < ApplicationController
   # DELETE /v1/collections/1
   # DELETE /v1/collections/1.json
   def destroy
+    items = @collection.collection_items.select(:id)
+    p items
+    if !items.blank?
+      @collection.delete_items(items)
+    end
     @collection.destroy
     render status: :ok
   end
diff --git a/app/models/collection.rb b/app/models/collection.rb
index 632f2179..cecb90f1 100644
--- a/app/models/collection.rb
+++ b/app/models/collection.rb
@@ -36,6 +36,8 @@ class Collection < ApplicationRecord
   include Highlights
   include Complainable
 
+  acts_as_paranoid
+
   has_many :collection_items, -> { order("position ASC") }, as: :collectionable, dependent: :destroy
   has_many :collections, through: :collection_items, source: :collectionable, source_type: 'Collection'
   has_many :learning_objects, through: :collection_items, source: :collectionable, source_type: 'LearningObject'
@@ -50,7 +52,6 @@ class Collection < ApplicationRecord
 
   searchkick language: 'brazilian', match: :word_start, searchable: [:name, :description, :author], callbacks: :async
 
-  acts_as_paranoid
   has_paper_trail
 
   def search_data
diff --git a/app/models/collection_item.rb b/app/models/collection_item.rb
index 1a73aaed..101e61d5 100644
--- a/app/models/collection_item.rb
+++ b/app/models/collection_item.rb
@@ -21,6 +21,8 @@ class CollectionItem < ApplicationRecord
   validates :collection, :collectionable, presence: true
   acts_as_list scope: :collection
 
+  acts_as_paranoid
+
   def recipient
     collection
   end
diff --git a/db/migrate/20170516111857_add_deleted_at_to_collection_items.rb b/db/migrate/20170516111857_add_deleted_at_to_collection_items.rb
new file mode 100644
index 00000000..abbe878f
--- /dev/null
+++ b/db/migrate/20170516111857_add_deleted_at_to_collection_items.rb
@@ -0,0 +1,6 @@
+class AddDeletedAtToCollectionItems < ActiveRecord::Migration[5.0]
+  def change
+    add_column :collection_items, :deleted_at, :datetime
+    add_index :collection_items, :deleted_at
+  end
+end
\ No newline at end of file
-- 
GitLab