dependent_restrict 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
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=
2
+ SHA1:
3
+ metadata.gz: e8feb61d8673fb8d7c47c9bcd17bbe61549b9f5b
4
+ data.tar.gz: 5228752defef4573852096371587e648fbed6cb2
5
+ SHA512:
6
+ metadata.gz: 8c464b58a1cdeb06c7eb614398b90400bb7365059a8c8ff8ba9eabe243cd071176d8518d7ef15ac88c48e0c089401ae103ebf608b25f294a37bc12ac70a82630
7
+ data.tar.gz: e4b732f96aa87dad2ad12793d2726a5d854fefa8ba760e9aa073a115bc7663b2d6ddd3bb7d327c5757a5854be166bdfecbad887749b817ead5a8ebfdc296de20
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
3
 
4
4
  group :development, :test do
@@ -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.1.0'
17
- s.date = '2013-09-14'
16
+ s.version = '0.1.1'
17
+ s.date = '2013-11-27'
18
18
  s.rubyforge_project = 'dependent_restrict'
19
19
 
20
20
  ## Make sure your summary is short. The description may be as long
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.authors = ["Michael Noack"]
29
29
  s.email = 'development@travellink.com.au'
30
30
  s.homepage = 'http://github.com/sealink/dependent_restrict'
31
+ s.license = 'MIT'
31
32
 
32
33
  ## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
33
34
  ## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
@@ -68,7 +69,7 @@ Gem::Specification.new do |s|
68
69
  gemfiles/rails4.gemfile
69
70
  lib/dependent_restrict.rb
70
71
  lib/dependent_restrict/delete_restriction_error.rb
71
- spec/dependent_protect_spec.rb
72
+ spec/dependent_restrict_spec.rb
72
73
  spec/schema.rb
73
74
  spec/spec_helper.rb
74
75
  ]
@@ -2,7 +2,7 @@ require 'active_record'
2
2
  require 'dependent_restrict/delete_restriction_error'
3
3
 
4
4
  module DependentRestrict
5
- VERSION = '0.1.0'
5
+ VERSION = '0.1.1'
6
6
 
7
7
  def self.included(base)
8
8
  super
@@ -24,12 +24,12 @@ 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
- create_reflection(:has_one, association_id, options, scope ||= {}, self)
27
+ create_reflection(:has_one, association_id, options || {}, scope || {}, self)
28
28
  else
29
29
  association_id, options, extension = *args
30
- create_reflection(:has_one, association_id, options, self)
30
+ create_reflection(:has_one, association_id, options || {}, self)
31
31
  end
32
- add_dependency_callback!(reflection, options)
32
+ add_dependency_callback!(reflection, options || {})
33
33
  has_one_without_restrict(*args) #association_id, options, &extension)
34
34
  end
35
35
 
@@ -0,0 +1,163 @@
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
+
14
+ context 'when associations are defined' do
15
+ before do
16
+ class OrderInvoice < ActiveRecord::Base
17
+ belongs_to :order
18
+ end
19
+
20
+ class Order < ActiveRecord::Base
21
+ belongs_to :category
22
+
23
+ def to_s
24
+ "Order #{id}"
25
+ end
26
+ end
27
+
28
+ class Category < ActiveRecord::Base
29
+ def to_s
30
+ "Category #{id}"
31
+ end
32
+ end
33
+ end
34
+
35
+ after do
36
+ %w(OrderInvoice Order Category).each { |klass| Object.send(:remove_const, klass) }
37
+ end
38
+
39
+
40
+ context 'when not restricting' do
41
+ it 'should allow creating reflections' do
42
+ expect {
43
+ class Order < ActiveRecord::Base
44
+ has_one :order_invoice
45
+ end
46
+
47
+ class Category < ActiveRecord::Base
48
+ has_many :orders do
49
+ def active
50
+ self.select(&:active?)
51
+ end
52
+ end
53
+ end
54
+ }.to_not raise_error
55
+ end
56
+ end
57
+
58
+
59
+ context 'when restricting with exception' do
60
+ before do
61
+ class Order < ActiveRecord::Base
62
+ has_one :order_invoice, :dependent => :restrict_with_exception
63
+ end
64
+
65
+ class Category < ActiveRecord::Base
66
+ has_many :orders, :dependent => :restrict_with_exception do
67
+ def active
68
+ self.select(&:active?)
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ it 'should restrict has_many relationships' do
75
+ category = Category.create!
76
+ 5.times { Order.create!(:category => category) }
77
+ expect { category.reload.destroy }.to raise_error(
78
+ ActiveRecord::DetailedDeleteRestrictionError,
79
+ 'Cannot delete record because 5 dependent orders exist'
80
+ )
81
+ begin
82
+ category.destroy
83
+ rescue ActiveRecord::DetailedDeleteRestrictionError => e
84
+ 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"
85
+ end
86
+ 1.times { Order.create!(:category => category) }
87
+ begin
88
+ category.destroy
89
+ rescue ActiveRecord::DetailedDeleteRestrictionError => e
90
+ 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"
91
+ end
92
+
93
+ Order.destroy_all
94
+ expect { category.reload.destroy }.to_not raise_error
95
+ end
96
+
97
+ it 'should restrict has_one relationships' do
98
+ order = Order.create!
99
+ order_invoice = OrderInvoice.create!(:order => order)
100
+ expect { order.reload.destroy }.to raise_error(
101
+ ActiveRecord::DetailedDeleteRestrictionError,
102
+ 'Cannot delete record because dependent order invoice exists'
103
+ )
104
+
105
+ order_invoice.destroy
106
+ expect { order.reload.destroy }.to_not raise_error
107
+ end
108
+
109
+ it 'should still filter active' do
110
+ category = Category.create!
111
+ 3.times { Order.create!(:category => category, :active => true) }
112
+ 2.times { Order.create!(:category => category, :active => false) }
113
+ category.orders.active.count.should == 3
114
+
115
+ Category.delete_all
116
+ Order.delete_all
117
+ end
118
+ end
119
+
120
+
121
+ context 'when restricting with error' do
122
+ before do
123
+ class Order < ActiveRecord::Base
124
+ has_one :order_invoice, :dependent => :restrict_with_error
125
+ end
126
+
127
+ class Category < ActiveRecord::Base
128
+ has_many :orders, :dependent => :restrict_with_error do
129
+ def active
130
+ self.select(&:active?)
131
+ end
132
+ end
133
+ end
134
+ end
135
+
136
+ it 'should restrict has_many relationships' do
137
+ category = Category.create!
138
+ Category.count.should == 1
139
+ 5.times { Order.create!(:category => category) }
140
+ category.destroy
141
+ Category.count.should == 1
142
+ Order.destroy_all
143
+ category.reload.destroy
144
+ Category.count.should == 0
145
+ end
146
+
147
+ it 'should restrict has_one relationships' do
148
+ order = Order.create!
149
+ Order.count.should == 1
150
+ order_invoice = OrderInvoice.create!(:order => order)
151
+ order.reload.destroy
152
+ Order.count.should == 1
153
+
154
+ order_invoice.destroy
155
+ order.reload.destroy
156
+ Order.count.should == 0
157
+ end
158
+ end
159
+ end
160
+
161
+
162
+ end
163
+
metadata CHANGED
@@ -1,20 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dependent_restrict
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Noack
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-14 00:00:00.000000000 Z
11
+ date: 2013-11-27 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
19
  version: 2.3.0
20
20
  - - <
@@ -24,7 +24,7 @@ dependencies:
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
- - - ! '>='
27
+ - - '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 2.3.0
30
30
  - - <
@@ -49,11 +49,12 @@ files:
49
49
  - gemfiles/rails4.gemfile
50
50
  - lib/dependent_restrict.rb
51
51
  - lib/dependent_restrict/delete_restriction_error.rb
52
- - spec/dependent_protect_spec.rb
52
+ - spec/dependent_restrict_spec.rb
53
53
  - spec/schema.rb
54
54
  - spec/spec_helper.rb
55
55
  homepage: http://github.com/sealink/dependent_restrict
56
- licenses: []
56
+ licenses:
57
+ - MIT
57
58
  metadata: {}
58
59
  post_install_message:
59
60
  rdoc_options:
@@ -62,17 +63,17 @@ require_paths:
62
63
  - lib
63
64
  required_ruby_version: !ruby/object:Gem::Requirement
64
65
  requirements:
65
- - - ! '>='
66
+ - - '>='
66
67
  - !ruby/object:Gem::Version
67
68
  version: '0'
68
69
  required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  requirements:
70
- - - ! '>='
71
+ - - '>='
71
72
  - !ruby/object:Gem::Version
72
73
  version: '0'
73
74
  requirements: []
74
75
  rubyforge_project: dependent_restrict
75
- rubygems_version: 2.0.3
76
+ rubygems_version: 2.1.11
76
77
  signing_key:
77
78
  specification_version: 2
78
79
  summary: Add dependent restrict and improves functionality to ActiveRecord 2/3/4.x.
@@ -1,139 +0,0 @@
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
-