dependent_restrict 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +2 -2
- data/dependent_restrict.gemspec +3 -3
- data/lib/dependent_restrict.rb +16 -4
- data/lib/dependent_restrict/delete_restriction_error.rb +12 -6
- data/spec/dependent_restrict_spec.rb +71 -0
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5209e19995822bf472777d6201448a094fb3d187
|
4
|
+
data.tar.gz: 6cc0a772cc00306ce70584dbc8c6c967158b5d12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 449ec1f1875a8c6734074b6d460bbf59dc86d5f7081e3aad72c79ee948d1587ba98d41cc6224fd478d9318bce3c04d3a5b6e8e6222797b1e0f5fb59b92a8315f
|
7
|
+
data.tar.gz: 38f9409944640ab6c44742ca9e72dee85b1efdcb0ec4a203446461437fcf97abd157a6643fb9eb7c1770c8defd1a4c01585e0b00261a8cf5f99ac3c490c20423
|
data/LICENSE
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
The MIT License
|
2
2
|
|
3
|
-
Copyright (c)
|
3
|
+
Copyright (c) The Sealink Travel Group
|
4
4
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
21
|
+
THE SOFTWARE.
|
data/dependent_restrict.gemspec
CHANGED
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
|
|
13
13
|
## If your rubyforge_project name is different, then edit it and comment out
|
14
14
|
## the sub! line in the Rakefile
|
15
15
|
s.name = 'dependent_restrict'
|
16
|
-
s.version = '0.
|
17
|
-
s.date = '
|
16
|
+
s.version = '0.2.0'
|
17
|
+
s.date = '2014-05-08'
|
18
18
|
s.rubyforge_project = 'dependent_restrict'
|
19
19
|
|
20
20
|
## Make sure your summary is short. The description may be as long
|
@@ -48,7 +48,7 @@ Gem::Specification.new do |s|
|
|
48
48
|
|
49
49
|
## List your runtime dependencies here. Runtime dependencies are those
|
50
50
|
## that are needed for an end user to actually USE your code.
|
51
|
-
s.add_dependency('activerecord', [">=
|
51
|
+
s.add_dependency('activerecord', [">= 3.0", "< 5.0.0"])
|
52
52
|
|
53
53
|
## List your development dependencies here. Development dependencies are
|
54
54
|
## those that are only needed during development
|
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.
|
5
|
+
VERSION = '0.2.0'
|
6
6
|
|
7
7
|
def self.included(base)
|
8
8
|
super
|
@@ -24,7 +24,7 @@ module DependentRestrict
|
|
24
24
|
def has_one_with_restrict(*args) #:nodoc:
|
25
25
|
reflection = if active_record_4?
|
26
26
|
association_id, options, scope, extension = *args
|
27
|
-
|
27
|
+
restrict_create_reflection(:has_one, association_id, options || {}, scope || {}, self)
|
28
28
|
else
|
29
29
|
association_id, options, extension = *args
|
30
30
|
create_reflection(:has_one, association_id, options || {}, self)
|
@@ -35,7 +35,7 @@ module DependentRestrict
|
|
35
35
|
|
36
36
|
def has_many_with_restrict(association_id, options = {}, &extension) #:nodoc:
|
37
37
|
reflection = if active_record_4?
|
38
|
-
|
38
|
+
restrict_create_reflection(:has_many, association_id, options, scope ||= {}, self)
|
39
39
|
else
|
40
40
|
create_reflection(:has_many, association_id, options, self)
|
41
41
|
end
|
@@ -44,7 +44,11 @@ module DependentRestrict
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def has_and_belongs_to_many_with_restrict(association_id, options = {}, &extension)
|
47
|
-
reflection =
|
47
|
+
reflection = if active_record_4?
|
48
|
+
restrict_create_reflection(:has_and_belongs_to_many, association_id, options, scope ||= {}, self)
|
49
|
+
else
|
50
|
+
create_reflection(:has_and_belongs_to_many, association_id, options, self)
|
51
|
+
end
|
48
52
|
add_dependency_callback!(reflection, options)
|
49
53
|
options.delete(:dependent)
|
50
54
|
has_and_belongs_to_many_without_restrict(association_id, options, &extension)
|
@@ -81,6 +85,14 @@ module DependentRestrict
|
|
81
85
|
::ActiveRecord::VERSION::MAJOR == 4
|
82
86
|
end
|
83
87
|
|
88
|
+
def restrict_create_reflection(*args)
|
89
|
+
if ActiveRecord::Reflection.respond_to? :create
|
90
|
+
ActiveRecord::Reflection.create *args
|
91
|
+
else
|
92
|
+
create_reflection(*args)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
84
96
|
end
|
85
97
|
end
|
86
98
|
|
@@ -10,21 +10,27 @@ module ActiveRecord
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def basic_message
|
13
|
-
name = @name.to_s.gsub('_', ' ')
|
14
13
|
assoc = @record.send(@name)
|
15
14
|
count = assoc.respond_to?(:count) ? assoc.count : (assoc ? 1 : 0)
|
15
|
+
name = I18n.t(@name.to_s.singularize, {
|
16
|
+
:scope => [:activerecord, :models],
|
17
|
+
:count => count,
|
18
|
+
:default => count == 1 ? @name.to_s.gsub('_', ' ') : @name.to_s.gsub('_', ' ').pluralize
|
19
|
+
}).downcase
|
20
|
+
|
16
21
|
if count == 1
|
17
|
-
"Cannot delete record because dependent #{name} exists"
|
22
|
+
I18n.t('dependent_restrict.basic_message.one', :name => name, :default => "Cannot delete record because dependent #{name} exists")
|
18
23
|
else
|
19
|
-
"Cannot delete record because #{count} dependent #{name.pluralize} exist"
|
24
|
+
I18n.t('dependent_restrict.basic_message.others', :count => count, :name => name, :default => "Cannot delete record because #{count} dependent #{name.pluralize} exist")
|
20
25
|
end
|
21
26
|
end
|
22
27
|
|
23
28
|
def detailed_message
|
24
29
|
count = @record.send(@name).count
|
25
|
-
examples = @record.send(@name).
|
26
|
-
examples[4] = "
|
27
|
-
|
30
|
+
examples = @record.send(@name).limit(5).map{|o| "#{o.id}: #{o.to_s}"}
|
31
|
+
examples[4] = "...#{I18n.t('dependent_restrict.detailed_message.and_more', :count => count - 4, :default => "and #{count-4} more")}" if count > 5
|
32
|
+
|
33
|
+
basic_message + "\n\n\n#{I18n.t('dependent_restrict.detailed_message.includes', :default => "These include")}:\n#{examples.join("\n")}"
|
28
34
|
end
|
29
35
|
end
|
30
36
|
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
#encoding: utf-8
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
DB_FILE = 'tmp/test_db'
|
@@ -115,6 +116,76 @@ describe DependentRestrict do
|
|
115
116
|
Category.delete_all
|
116
117
|
Order.delete_all
|
117
118
|
end
|
119
|
+
|
120
|
+
context "using i18n" do
|
121
|
+
before do
|
122
|
+
I18n.backend.store_translations(:br, {
|
123
|
+
:dependent_restrict => {
|
124
|
+
:basic_message => {
|
125
|
+
:one => 'Não pode ser excluído pois um(a) %{name} relacionado(a) foi encontrado(a)',
|
126
|
+
:others => 'Não pode ser excluído pois %{count} %{name} relacionados(as) foram encontrados(as)'
|
127
|
+
},
|
128
|
+
:detailed_message => {
|
129
|
+
:and_more => "e mais %{count}",
|
130
|
+
:includes => "Incluindo"
|
131
|
+
}
|
132
|
+
},
|
133
|
+
:activerecord => {
|
134
|
+
:models => {
|
135
|
+
:order => {
|
136
|
+
:one => "Pedido",
|
137
|
+
:other => "Pedidos"
|
138
|
+
},
|
139
|
+
:order_invoice => {
|
140
|
+
:one => "Ordem de pedido"
|
141
|
+
}
|
142
|
+
}
|
143
|
+
}
|
144
|
+
})
|
145
|
+
|
146
|
+
I18n.locale = :br
|
147
|
+
end
|
148
|
+
|
149
|
+
after do
|
150
|
+
I18n.locale = :en
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should restrict has_many relationships' do
|
154
|
+
|
155
|
+
category = Category.create!
|
156
|
+
5.times { Order.create!(:category => category) }
|
157
|
+
expect { category.reload.destroy }.to raise_error(
|
158
|
+
ActiveRecord::DetailedDeleteRestrictionError,
|
159
|
+
'Não pode ser excluído pois 5 pedidos relacionados(as) foram encontrados(as)'
|
160
|
+
)
|
161
|
+
begin
|
162
|
+
category.destroy
|
163
|
+
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
164
|
+
e.detailed_message.should == "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"
|
165
|
+
end
|
166
|
+
1.times { Order.create!(:category => category) }
|
167
|
+
begin
|
168
|
+
category.destroy
|
169
|
+
rescue ActiveRecord::DetailedDeleteRestrictionError => e
|
170
|
+
e.detailed_message.should == "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"
|
171
|
+
end
|
172
|
+
|
173
|
+
Order.destroy_all
|
174
|
+
expect { category.reload.destroy }.to_not raise_error
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'should restrict has_one relationships' do
|
178
|
+
order = Order.create!
|
179
|
+
order_invoice = OrderInvoice.create!(:order => order)
|
180
|
+
expect { order.reload.destroy }.to raise_error(
|
181
|
+
ActiveRecord::DetailedDeleteRestrictionError,
|
182
|
+
'Não pode ser excluído pois um(a) ordem de pedido relacionado(a) foi encontrado(a)'
|
183
|
+
)
|
184
|
+
|
185
|
+
order_invoice.destroy
|
186
|
+
expect { order.reload.destroy }.to_not raise_error
|
187
|
+
end
|
188
|
+
end
|
118
189
|
end
|
119
190
|
|
120
191
|
|
metadata
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dependent_restrict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Noack
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
20
|
-
- - <
|
19
|
+
version: '3.0'
|
20
|
+
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: 5.0.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
30
|
-
- - <
|
29
|
+
version: '3.0'
|
30
|
+
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: 5.0.0
|
33
33
|
description: This gem is not needed in Rails 3 as dependent => :raise is included
|
@@ -58,22 +58,22 @@ licenses:
|
|
58
58
|
metadata: {}
|
59
59
|
post_install_message:
|
60
60
|
rdoc_options:
|
61
|
-
- --charset=UTF-8
|
61
|
+
- "--charset=UTF-8"
|
62
62
|
require_paths:
|
63
63
|
- lib
|
64
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
70
|
requirements:
|
71
|
-
- -
|
71
|
+
- - ">="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
75
|
rubyforge_project: dependent_restrict
|
76
|
-
rubygems_version: 2.
|
76
|
+
rubygems_version: 2.2.0
|
77
77
|
signing_key:
|
78
78
|
specification_version: 2
|
79
79
|
summary: Add dependent restrict and improves functionality to ActiveRecord 2/3/4.x.
|