affected_by_destroy 1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in affected_by_destroy.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Lucas Clemente
2
+
3
+ MIT License
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.
@@ -0,0 +1,29 @@
1
+ # AffectedByDestroy
2
+
3
+ Based on https://github.com/murbanski/affected_on_destroy.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'affected_by_destroy'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install affected_by_destroy
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'affected_by_destroy/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "affected_by_destroy"
8
+ gem.version = AffectedByDestroy::VERSION
9
+ gem.authors = ["Lucas Clemente"]
10
+ gem.email = ["luke.clemente@gmail.com"]
11
+ gem.description = %q{Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used}
12
+ gem.summary = %q{Rails plugin showing which related records will be deleted from DB when :dependent => :destroy is used}
13
+ gem.homepage = "https://github.com/lucas-clemente/affected_by_destroy"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+ end
@@ -0,0 +1,21 @@
1
+ require "affected_by_destroy/version"
2
+
3
+ class ActiveRecord::Base
4
+ def affected_by_destroy(affected = [])
5
+ affected << self
6
+ self.class.reflect_on_all_associations.select {|a| a.options[:dependent] == :destroy}.each do |assoc|
7
+ assoc_result = self.send(assoc.name)
8
+ next if assoc_result.blank?
9
+ if assoc.macro == :has_many
10
+ assoc_result.each do |assoc_object|
11
+ affected += assoc_object.affected_by_destroy(affected) unless affected.include?(assoc_object)
12
+ end
13
+ elsif assoc.macro == :belongs_to
14
+ affected += assoc_result.affected_by_destroy(affected) unless affected.include?(assoc_result)
15
+ else
16
+ raise 'Association Type is not supported'
17
+ end
18
+ end
19
+ affected.uniq
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module AffectedByDestroy
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1 @@
1
+ require 'affected_by_destroy'
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ describe AffectedByDestroy do
4
+ it 'shows has_many objects' do
5
+ u = User.create
6
+ b = Blog.create user: u
7
+ u.affected_by_destroy.should == [u, b]
8
+ end
9
+
10
+ it 'recursively shows has_many objects' do
11
+ u = User.create
12
+ b = Blog.create user: u
13
+ p = Post.create blog: b
14
+ u.affected_by_destroy.should == [u, b, p]
15
+ end
16
+
17
+ it 'shows belongs_to objects' do
18
+ u = User.create
19
+ b = Blog.create user: u
20
+ p = Post.create blog: b
21
+ u.affected_by_destroy.should == [u, b, p]
22
+ end
23
+
24
+ it 'works with self-joining classes' do
25
+ p = Post.create
26
+ p1 = Post.create parent: p
27
+ p2 = Post.create parent: p
28
+ p.affected_by_destroy.should == [p, p1, p2]
29
+ end
30
+ end
@@ -0,0 +1,46 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'rspec'
4
+ require 'active_record'
5
+ require 'affected_by_destroy'
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
8
+
9
+ ActiveRecord::Schema.define(:version => 1) do
10
+ create_table "users", :force => true do |t|
11
+ t.string "login"
12
+ t.string "password"
13
+ t.datetime "created_at"
14
+ t.datetime "updated_at"
15
+ end
16
+
17
+ create_table "blogs", :force => true do |t|
18
+ t.integer "user_id"
19
+ t.string "name"
20
+ t.datetime "created_at"
21
+ t.datetime "updated_at"
22
+ end
23
+
24
+ create_table "posts", :force => true do |t|
25
+ t.integer "blog_id"
26
+ t.text "content"
27
+ t.integer "parent_id"
28
+ t.datetime "created_at"
29
+ t.datetime "updated_at"
30
+ end
31
+ end
32
+
33
+ class User < ActiveRecord::Base
34
+ has_many :blogs, :dependent => :destroy
35
+ end
36
+
37
+ class Blog < ActiveRecord::Base
38
+ belongs_to :user, dependent: :destroy
39
+ has_many :posts, :dependent => :destroy
40
+ end
41
+
42
+ class Post < ActiveRecord::Base
43
+ belongs_to :blog
44
+ belongs_to :parent, class_name: "Post"
45
+ has_many :children, class_name: "Post", foreign_key: "parent_id", dependent: :destroy
46
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: affected_by_destroy
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lucas Clemente
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-11 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Rails plugin showing which related records will be deleted from DB when
15
+ :dependent => :destroy is used
16
+ email:
17
+ - luke.clemente@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - affected_by_destroy.gemspec
28
+ - lib/affected_by_destroy.rb
29
+ - lib/affected_by_destroy/version.rb
30
+ - rails/init.rb
31
+ - test/test.rb
32
+ - test/test_helper.rb
33
+ homepage: https://github.com/lucas-clemente/affected_by_destroy
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Rails plugin showing which related records will be deleted from DB when :dependent
57
+ => :destroy is used
58
+ test_files:
59
+ - test/test.rb
60
+ - test/test_helper.rb