dependent_restrict 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/.travis.yml +13 -0
- data/Gemfile +1 -7
- data/README.md +5 -0
- data/Rakefile +1 -133
- data/dependent_restrict.gemspec +26 -81
- data/gemfiles/rails3.gemfile +2 -1
- data/gemfiles/rails4_0.gemfile +1 -1
- data/gemfiles/rails4_1.gemfile +1 -1
- data/lib/dependent_restrict.rb +50 -30
- data/spec/dependent_restrict_spec.rb +26 -21
- data/spec/schema.rb +1 -1
- data/spec/spec_helper.rb +21 -7
- metadata +142 -23
- checksums.yaml +0 -7
- data/gemfiles/rails2.gemfile +0 -12
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.9.3
|
4
|
+
- 2.0.0
|
5
|
+
script: "bundle exec rake coverage"
|
6
|
+
gemfile:
|
7
|
+
- gemfiles/rails3.gemfile
|
8
|
+
- gemfiles/rails4_0.gemfile
|
9
|
+
- gemfiles/rails4_1.gemfile
|
10
|
+
notifications:
|
11
|
+
email:
|
12
|
+
- support@travellink.com.au
|
13
|
+
flowdock: e69dcafad1fea15c6b8c76e9ced965af
|
data/Gemfile
CHANGED
@@ -2,12 +2,6 @@ source 'https://rubygems.org'
|
|
2
2
|
gemspec
|
3
3
|
|
4
4
|
group :development, :test do
|
5
|
-
gem 'pry-debugger'
|
6
|
-
gem 'rake', '~> 0.9.2'
|
7
|
-
gem 'rdoc', '~> 3.12'
|
8
|
-
gem 'rspec'
|
9
|
-
gem 'simplecov'
|
10
|
-
gem 'simplecov-rcov'
|
11
|
-
gem 'sqlite3'
|
12
5
|
gem 'activerecord', '~> 4.0.0'
|
6
|
+
gem 'mime-types', '< 2.0', :platforms => :ruby_18 # coveralls
|
13
7
|
end
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
Dependent Restrict
|
2
2
|
==================
|
3
3
|
|
4
|
+
[![Build Status](https://travis-ci.org/sealink/dependent_restrict.png?branch=master)](https://travis-ci.org/sealink/dependent_restrict)
|
5
|
+
[![Coverage Status](https://coveralls.io/repos/sealink/dependent_restrict/badge.png)](https://coveralls.io/r/sealink/dependent_restrict)
|
6
|
+
[![Dependency Status](https://gemnasium.com/sealink/dependent_restrict.png?travis)](https://gemnasium.com/sealink/dependent_restrict)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/sealink/dependent_restrict.png)](https://codeclimate.com/github/sealink/dependent_restrict)
|
8
|
+
|
4
9
|
A gem for rails 2, 3 and 4 that retrofits and improves rails 4 functionality
|
5
10
|
|
6
11
|
Rails 4 offers 2 very useful dependent restrictions:
|
data/Rakefile
CHANGED
@@ -1,47 +1,4 @@
|
|
1
|
-
require
|
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
|
-
#############################################################################
|
1
|
+
require "bundler/gem_tasks"
|
45
2
|
|
46
3
|
desc 'Default: run specs.'
|
47
4
|
task :default => :spec
|
@@ -59,92 +16,3 @@ task :coverage do
|
|
59
16
|
ENV['COVERAGE'] = 'true'
|
60
17
|
Rake::Task['spec'].invoke
|
61
18
|
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
|
data/dependent_restrict.gemspec
CHANGED
@@ -1,82 +1,27 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
-
s.license = 'MIT'
|
32
|
-
|
33
|
-
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
34
|
-
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
35
|
-
s.require_paths = %w[lib]
|
36
|
-
|
37
|
-
## This sections is only necessary if you have C extensions.
|
38
|
-
# s.require_paths << 'ext'
|
39
|
-
# s.extensions = %w[ext/extconf.rb]
|
40
|
-
|
41
|
-
## If your gem includes any executables, list them here.
|
42
|
-
# s.executables = ["name"]
|
43
|
-
|
44
|
-
## Specify any RDoc options here. You'll want to add your README and
|
45
|
-
## LICENSE files to the extra_rdoc_files list.
|
46
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
47
|
-
s.extra_rdoc_files = %w[README.md LICENSE]
|
48
|
-
|
49
|
-
## List your runtime dependencies here. Runtime dependencies are those
|
50
|
-
## that are needed for an end user to actually USE your code.
|
51
|
-
s.add_dependency('activerecord', [">= 3.0", "< 5.0.0"])
|
52
|
-
|
53
|
-
## List your development dependencies here. Development dependencies are
|
54
|
-
## those that are only needed during development
|
55
|
-
# s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
|
56
|
-
|
57
|
-
## Leave this section as-is. It will be automatically generated from the
|
58
|
-
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
59
|
-
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
60
|
-
# = MANIFEST =
|
61
|
-
s.files = %w[
|
62
|
-
Gemfile
|
63
|
-
LICENSE
|
64
|
-
README.md
|
65
|
-
Rakefile
|
66
|
-
dependent_restrict.gemspec
|
67
|
-
gemfiles/rails2.gemfile
|
68
|
-
gemfiles/rails3.gemfile
|
69
|
-
gemfiles/rails4_0.gemfile
|
70
|
-
gemfiles/rails4_1.gemfile
|
71
|
-
lib/dependent_restrict.rb
|
72
|
-
lib/dependent_restrict/delete_restriction_error.rb
|
73
|
-
spec/dependent_restrict_spec.rb
|
74
|
-
spec/schema.rb
|
75
|
-
spec/spec_helper.rb
|
76
|
-
]
|
77
|
-
# = MANIFEST =
|
78
|
-
|
79
|
-
## Test files will be grabbed from the file list. Make sure the path glob
|
80
|
-
## matches what you actually use.
|
81
|
-
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "dependent_restrict"
|
5
|
+
spec.version = "0.2.2"
|
6
|
+
spec.authors = ["Michael Noack"]
|
7
|
+
spec.email = ["support@travellink.com.au"]
|
8
|
+
spec.description = %q{This gem is not needed in Rails 3 as dependent => :raise is included in 3.0.}
|
9
|
+
spec.summary = %q{Add dependent restrict and improves functionality to ActiveRecord 2/3/4.x.}
|
10
|
+
spec.homepage = 'http://github.com/sealink/dependent_restrict'
|
11
|
+
|
12
|
+
spec.license = "MIT"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_runtime_dependency "activerecord", ">= 3.0"
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "simplecov"
|
24
|
+
spec.add_development_dependency "simplecov-rcov"
|
25
|
+
spec.add_development_dependency "coveralls"
|
26
|
+
spec.add_development_dependency 'sqlite3'
|
82
27
|
end
|
data/gemfiles/rails3.gemfile
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
gemspec :path => '../'
|
3
3
|
|
4
4
|
group :development, :test do
|
@@ -9,4 +9,5 @@ group :development, :test do
|
|
9
9
|
gem 'simplecov-rcov'
|
10
10
|
gem 'sqlite3'
|
11
11
|
gem 'activerecord', '~> 3.2.0'
|
12
|
+
gem 'mime-types', '< 2.0', :platforms => :ruby_18 # coveralls
|
12
13
|
end
|
data/gemfiles/rails4_0.gemfile
CHANGED
data/gemfiles/rails4_1.gemfile
CHANGED
data/lib/dependent_restrict.rb
CHANGED
@@ -2,7 +2,7 @@ require 'active_record'
|
|
2
2
|
require 'dependent_restrict/delete_restriction_error'
|
3
3
|
|
4
4
|
module DependentRestrict
|
5
|
-
VERSION = '0.2.
|
5
|
+
VERSION = '0.2.2'
|
6
6
|
|
7
7
|
def self.included(base)
|
8
8
|
super
|
@@ -18,54 +18,74 @@ module DependentRestrict
|
|
18
18
|
end
|
19
19
|
|
20
20
|
module ClassMethods
|
21
|
+
VALID_DEPENDENTS = [:rollback, :restrict_with_error, :restrict, :restrict_with_exception]
|
22
|
+
|
21
23
|
# We should be aliasing configure_dependency_for_has_many but that method
|
22
24
|
# is private so we can't. We alias has_many instead trying to be as fair
|
23
25
|
# as we can to the original behaviour.
|
24
|
-
def has_one_with_restrict(*args)
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
26
|
+
def has_one_with_restrict(*args, &extension)
|
27
|
+
options = args.extract_options! || {}
|
28
|
+
if VALID_DEPENDENTS.include?(options[:dependent].try(:to_sym))
|
29
|
+
reflection = if active_record_4?
|
30
|
+
association_id, scope = *args
|
31
|
+
restrict_create_reflection(:has_one, association_id, scope || {}, options, self)
|
32
|
+
else
|
33
|
+
association_id = args[0]
|
34
|
+
create_reflection(:has_one, association_id, options, self)
|
35
|
+
end
|
36
|
+
add_dependency_callback!(reflection, options)
|
31
37
|
end
|
32
|
-
|
33
|
-
has_one_without_restrict(*args
|
38
|
+
args << options
|
39
|
+
has_one_without_restrict(*args, &extension)
|
34
40
|
end
|
35
41
|
|
36
|
-
def has_many_with_restrict(
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
42
|
+
def has_many_with_restrict(*args, &extension)
|
43
|
+
options = args.extract_options! || {}
|
44
|
+
if VALID_DEPENDENTS.include?(options[:dependent].try(:to_sym))
|
45
|
+
reflection = if active_record_4?
|
46
|
+
association_id, scope = *args
|
47
|
+
restrict_create_reflection(:has_many, association_id, scope || {}, options, self)
|
48
|
+
else
|
49
|
+
association_id = args.first
|
50
|
+
create_reflection(:has_many, association_id, options, self)
|
51
|
+
end
|
52
|
+
add_dependency_callback!(reflection, options)
|
41
53
|
end
|
42
|
-
|
43
|
-
has_many_without_restrict(
|
54
|
+
args << options
|
55
|
+
has_many_without_restrict(*args, &extension)
|
44
56
|
end
|
45
57
|
|
46
|
-
def has_and_belongs_to_many_with_restrict(
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
58
|
+
def has_and_belongs_to_many_with_restrict(*args, &extension)
|
59
|
+
options = args.extract_options! || {}
|
60
|
+
if VALID_DEPENDENTS.include?(options[:dependent].try(:to_sym))
|
61
|
+
reflection = if active_record_4?
|
62
|
+
raise ArgumentError, "dependent_restrict doesn't work with has_and_belongs_to_many. Use equivalent rails 4.1 has_many :through" if ActiveRecord::Reflection.respond_to? :create
|
63
|
+
association_id, scope = *args
|
64
|
+
restrict_create_reflection(:has_and_belongs_to_many, association_id, scope || {}, options, self)
|
65
|
+
else
|
66
|
+
association_id = args.first
|
67
|
+
create_reflection(:has_and_belongs_to_many, association_id, options, self)
|
68
|
+
end
|
69
|
+
add_dependency_callback!(reflection, options)
|
70
|
+
options.delete(:dependent)
|
52
71
|
end
|
53
|
-
|
54
|
-
|
55
|
-
has_and_belongs_to_many_without_restrict(association_id, options, &extension)
|
72
|
+
args << options
|
73
|
+
has_and_belongs_to_many_without_restrict(*args, &extension)
|
56
74
|
end
|
57
75
|
|
58
76
|
private
|
59
77
|
|
60
78
|
def add_dependency_callback!(reflection, options)
|
61
79
|
dependent_type = active_record_4? ? options[:dependent] : reflection.options[:dependent]
|
62
|
-
|
80
|
+
name = reflection.name
|
81
|
+
name = name.first if name.is_a?(Array) # rails 3
|
82
|
+
method_name = "dependent_#{dependent_type}_for_#{name}"
|
63
83
|
case dependent_type
|
64
84
|
when :rollback, :restrict_with_error
|
65
85
|
options.delete(:dependent)
|
66
86
|
define_method(method_name) do
|
67
87
|
method = reflection.collection? ? :empty? : :nil?
|
68
|
-
unless send(
|
88
|
+
unless send(name).send(method)
|
69
89
|
raise ActiveRecord::Rollback
|
70
90
|
end
|
71
91
|
end
|
@@ -74,8 +94,8 @@ module DependentRestrict
|
|
74
94
|
options.delete(:dependent)
|
75
95
|
define_method(method_name) do
|
76
96
|
method = reflection.collection? ? :empty? : :nil?
|
77
|
-
unless send(
|
78
|
-
raise ActiveRecord::DetailedDeleteRestrictionError.new(
|
97
|
+
unless send(name).send(method)
|
98
|
+
raise ActiveRecord::DetailedDeleteRestrictionError.new(name, self)
|
79
99
|
end
|
80
100
|
end
|
81
101
|
before_destroy method_name
|
@@ -1,15 +1,6 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
|
-
DB_FILE = 'tmp/test_db'
|
5
|
-
FileUtils.mkdir_p File.dirname(DB_FILE)
|
6
|
-
FileUtils.rm_f DB_FILE
|
7
|
-
|
8
|
-
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
|
9
|
-
|
10
|
-
load 'spec/schema.rb'
|
11
|
-
|
12
|
-
|
13
4
|
describe DependentRestrict do
|
14
5
|
|
15
6
|
context 'when associations are defined' do
|
@@ -44,10 +35,12 @@ describe DependentRestrict do
|
|
44
35
|
has_and_belongs_to_many :categories
|
45
36
|
end
|
46
37
|
end
|
38
|
+
|
47
39
|
end
|
48
40
|
|
49
41
|
after do
|
50
|
-
|
42
|
+
[OrderInvoice, Order, Category, ProductsCategory, Product].each(&:delete_all)
|
43
|
+
%w(OrderInvoice Order Category ProductsCategory Product).each { |klass| Object.send(:remove_const, klass) }
|
51
44
|
end
|
52
45
|
|
53
46
|
|
@@ -64,6 +57,8 @@ describe DependentRestrict do
|
|
64
57
|
self.select(&:active?)
|
65
58
|
end
|
66
59
|
end
|
60
|
+
|
61
|
+
has_many :order_invoices, :through => :orders
|
67
62
|
end
|
68
63
|
}.to_not raise_error
|
69
64
|
end
|
@@ -82,9 +77,19 @@ describe DependentRestrict do
|
|
82
77
|
self.select(&:active?)
|
83
78
|
end
|
84
79
|
end
|
80
|
+
|
81
|
+
has_many :order_invoices, :through => :orders, :dependent => :restrict_with_exception
|
85
82
|
end
|
86
83
|
end
|
87
84
|
|
85
|
+
it 'should create the reflections on Order' do
|
86
|
+
expect(Order.reflect_on_all_associations.map(&:name)).to eq [:category, :order_invoice]
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'should create the reflections on Category' do
|
90
|
+
expect(Category.reflect_on_all_associations.map(&:name)).to eq [:orders, :order_invoices]
|
91
|
+
end
|
92
|
+
|
88
93
|
it 'should restrict has_many relationships' do
|
89
94
|
category = Category.create!
|
90
95
|
5.times { Order.create!(:category => category) }
|
@@ -95,13 +100,13 @@ describe DependentRestrict do
|
|
95
100
|
begin
|
96
101
|
category.destroy
|
97
102
|
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
98
|
-
e.detailed_message.
|
103
|
+
expect(e.detailed_message).to eq "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"
|
99
104
|
end
|
100
105
|
1.times { Order.create!(:category => category) }
|
101
106
|
begin
|
102
107
|
category.destroy
|
103
108
|
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
104
|
-
e.detailed_message.
|
109
|
+
expect(e.detailed_message).to eq "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"
|
105
110
|
end
|
106
111
|
|
107
112
|
Order.destroy_all
|
@@ -124,7 +129,7 @@ describe DependentRestrict do
|
|
124
129
|
category = Category.create!
|
125
130
|
3.times { Order.create!(:category => category, :active => true) }
|
126
131
|
2.times { Order.create!(:category => category, :active => false) }
|
127
|
-
category.orders.active.count.
|
132
|
+
expect(category.orders.active.count).to eq 3
|
128
133
|
|
129
134
|
Category.delete_all
|
130
135
|
Order.delete_all
|
@@ -174,13 +179,13 @@ describe DependentRestrict do
|
|
174
179
|
begin
|
175
180
|
category.destroy
|
176
181
|
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
177
|
-
e.detailed_message.
|
182
|
+
expect(e.detailed_message).to eq "Não pode ser excluído pois 5 pedidos relacionados(as) foram encontrados(as)\n\n\nIncluindo:\n13: Order 13\n14: Order 14\n15: Order 15\n16: Order 16\n17: Order 17"
|
178
183
|
end
|
179
184
|
1.times { Order.create!(:category => category) }
|
180
185
|
begin
|
181
186
|
category.destroy
|
182
187
|
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
183
|
-
e.detailed_message.
|
188
|
+
expect(e.detailed_message).to eq "Não pode ser excluído pois 6 pedidos relacionados(as) foram encontrados(as)\n\n\nIncluindo:\n13: Order 13\n14: Order 14\n15: Order 15\n16: Order 16\n...e mais 2"
|
184
189
|
end
|
185
190
|
|
186
191
|
Order.destroy_all
|
@@ -219,25 +224,25 @@ describe DependentRestrict do
|
|
219
224
|
|
220
225
|
it 'should restrict has_many relationships' do
|
221
226
|
category = Category.create!
|
222
|
-
Category.count.
|
227
|
+
expect(Category.count).to eq 1
|
223
228
|
5.times { Order.create!(:category => category) }
|
224
229
|
category.destroy
|
225
|
-
Category.count.
|
230
|
+
expect(Category.count).to eq 1
|
226
231
|
Order.destroy_all
|
227
232
|
category.reload.destroy
|
228
|
-
Category.count.
|
233
|
+
expect(Category.count).to eq 0
|
229
234
|
end
|
230
235
|
|
231
236
|
it 'should restrict has_one relationships' do
|
232
237
|
order = Order.create!
|
233
|
-
Order.count.
|
238
|
+
expect(Order.count).to eq 1
|
234
239
|
order_invoice = OrderInvoice.create!(:order => order)
|
235
240
|
order.reload.destroy
|
236
|
-
Order.count.
|
241
|
+
expect(Order.count).to eq 1
|
237
242
|
|
238
243
|
order_invoice.destroy
|
239
244
|
order.reload.destroy
|
240
|
-
Order.count.
|
245
|
+
expect(Order.count).to eq 0
|
241
246
|
end
|
242
247
|
end
|
243
248
|
end
|
data/spec/schema.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -8,21 +8,35 @@
|
|
8
8
|
require 'rubygems'
|
9
9
|
require 'bundler/setup'
|
10
10
|
|
11
|
+
MINIMUM_COVERAGE = 81
|
12
|
+
|
11
13
|
if ENV['COVERAGE']
|
12
14
|
require 'simplecov'
|
13
|
-
require '
|
14
|
-
|
15
|
+
require 'coveralls'
|
16
|
+
Coveralls.wear!
|
17
|
+
|
18
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
15
19
|
SimpleCov.start do
|
16
20
|
add_filter '/vendor/'
|
17
21
|
add_filter '/spec/'
|
18
22
|
add_group 'lib', 'lib'
|
19
23
|
end
|
24
|
+
SimpleCov.at_exit do
|
25
|
+
SimpleCov.result.format!
|
26
|
+
percent = SimpleCov.result.covered_percent
|
27
|
+
unless percent >= MINIMUM_COVERAGE
|
28
|
+
puts "Coverage must be above #{MINIMUM_COVERAGE}%. It is #{"%.2f" % percent}%"
|
29
|
+
Kernel.exit(1)
|
30
|
+
end
|
31
|
+
end
|
20
32
|
end
|
21
33
|
|
22
34
|
require 'dependent_restrict'
|
23
35
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
36
|
+
DB_FILE = 'tmp/test_db'
|
37
|
+
FileUtils.mkdir_p File.dirname(DB_FILE)
|
38
|
+
FileUtils.rm_f DB_FILE
|
39
|
+
|
40
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => DB_FILE
|
41
|
+
|
42
|
+
load 'spec/schema.rb'
|
metadata
CHANGED
@@ -1,50 +1,160 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dependent_restrict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
7
|
authors:
|
7
8
|
- Michael Noack
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2014-
|
12
|
+
date: 2014-07-23 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: activerecord
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
16
18
|
requirements:
|
17
|
-
- -
|
19
|
+
- - ! '>='
|
18
20
|
- !ruby/object:Gem::Version
|
19
21
|
version: '3.0'
|
20
|
-
- - "<"
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 5.0.0
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '3.0'
|
30
|
-
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
31
60
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: simplecov
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov-rcov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: coveralls
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: sqlite3
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
33
142
|
description: This gem is not needed in Rails 3 as dependent => :raise is included
|
34
143
|
in 3.0.
|
35
|
-
email:
|
144
|
+
email:
|
145
|
+
- support@travellink.com.au
|
36
146
|
executables: []
|
37
147
|
extensions: []
|
38
|
-
extra_rdoc_files:
|
39
|
-
- README.md
|
40
|
-
- LICENSE
|
148
|
+
extra_rdoc_files: []
|
41
149
|
files:
|
150
|
+
- .gitignore
|
151
|
+
- .rspec
|
152
|
+
- .travis.yml
|
42
153
|
- Gemfile
|
43
154
|
- LICENSE
|
44
155
|
- README.md
|
45
156
|
- Rakefile
|
46
157
|
- dependent_restrict.gemspec
|
47
|
-
- gemfiles/rails2.gemfile
|
48
158
|
- gemfiles/rails3.gemfile
|
49
159
|
- gemfiles/rails4_0.gemfile
|
50
160
|
- gemfiles/rails4_1.gemfile
|
@@ -56,26 +166,35 @@ files:
|
|
56
166
|
homepage: http://github.com/sealink/dependent_restrict
|
57
167
|
licenses:
|
58
168
|
- MIT
|
59
|
-
metadata: {}
|
60
169
|
post_install_message:
|
61
|
-
rdoc_options:
|
62
|
-
- "--charset=UTF-8"
|
170
|
+
rdoc_options: []
|
63
171
|
require_paths:
|
64
172
|
- lib
|
65
173
|
required_ruby_version: !ruby/object:Gem::Requirement
|
174
|
+
none: false
|
66
175
|
requirements:
|
67
|
-
- -
|
176
|
+
- - ! '>='
|
68
177
|
- !ruby/object:Gem::Version
|
69
178
|
version: '0'
|
179
|
+
segments:
|
180
|
+
- 0
|
181
|
+
hash: 551629118570124682
|
70
182
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
none: false
|
71
184
|
requirements:
|
72
|
-
- -
|
185
|
+
- - ! '>='
|
73
186
|
- !ruby/object:Gem::Version
|
74
187
|
version: '0'
|
188
|
+
segments:
|
189
|
+
- 0
|
190
|
+
hash: 551629118570124682
|
75
191
|
requirements: []
|
76
|
-
rubyforge_project:
|
77
|
-
rubygems_version:
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 1.8.25
|
78
194
|
signing_key:
|
79
|
-
specification_version:
|
195
|
+
specification_version: 3
|
80
196
|
summary: Add dependent restrict and improves functionality to ActiveRecord 2/3/4.x.
|
81
|
-
test_files:
|
197
|
+
test_files:
|
198
|
+
- spec/dependent_restrict_spec.rb
|
199
|
+
- spec/schema.rb
|
200
|
+
- spec/spec_helper.rb
|
checksums.yaml
DELETED
@@ -1,7 +0,0 @@
|
|
1
|
-
---
|
2
|
-
SHA1:
|
3
|
-
metadata.gz: 98d742af335bdd2b42c95160496b044c555c2fc1
|
4
|
-
data.tar.gz: 7e2b5342a2c9ce2e2d3a8f842b1221109a3317f0
|
5
|
-
SHA512:
|
6
|
-
metadata.gz: b7e03ba470c1621de281bfebcc90f085502bc11c5f848e36065bf04e8b47049c516334fb08ed24fe4e61e36dba901ca97bc1ae40fb3570052a20e110b0ac3960
|
7
|
-
data.tar.gz: a06010a30a0ff6f5edff714928ac009ee42bb857a616e8ca03ddb92239a1773360db3d34c2c9e6c8860e71c85a2cfcc554f3256b4c50112ba37288903516ef05
|
data/gemfiles/rails2.gemfile
DELETED