dependent_restrict 0.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.
- checksums.yaml +15 -0
- data/Gemfile +12 -0
- data/LICENSE +21 -0
- data/README.md +28 -0
- data/Rakefile +150 -0
- data/dependent_restrict.gemspec +80 -0
- data/gemfiles/rails2.gemfile +12 -0
- data/gemfiles/rails3.gemfile +12 -0
- data/gemfiles/rails4.gemfile +13 -0
- data/lib/dependent_restrict.rb +87 -0
- data/lib/dependent_restrict/delete_restriction_error.rb +30 -0
- data/spec/dependent_protect_spec.rb +139 -0
- data/spec/schema.rb +15 -0
- data/spec/spec_helper.rb +28 -0
- metadata +79 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDhiYjQwNjJiNGM4OTFkNmU2MWFiMzM2OTE2N2ZmZmNkZGY5NWQ4ZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MDA1NmZhODg4NzUxOThjNjg5MjlkZDIzYjlkNTczM2RiODI4NmFjOA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmUyZDMzNDc0YTcwYjUyYzY0NWI5ZDAyODRlNGY2MDZiNDdlNDM1M2Y2Njhk
|
10
|
+
YTMxYTM2MjMxYTQ2NjMyMDI1MjQzMjA3OGM1MDBlMGFjNzk4ODM4NzUwMzFh
|
11
|
+
N2Y4ZGVkMDNkODdmNzAwOTZmY2Y0ZjFhYzdhMDIyYjI0NGJkY2M=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ODJmMDViYmFkMWM1NzkwZDIzYzFhMWY4ZDUwZmNhMDdlN2ExMzc0ZDQ5YzNj
|
14
|
+
NDQ2OTEyNTFiMWVmOWNlN2M5NzFhYjFlMTA4MWM1NDA4MTg1NzVmZjE0MmU1
|
15
|
+
M2M4NGJmZDRmZjE3ZDFkMjQ5Y2M4M2MzYmFjOWNjM2IyMTFiMmI=
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) Tom Preston-Werner
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
Dependent Restrict
|
2
|
+
==================
|
3
|
+
|
4
|
+
A gem for rails 2, 3 and 4 that retrofits and improves rails 4 functionality
|
5
|
+
|
6
|
+
Rails 4 offers 2 very useful dependent restrictions:
|
7
|
+
```ruby
|
8
|
+
dependent: :raise_with_exception
|
9
|
+
dependent: :raise_with_error
|
10
|
+
```
|
11
|
+
|
12
|
+
In rails 3 it was just:
|
13
|
+
```ruby
|
14
|
+
dependent: :raise # same sa raise_with_exception
|
15
|
+
```
|
16
|
+
|
17
|
+
In rails 2 these didn't exist, it was just
|
18
|
+
```ruby
|
19
|
+
dependent: destroy
|
20
|
+
```
|
21
|
+
|
22
|
+
Which is available in rails 2, 3 and 4
|
23
|
+
|
24
|
+
## Differences from standard rails 4
|
25
|
+
|
26
|
+
* Error includes detailed message which shows up to 5 of the records that are dependent
|
27
|
+
This is useful for users to know which dependencies must be removed before they can
|
28
|
+
remove the parent record
|
data/Rakefile
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
desc 'Default: run specs.'
|
47
|
+
task :default => :spec
|
48
|
+
|
49
|
+
require 'rspec/core/rake_task'
|
50
|
+
|
51
|
+
desc "Run specs"
|
52
|
+
RSpec::Core::RakeTask.new do |t|
|
53
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
54
|
+
# Put spec opts in a file named .rspec in root
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "Generate SimpleCov test coverage and open in your browser"
|
58
|
+
task :coverage do
|
59
|
+
ENV['COVERAGE'] = 'true'
|
60
|
+
Rake::Task['spec'].invoke
|
61
|
+
end
|
62
|
+
|
63
|
+
require 'rdoc/task'
|
64
|
+
RDoc::Task.new do |rdoc|
|
65
|
+
rdoc.rdoc_dir = 'rdoc'
|
66
|
+
rdoc.title = "#{name} #{version}"
|
67
|
+
rdoc.rdoc_files.include('README*')
|
68
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
69
|
+
end
|
70
|
+
|
71
|
+
desc "Open an irb session preloaded with this library"
|
72
|
+
task :console do
|
73
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
74
|
+
end
|
75
|
+
|
76
|
+
#############################################################################
|
77
|
+
#
|
78
|
+
# Custom tasks (add your own tasks here)
|
79
|
+
#
|
80
|
+
#############################################################################
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
#############################################################################
|
85
|
+
#
|
86
|
+
# Packaging tasks
|
87
|
+
#
|
88
|
+
#############################################################################
|
89
|
+
|
90
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
91
|
+
task :release => :build do
|
92
|
+
unless `git branch` =~ /^\* master$/
|
93
|
+
puts "You must be on the master branch to release!"
|
94
|
+
exit!
|
95
|
+
end
|
96
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
97
|
+
sh "git tag v#{version}"
|
98
|
+
sh "git push origin master"
|
99
|
+
sh "git push origin v#{version}"
|
100
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
101
|
+
end
|
102
|
+
|
103
|
+
desc "Build #{gem_file} into the pkg directory"
|
104
|
+
task :build => :gemspec do
|
105
|
+
sh "mkdir -p pkg"
|
106
|
+
sh "gem build #{gemspec_file}"
|
107
|
+
sh "mv #{gem_file} pkg"
|
108
|
+
end
|
109
|
+
|
110
|
+
desc "Generate #{gemspec_file}"
|
111
|
+
task :gemspec => :validate do
|
112
|
+
# read spec file and split out manifest section
|
113
|
+
spec = File.read(gemspec_file)
|
114
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
115
|
+
|
116
|
+
# replace name version and date
|
117
|
+
replace_header(head, :name)
|
118
|
+
replace_header(head, :version)
|
119
|
+
replace_header(head, :date)
|
120
|
+
#comment this out if your rubyforge_project has a different name
|
121
|
+
replace_header(head, :rubyforge_project)
|
122
|
+
|
123
|
+
# determine file list from git ls-files
|
124
|
+
files = `git ls-files`.
|
125
|
+
split("\n").
|
126
|
+
sort.
|
127
|
+
reject { |file| file =~ /^\./ }.
|
128
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
129
|
+
map { |file| " #{file}" }.
|
130
|
+
join("\n")
|
131
|
+
|
132
|
+
# piece file back together and write
|
133
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
134
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
135
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
136
|
+
puts "Updated #{gemspec_file}"
|
137
|
+
end
|
138
|
+
|
139
|
+
desc "Validate #{gemspec_file}"
|
140
|
+
task :validate do
|
141
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
142
|
+
unless libfiles.empty?
|
143
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
144
|
+
exit!
|
145
|
+
end
|
146
|
+
unless Dir['VERSION*'].empty?
|
147
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
148
|
+
exit!
|
149
|
+
end
|
150
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'dependent_restrict'
|
16
|
+
s.version = '0.1.0'
|
17
|
+
s.date = '2013-09-14'
|
18
|
+
s.rubyforge_project = 'dependent_restrict'
|
19
|
+
|
20
|
+
## Make sure your summary is short. The description may be as long
|
21
|
+
## as you like.
|
22
|
+
s.summary = "Add dependent restrict and improves functionality to ActiveRecord 2/3/4.x."
|
23
|
+
s.description = "This gem is not needed in Rails 3 as dependent => :raise is included in 3.0."
|
24
|
+
|
25
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
26
|
+
## better to set the email to an email list or something. If you don't have
|
27
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
28
|
+
s.authors = ["Michael Noack"]
|
29
|
+
s.email = 'development@travellink.com.au'
|
30
|
+
s.homepage = 'http://github.com/sealink/dependent_restrict'
|
31
|
+
|
32
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
33
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
34
|
+
s.require_paths = %w[lib]
|
35
|
+
|
36
|
+
## This sections is only necessary if you have C extensions.
|
37
|
+
# s.require_paths << 'ext'
|
38
|
+
# s.extensions = %w[ext/extconf.rb]
|
39
|
+
|
40
|
+
## If your gem includes any executables, list them here.
|
41
|
+
# s.executables = ["name"]
|
42
|
+
|
43
|
+
## Specify any RDoc options here. You'll want to add your README and
|
44
|
+
## LICENSE files to the extra_rdoc_files list.
|
45
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
46
|
+
s.extra_rdoc_files = %w[README.md LICENSE]
|
47
|
+
|
48
|
+
## List your runtime dependencies here. Runtime dependencies are those
|
49
|
+
## that are needed for an end user to actually USE your code.
|
50
|
+
s.add_dependency('activerecord', [">= 2.3.0", "< 5.0.0"])
|
51
|
+
|
52
|
+
## List your development dependencies here. Development dependencies are
|
53
|
+
## those that are only needed during development
|
54
|
+
# s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
|
55
|
+
|
56
|
+
## Leave this section as-is. It will be automatically generated from the
|
57
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
58
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
59
|
+
# = MANIFEST =
|
60
|
+
s.files = %w[
|
61
|
+
Gemfile
|
62
|
+
LICENSE
|
63
|
+
README.md
|
64
|
+
Rakefile
|
65
|
+
dependent_restrict.gemspec
|
66
|
+
gemfiles/rails2.gemfile
|
67
|
+
gemfiles/rails3.gemfile
|
68
|
+
gemfiles/rails4.gemfile
|
69
|
+
lib/dependent_restrict.rb
|
70
|
+
lib/dependent_restrict/delete_restriction_error.rb
|
71
|
+
spec/dependent_protect_spec.rb
|
72
|
+
spec/schema.rb
|
73
|
+
spec/spec_helper.rb
|
74
|
+
]
|
75
|
+
# = MANIFEST =
|
76
|
+
|
77
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
78
|
+
## matches what you actually use.
|
79
|
+
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
80
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'dependent_restrict/delete_restriction_error'
|
3
|
+
|
4
|
+
module DependentRestrict
|
5
|
+
VERSION = '0.1.0'
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
super
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
|
11
|
+
base.class_eval do
|
12
|
+
class << self
|
13
|
+
alias_method_chain :has_one, :restrict
|
14
|
+
alias_method_chain :has_many, :restrict
|
15
|
+
alias_method_chain :has_and_belongs_to_many, :restrict
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ClassMethods
|
21
|
+
# We should be aliasing configure_dependency_for_has_many but that method
|
22
|
+
# is private so we can't. We alias has_many instead trying to be as fair
|
23
|
+
# as we can to the original behaviour.
|
24
|
+
def has_one_with_restrict(*args) #:nodoc:
|
25
|
+
reflection = if active_record_4?
|
26
|
+
association_id, options, scope, extension = *args
|
27
|
+
create_reflection(:has_one, association_id, options, scope ||= {}, self)
|
28
|
+
else
|
29
|
+
association_id, options, extension = *args
|
30
|
+
create_reflection(:has_one, association_id, options, self)
|
31
|
+
end
|
32
|
+
add_dependency_callback!(reflection, options)
|
33
|
+
has_one_without_restrict(*args) #association_id, options, &extension)
|
34
|
+
end
|
35
|
+
|
36
|
+
def has_many_with_restrict(association_id, options = {}, &extension) #:nodoc:
|
37
|
+
reflection = if active_record_4?
|
38
|
+
create_reflection(:has_many, association_id, options, scope ||= {}, self)
|
39
|
+
else
|
40
|
+
create_reflection(:has_many, association_id, options, self)
|
41
|
+
end
|
42
|
+
add_dependency_callback!(reflection, options)
|
43
|
+
has_many_without_restrict(association_id, options, &extension)
|
44
|
+
end
|
45
|
+
|
46
|
+
def has_and_belongs_to_many_with_restrict(association_id, options = {}, &extension)
|
47
|
+
reflection = create_reflection(:has_and_belongs_to_many, association_id, options, self)
|
48
|
+
add_dependency_callback!(reflection, options)
|
49
|
+
options.delete(:dependent)
|
50
|
+
has_and_belongs_to_many_without_restrict(association_id, options, &extension)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def add_dependency_callback!(reflection, options)
|
56
|
+
dependent_type = active_record_4? ? options[:dependent] : reflection.options[:dependent]
|
57
|
+
method_name = "dependent_#{dependent_type}_for_#{reflection.name}"
|
58
|
+
case dependent_type
|
59
|
+
when :rollback, :restrict_with_error
|
60
|
+
options.delete(:dependent)
|
61
|
+
define_method(method_name) do
|
62
|
+
method = reflection.collection? ? :empty? : :nil?
|
63
|
+
unless send(reflection.name).send(method)
|
64
|
+
raise ActiveRecord::Rollback
|
65
|
+
end
|
66
|
+
end
|
67
|
+
before_destroy method_name
|
68
|
+
when :restrict, :restrict_with_exception
|
69
|
+
options.delete(:dependent)
|
70
|
+
define_method(method_name) do
|
71
|
+
method = reflection.collection? ? :empty? : :nil?
|
72
|
+
unless send(reflection.name).send(method)
|
73
|
+
raise ActiveRecord::DetailedDeleteRestrictionError.new(reflection.name, self)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
before_destroy method_name
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def active_record_4?
|
81
|
+
::ActiveRecord::VERSION::MAJOR == 4
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
ActiveRecord::Base.send(:include, DependentRestrict)
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
# This error is raised when trying to destroy a parent instance in N:1 or 1:1 associations
|
3
|
+
# (has_many, has_one) when there is at least 1 child associated instance.
|
4
|
+
# ex: if @project.tasks.size > 0, DeleteRestrictionError will be raised when trying to destroy @project
|
5
|
+
class DetailedDeleteRestrictionError < ActiveRecordError #:nodoc:
|
6
|
+
def initialize(name, record)
|
7
|
+
@name = name
|
8
|
+
@record = record
|
9
|
+
super(basic_message)
|
10
|
+
end
|
11
|
+
|
12
|
+
def basic_message
|
13
|
+
name = @name.to_s.gsub('_', ' ')
|
14
|
+
assoc = @record.send(@name)
|
15
|
+
count = assoc.respond_to?(:count) ? assoc.count : (assoc ? 1 : 0)
|
16
|
+
if count == 1
|
17
|
+
"Cannot delete record because dependent #{name} exists"
|
18
|
+
else
|
19
|
+
"Cannot delete record because #{count} dependent #{name.pluralize} exist"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def detailed_message
|
24
|
+
count = @record.send(@name).count
|
25
|
+
examples = @record.send(@name).all(:limit => 5).map{|o| "#{o.id}: #{o.to_s}"}
|
26
|
+
examples[4] = "...and #{count - 4} more" if count > 5
|
27
|
+
basic_message + "\n\n\nThese include:\n#{examples.join("\n")}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
DB_FILE = 'tmp/test_db'
|
4
|
+
FileUtils.mkdir_p File.dirname(DB_FILE)
|
5
|
+
FileUtils.rm_f DB_FILE
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
|
8
|
+
|
9
|
+
load 'spec/schema.rb'
|
10
|
+
|
11
|
+
|
12
|
+
describe DependentRestrict do
|
13
|
+
context 'should restrict' do
|
14
|
+
before do
|
15
|
+
class OrderInvoice < ActiveRecord::Base
|
16
|
+
belongs_to :order
|
17
|
+
end
|
18
|
+
|
19
|
+
class Order < ActiveRecord::Base
|
20
|
+
belongs_to :category
|
21
|
+
has_one :order_invoice, :dependent => :restrict_with_exception
|
22
|
+
def to_s
|
23
|
+
"Order #{id}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Category < ActiveRecord::Base
|
28
|
+
has_many :orders, :dependent => :restrict_with_exception do
|
29
|
+
def active
|
30
|
+
self.select(&:active?)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
def to_s
|
34
|
+
"Category #{id}"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
after do
|
40
|
+
%w(OrderInvoice Order Category).each { |klass| Object.send(:remove_const, klass) }
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should restrict has_many relationships' do
|
44
|
+
category = Category.create!
|
45
|
+
5.times { Order.create!(:category => category) }
|
46
|
+
expect { category.reload.destroy }.to raise_error(
|
47
|
+
ActiveRecord::DetailedDeleteRestrictionError,
|
48
|
+
'Cannot delete record because 5 dependent orders exist'
|
49
|
+
)
|
50
|
+
begin
|
51
|
+
category.destroy
|
52
|
+
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
53
|
+
e.detailed_message.should == "Cannot delete record because 5 dependent orders exist\n\n\nThese include:\n1: Order 1\n2: Order 2\n3: Order 3\n4: Order 4\n5: Order 5"
|
54
|
+
end
|
55
|
+
1.times { Order.create!(:category => category) }
|
56
|
+
begin
|
57
|
+
category.destroy
|
58
|
+
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
59
|
+
e.detailed_message.should == "Cannot delete record because 6 dependent orders exist\n\n\nThese include:\n1: Order 1\n2: Order 2\n3: Order 3\n4: Order 4\n...and 2 more"
|
60
|
+
end
|
61
|
+
|
62
|
+
Order.destroy_all
|
63
|
+
expect{category.reload.destroy}.to_not raise_error
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should restrict has_one relationships' do
|
67
|
+
order = Order.create!
|
68
|
+
order_invoice = OrderInvoice.create!(:order => order)
|
69
|
+
expect{order.reload.destroy}.to raise_error(
|
70
|
+
ActiveRecord::DetailedDeleteRestrictionError,
|
71
|
+
'Cannot delete record because dependent order invoice exists'
|
72
|
+
)
|
73
|
+
|
74
|
+
order_invoice.destroy
|
75
|
+
expect{order.reload.destroy}.to_not raise_error
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should still filter active' do
|
79
|
+
category = Category.create!
|
80
|
+
3.times { Order.create!(:category => category, :active => true) }
|
81
|
+
2.times { Order.create!(:category => category, :active => false) }
|
82
|
+
category.orders.active.count.should == 3
|
83
|
+
|
84
|
+
Category.delete_all
|
85
|
+
Order.delete_all
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'should restrict_with_error' do
|
90
|
+
before do
|
91
|
+
class OrderInvoice < ActiveRecord::Base
|
92
|
+
belongs_to :order
|
93
|
+
end
|
94
|
+
|
95
|
+
class Order < ActiveRecord::Base
|
96
|
+
belongs_to :category
|
97
|
+
has_one :order_invoice, :dependent => :restrict_with_error
|
98
|
+
def to_s
|
99
|
+
"Order #{id}"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class Category < ActiveRecord::Base
|
104
|
+
has_many :orders, :dependent => :restrict_with_error
|
105
|
+
def to_s
|
106
|
+
"Category #{id}"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
after do
|
112
|
+
%w(OrderInvoice Order Category).each { |klass| Object.send(:remove_const, klass) }
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should restrict has_many relationships' do
|
116
|
+
category = Category.create!
|
117
|
+
Category.count.should == 1
|
118
|
+
5.times { Order.create!(:category => category) }
|
119
|
+
category.destroy
|
120
|
+
Category.count.should == 1
|
121
|
+
Order.destroy_all
|
122
|
+
category.reload.destroy
|
123
|
+
Category.count.should == 0
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'should restrict has_one relationships' do
|
127
|
+
order = Order.create!
|
128
|
+
Order.count.should == 1
|
129
|
+
order_invoice = OrderInvoice.create!(:order => order)
|
130
|
+
order.reload.destroy
|
131
|
+
Order.count.should == 1
|
132
|
+
|
133
|
+
order_invoice.destroy
|
134
|
+
order.reload.destroy
|
135
|
+
Order.count.should == 0
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
create_table :categories do |t|
|
3
|
+
end
|
4
|
+
|
5
|
+
create_table :order_invoices do |t|
|
6
|
+
t.integer :order_id
|
7
|
+
t.boolean :active
|
8
|
+
end
|
9
|
+
|
10
|
+
create_table :orders do |t|
|
11
|
+
t.integer :category_id
|
12
|
+
t.boolean :active
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper.rb"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
require 'bundler/setup'
|
10
|
+
|
11
|
+
if ENV['COVERAGE']
|
12
|
+
require 'simplecov'
|
13
|
+
require 'simplecov-rcov'
|
14
|
+
SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
|
15
|
+
SimpleCov.start do
|
16
|
+
add_filter '/vendor/'
|
17
|
+
add_filter '/spec/'
|
18
|
+
add_group 'lib', 'lib'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'dependent_restrict'
|
23
|
+
|
24
|
+
RSpec.configure do |config|
|
25
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
26
|
+
config.run_all_when_everything_filtered = true
|
27
|
+
config.filter_run :focus
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dependent_restrict
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michael Noack
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.3.0
|
20
|
+
- - <
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 5.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.3.0
|
30
|
+
- - <
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 5.0.0
|
33
|
+
description: This gem is not needed in Rails 3 as dependent => :raise is included
|
34
|
+
in 3.0.
|
35
|
+
email: development@travellink.com.au
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files:
|
39
|
+
- README.md
|
40
|
+
- LICENSE
|
41
|
+
files:
|
42
|
+
- Gemfile
|
43
|
+
- LICENSE
|
44
|
+
- README.md
|
45
|
+
- Rakefile
|
46
|
+
- dependent_restrict.gemspec
|
47
|
+
- gemfiles/rails2.gemfile
|
48
|
+
- gemfiles/rails3.gemfile
|
49
|
+
- gemfiles/rails4.gemfile
|
50
|
+
- lib/dependent_restrict.rb
|
51
|
+
- lib/dependent_restrict/delete_restriction_error.rb
|
52
|
+
- spec/dependent_protect_spec.rb
|
53
|
+
- spec/schema.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
homepage: http://github.com/sealink/dependent_restrict
|
56
|
+
licenses: []
|
57
|
+
metadata: {}
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options:
|
60
|
+
- --charset=UTF-8
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project: dependent_restrict
|
75
|
+
rubygems_version: 2.0.3
|
76
|
+
signing_key:
|
77
|
+
specification_version: 2
|
78
|
+
summary: Add dependent restrict and improves functionality to ActiveRecord 2/3/4.x.
|
79
|
+
test_files: []
|