dependent destroy models for topics/maps/synapses on mappings

This commit is contained in:
Devin Howard 2015-10-01 11:02:39 +08:00 committed by Connor Turland
parent 0a63b5e79a
commit e5c83a2a0c
6 changed files with 9 additions and 51 deletions

View file

@ -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 {

View file

@ -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 }

View file

@ -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 }

View file

@ -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

View file

@ -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

View file

@ -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