From 9e43cc2e968f221447ccef60e46266fe445dd359 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 1 Oct 2015 11:02:39 +0800 Subject: [PATCH] dependent destroy models for topics/maps/synapses on mappings --- app/controllers/maps_controller.rb | 10 +-------- app/controllers/synapses_controller.rb | 10 +-------- app/controllers/topics_controller.rb | 28 +------------------------- app/models/map.rb | 4 ++-- app/models/synapse.rb | 2 +- app/models/topic.rb | 6 +++--- 6 files changed, 9 insertions(+), 51 deletions(-) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index c60b8831..21946f88 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -218,15 +218,7 @@ class MapsController < ApplicationController @map = Map.find(params[:id]).authorize_to_delete(@current) - if @map - @mappings = @map.mappings - - @mappings.each do |mapping| - mapping.delete - end - - @map.delete - end + @map.delete if @map respond_to do |format| format.json { diff --git a/app/controllers/synapses_controller.rb b/app/controllers/synapses_controller.rb index 3782d336..47d3321e 100644 --- a/app/controllers/synapses_controller.rb +++ b/app/controllers/synapses_controller.rb @@ -50,15 +50,7 @@ class SynapsesController < ApplicationController def destroy @current = current_user @synapse = Synapse.find(params[:id]).authorize_to_delete(@current) - - if @synapse - @synapse.mappings.each do |m| - m.map.touch(:updated_at) - m.delete - end - - @synapse.delete - end + @synapse.delete if @synapse respond_to do |format| format.json { head :no_content } diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 3cc029f9..d73be190 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -200,33 +200,7 @@ class TopicsController < ApplicationController def destroy @current = current_user @topic = Topic.find(params[:id]).authorize_to_delete(@current) - - if @topic - @synapses = @topic.synapses - @mappings = @topic.mappings - - @synapses.each do |synapse| - synapse.mappings.each do |m| - - @map = m.map - @map.touch(:updated_at) - - m.delete - end - - synapse.delete - end - - @mappings.each do |mapping| - - @map = mapping.map - @map.touch(:updated_at) - - mapping.delete - end - - @topic.delete - end + @topic.delete if @topic respond_to do |format| format.json { head :no_content } diff --git a/app/models/map.rb b/app/models/map.rb index ee949676..6262924e 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -2,8 +2,8 @@ class Map < ActiveRecord::Base belongs_to :user - has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping - has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping + has_many :topicmappings, -> { Mapping.topicmapping }, class_name: :Mapping, dependent: :destroy + has_many :synapsemappings, -> { Mapping.synapsemapping }, class_name: :Mapping, dependent: :destroy has_many :topics, through: :topicmappings has_many :synapses, through: :synapsemappings diff --git a/app/models/synapse.rb b/app/models/synapse.rb index bf3bdab2..10fba6e9 100644 --- a/app/models/synapse.rb +++ b/app/models/synapse.rb @@ -5,7 +5,7 @@ class Synapse < ActiveRecord::Base belongs_to :topic1, :class_name => "Topic", :foreign_key => "node1_id" belongs_to :topic2, :class_name => "Topic", :foreign_key => "node2_id" - has_many :mappings + has_many :mappings, dependent: :destroy has_many :maps, :through => :mappings def user_name diff --git a/app/models/topic.rb b/app/models/topic.rb index 078c633e..d28127fa 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -3,12 +3,12 @@ class Topic < ActiveRecord::Base belongs_to :user - has_many :synapses1, :class_name => 'Synapse', :foreign_key => 'node1_id' - has_many :synapses2, :class_name => 'Synapse', :foreign_key => 'node2_id' + has_many :synapses1, :class_name => 'Synapse', :foreign_key => 'node1_id', dependent: :destroy + has_many :synapses2, :class_name => 'Synapse', :foreign_key => 'node2_id', dependent: :destroy has_many :topics1, :through => :synapses2, :source => :topic1 has_many :topics2, :through => :synapses1, :source => :topic2 - has_many :mappings + has_many :mappings, dependent: :destroy has_many :maps, :through => :mappings # This method associates the attribute ":image" with a file attachment