murbanski-affected_on_destroy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.md +91 -0
- data/Rakefile +57 -0
- data/VERSION +1 -0
- data/affected_on_destroy.gemspec +48 -0
- data/lib/affected_on_destroy.rb +57 -0
- data/rails/init.rb +1 -0
- data/test/affected_on_destroy_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +64 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Marcin Urbanski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# affected_on_destroy
|
2
|
+
|
3
|
+
affected_on_destroy is a simple Rails plugin that recursively reflects on defined associations and lets you know which records will be deleted/nullified when using <b>:dependent => :destroy</b> or <b>:dependent => :nullify</b>.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
config.gem 'murbanski-affected_on_destroy', :lib => 'affected_on_destroy', :source => 'http://gems.github.com'
|
8
|
+
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
schema.rb:
|
13
|
+
|
14
|
+
create_table "users", :force => true do |t|
|
15
|
+
t.string "login"
|
16
|
+
t.string "password"
|
17
|
+
t.datetime "created_at"
|
18
|
+
t.datetime "updated_at"
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table "blogs", :force => true do |t|
|
22
|
+
t.integer "user_id"
|
23
|
+
t.string "name"
|
24
|
+
t.datetime "created_at"
|
25
|
+
t.datetime "updated_at"
|
26
|
+
end
|
27
|
+
|
28
|
+
create_table "posts", :force => true do |t|
|
29
|
+
t.integer "blog_id"
|
30
|
+
t.text "content"
|
31
|
+
t.datetime "created_at"
|
32
|
+
t.datetime "updated_at"
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
models:
|
39
|
+
class User < ActiveRecord::Base
|
40
|
+
has_many :blogs, :dependent => :destroy
|
41
|
+
end
|
42
|
+
|
43
|
+
class Blog < ActiveRecord::Base
|
44
|
+
belongs_to :user
|
45
|
+
has_many :posts, :dependent => :nullify
|
46
|
+
end
|
47
|
+
|
48
|
+
class Post < ActiveRecord::Base
|
49
|
+
belongs_to :blog
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
Sample script/console session:
|
54
|
+
>> marcin = User.create(:login => 'marcin', :password => 'my_pass')
|
55
|
+
=> #<User id: 1, login: "marcin", password: "my_pass", created_at: "2009-06-26 01:47:36", updated_at: "2009-06-26 01:47:36">
|
56
|
+
|
57
|
+
>> blog = marcin.blogs.create(:name => 'My Ruby Blog')
|
58
|
+
=> #<Blog id: 1, user_id: 1, name: "My Ruby Blog", created_at: "2009-06-26 01:47:57", updated_at: "2009-06-26 01:47:57">
|
59
|
+
|
60
|
+
>> blog.posts.create(:content => 'Ruby tricks #1')
|
61
|
+
=> #<Post id: 1, blog_id: 1, content: "Ruby tricks #1", created_at: "2009-06-26 01:48:40", updated_at: "2009-06-26 01:48:40">
|
62
|
+
>> blog.posts.create(:content => 'Ruby tricks #2')
|
63
|
+
=> #<Post id: 2, blog_id: 1, content: "Ruby tricks #2", created_at: "2009-06-26 01:48:45", updated_at: "2009-06-26 01:48:45">
|
64
|
+
|
65
|
+
What records will be affected when we destroy user 'marcin'?
|
66
|
+
>> marcin.affected_on_destroy
|
67
|
+
=> [#<Blog id: 1, user_id: 1, name: "My Ruby Blog", created_at: "2009-06-26 01:47:57", updated_at: "2009-06-26 01:47:57">,
|
68
|
+
#<Post id: 1, blog_id: 1, content: "Ruby tricks #1", created_at: "2009-06-26 01:48:40", updated_at: "2009-06-26 01:48:40">,
|
69
|
+
#<Post id: 2, blog_id: 1, content: "Ruby tricks #2", created_at: "2009-06-26 01:48:45", updated_at: "2009-06-26 01:48:45">]
|
70
|
+
|
71
|
+
A bit cleaner summary - only with model name and id:
|
72
|
+
>> puts marcin.affected_on_destroy_info
|
73
|
+
Blog 1
|
74
|
+
Post 1
|
75
|
+
Post 2
|
76
|
+
=> nil
|
77
|
+
|
78
|
+
Which models are affected when destroying user?
|
79
|
+
>> User.affected_on_destroy
|
80
|
+
=> ["Blog", "Post"]
|
81
|
+
|
82
|
+
Same for blog:
|
83
|
+
>> Blog.affected_on_destroy
|
84
|
+
=> ["Post"]
|
85
|
+
|
86
|
+
|
87
|
+
## Rails Version
|
88
|
+
|
89
|
+
Works on Rails 2.3 and 2.2
|
90
|
+
|
91
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "affected_on_destroy"
|
8
|
+
gem.summary = %Q{Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used}
|
9
|
+
gem.description = %Q{Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used}
|
10
|
+
gem.email = "marcin@urbanski.vdl.pl"
|
11
|
+
gem.homepage = "http://github.com/murbanski/affected_on_destroy"
|
12
|
+
gem.authors = ["Marcin Urbanski"]
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/testtask'
|
21
|
+
Rake::TestTask.new(:test) do |test|
|
22
|
+
test.libs << 'lib' << 'test'
|
23
|
+
test.pattern = 'test/**/*_test.rb'
|
24
|
+
test.verbose = true
|
25
|
+
end
|
26
|
+
|
27
|
+
begin
|
28
|
+
require 'rcov/rcovtask'
|
29
|
+
Rcov::RcovTask.new do |test|
|
30
|
+
test.libs << 'test'
|
31
|
+
test.pattern = 'test/**/*_test.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
task :rcov do
|
36
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?('VERSION.yml')
|
46
|
+
config = YAML.load(File.read('VERSION.yml'))
|
47
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
48
|
+
else
|
49
|
+
version = ""
|
50
|
+
end
|
51
|
+
|
52
|
+
rdoc.rdoc_dir = 'rdoc'
|
53
|
+
rdoc.title = "affected_on_destroy #{version}"
|
54
|
+
rdoc.rdoc_files.include('README*')
|
55
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
56
|
+
end
|
57
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{affected_on_destroy}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Marcin Urbanski"]
|
9
|
+
s.date = %q{2009-06-26}
|
10
|
+
s.description = %q{Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used}
|
11
|
+
s.email = %q{marcin@urbanski.vdl.pl}
|
12
|
+
s.extra_rdoc_files = [
|
13
|
+
"LICENSE",
|
14
|
+
"README.md"
|
15
|
+
]
|
16
|
+
s.files = [
|
17
|
+
".gitignore",
|
18
|
+
"LICENSE",
|
19
|
+
"README.md",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"affected_on_destroy.gemspec",
|
23
|
+
"lib/affected_on_destroy.rb",
|
24
|
+
"rails/init.rb",
|
25
|
+
"test/affected_on_destroy_test.rb",
|
26
|
+
"test/test_helper.rb"
|
27
|
+
]
|
28
|
+
s.has_rdoc = true
|
29
|
+
s.homepage = %q{http://github.com/murbanski/affected_on_destroy}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.1}
|
33
|
+
s.summary = %q{Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used}
|
34
|
+
s.test_files = [
|
35
|
+
"test/affected_on_destroy_test.rb",
|
36
|
+
"test/test_helper.rb"
|
37
|
+
]
|
38
|
+
|
39
|
+
if s.respond_to? :specification_version then
|
40
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
41
|
+
s.specification_version = 2
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
44
|
+
else
|
45
|
+
end
|
46
|
+
else
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module AffectedOnDestroy
|
4
|
+
|
5
|
+
def self.included(klass)
|
6
|
+
klass.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def dependent_associations
|
12
|
+
associations = self.reflect_on_all_associations(:has_many) + self.reflect_on_all_associations(:has_one)
|
13
|
+
associations.select {|a| a.options.has_key?(:dependent)}
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# TODO: infinite recursion: create array already_inspected_associations
|
18
|
+
# User.affected_on_destroy # => ["Post", "Comments", "Ratings"]
|
19
|
+
def affected_on_destroy
|
20
|
+
returning affected = [] do
|
21
|
+
dependent_associations.map do |assoc|
|
22
|
+
affected << assoc.class_name
|
23
|
+
affected << assoc.class_name.constantize.affected_on_destroy
|
24
|
+
end
|
25
|
+
end.flatten.uniq.sort
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# u = User.first
|
31
|
+
# u.affected_on_destroy # => [ #<Post id: 25>, #<Comment id: 10, post_id: 25>, #<Rating id: 3, post_id: 25>]
|
32
|
+
def affected_on_destroy
|
33
|
+
affected_objects = []
|
34
|
+
self.class.dependent_associations.each do |assoc|
|
35
|
+
next if (associated_relation = self.send(assoc.name)).blank?
|
36
|
+
if assoc.macro == :has_one
|
37
|
+
affected_objects << associated_relation
|
38
|
+
affected_objects << associated_relation.affected_on_destroy
|
39
|
+
elsif assoc.macro == :has_many
|
40
|
+
associated_relation.each do |associated_object|
|
41
|
+
affected_objects << associated_object
|
42
|
+
affected_objects << associated_object.affected_on_destroy
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
affected_objects.flatten.uniq.sort_by {|elem| elem.class.to_s }
|
48
|
+
end
|
49
|
+
|
50
|
+
def affected_on_destroy_info
|
51
|
+
self.affected_on_destroy.map{|obj| "#{obj.class.to_s} #{obj.id}"}
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
ActiveRecord::Base.send :include, AffectedOnDestroy
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'affected_on_destroy'
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: murbanski-affected_on_destroy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Urbanski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-26 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used
|
17
|
+
email: marcin@urbanski.vdl.pl
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- LICENSE
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- VERSION
|
31
|
+
- affected_on_destroy.gemspec
|
32
|
+
- lib/affected_on_destroy.rb
|
33
|
+
- rails/init.rb
|
34
|
+
- test/affected_on_destroy_test.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/murbanski/affected_on_destroy
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --charset=UTF-8
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.2.0
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used
|
62
|
+
test_files:
|
63
|
+
- test/affected_on_destroy_test.rb
|
64
|
+
- test/test_helper.rb
|