recurse-delete 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/recurse-delete.rb +51 -0
- metadata +56 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
# Recurse Delete by John Isaacks (programming-perils.com)
|
2
|
+
#
|
3
|
+
# Copyright (c) 2012 John Isaacks
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module RecurseDelete
|
25
|
+
|
26
|
+
def recurse_delete(model_class=nil, ids=nil)
|
27
|
+
ids = self.id if ids.nil?
|
28
|
+
model_class = self.class if model_class.nil?
|
29
|
+
|
30
|
+
# Delete self
|
31
|
+
model_class.delete_all(:id => ids)
|
32
|
+
|
33
|
+
# find and delete all dependent associations
|
34
|
+
delete_assocs = [:delete_all,:delete,:destroy,:destroy_all]
|
35
|
+
table_name = model_class.to_s.tableize
|
36
|
+
model_class.reflect_on_all_associations.select do |assoc|
|
37
|
+
delete_assocs.include? assoc.options[:dependent]
|
38
|
+
end.map(&:name).each do |sub|
|
39
|
+
sub_model_class = sub.to_s.classify.constantize
|
40
|
+
sub_ids = sub_model_class.send("find_all_by_#{table_name.classify.foreign_key}", ids).map(&:id)
|
41
|
+
recurse_delete(sub_model_class, sub_ids)
|
42
|
+
end
|
43
|
+
|
44
|
+
self
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
class ActiveRecord::Base
|
50
|
+
include RecurseDelete
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: recurse-delete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- JD Isaacks
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70171390191620 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70171390191620
|
25
|
+
description: Recursively delete all dependent model associations without an N + 1
|
26
|
+
email: john.isaacks@programming-perils.com
|
27
|
+
executables: []
|
28
|
+
extensions: []
|
29
|
+
extra_rdoc_files: []
|
30
|
+
files:
|
31
|
+
- lib/recurse-delete.rb
|
32
|
+
homepage: https://github.com/jisaacks/recurse-delete
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.15
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Delete records efficiently
|
56
|
+
test_files: []
|