poly_belongs_to 0.1.9 → 0.1.10
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 +4 -4
- data/README.md +11 -3
- data/lib/poly_belongs_to/core.rb +12 -1
- data/lib/poly_belongs_to/singleton_set.rb +7 -0
- data/lib/poly_belongs_to/version.rb +1 -1
- data/test/core_test.rb +25 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +23231 -0
- data/test/pbt_test.rb +22 -18
- data/test/singleton_set_test.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a87c53ed8f75c2ffbf32fba761b35e633bd8c0ba
|
4
|
+
data.tar.gz: 6483b2649d7128f5a8b6a6cef5c3a9ff040a1112
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef4a8c310620f69522b4ae9fa6e7b9a90f242bd38b5205969d77d60036bbf8af4f2961b339eede164ee5b3025a2a471863b96c64a601e384b75481c5888be33d
|
7
|
+
data.tar.gz: 6c8b1386efe6f406057e52e376c9dfda301db3ca9c0b8cae953438b7276ebcf77c2cef9f3e5f6630440872ca94c56e050a6de215c131dfdf65b5c9849c4ca99c
|
data/README.md
CHANGED
@@ -61,6 +61,10 @@ MyObject.first.pbt_type
|
|
61
61
|
MyObject.first.pbt_parent
|
62
62
|
# => #<User id: 123 ... >
|
63
63
|
|
64
|
+
# Get Top Hierarchical Parent Object (Works on all belongs_to Objects)
|
65
|
+
MyObject.first.pbt_top_parent
|
66
|
+
# => #<User id: 123 ... >
|
67
|
+
|
64
68
|
# Mutliple Parent Objects (List of one item for Polymorphic, full list otherwise.)
|
65
69
|
Tire.first.pbt_parents
|
66
70
|
# => [#<User id: 123 ... >, #<Car id: 234 ... >]
|
@@ -128,14 +132,19 @@ PolyBelongsTo::Pbt::IsSingular[ obj, child ]
|
|
128
132
|
PolyBelongsTo::Pbt::IsPlural[ obj, child ]
|
129
133
|
|
130
134
|
# Returns the symbol for the CollectionProxy the child belongs to in relation to obj
|
131
|
-
# NOTE: For has_one the symbol is not a CollectionProxy, but the instance
|
135
|
+
# NOTE: For has_one the symbol is not a CollectionProxy, but represents the instance
|
132
136
|
PolyBelongsTo::Pbt::CollectionProxy[ obj, child ]
|
133
137
|
|
134
|
-
# Always returns a collection proxy; fakes a collection proxy for has_one
|
138
|
+
# Always returns a collection proxy; fakes a collection proxy for has_one
|
135
139
|
PolyBelongsTo::Pbt::AsCollectionProxy[ obj, child ]
|
136
140
|
|
137
141
|
# Wrapper for has_one objects to be a collection proxy
|
138
142
|
PolyBelongsTo::FakedCollection.new(obj, child)
|
143
|
+
|
144
|
+
# Track which DB records have already been processed
|
145
|
+
# Current recommended methods to use on instance are
|
146
|
+
# :add?, :include?, :flag, :flagged?
|
147
|
+
PolyBelongsTo::SingletonSet.new
|
139
148
|
```
|
140
149
|
##Record Duplication
|
141
150
|
|
@@ -169,7 +178,6 @@ contact.save
|
|
169
178
|
|
170
179
|
|
171
180
|
# For a fully recursive copy do the same with pbt_deep_dup_build
|
172
|
-
# Keep in mind this is vulnerable to infinite loops!
|
173
181
|
contact.pbt_deep_dup_build( User.last.profile )
|
174
182
|
|
175
183
|
# Remeber to save!
|
data/lib/poly_belongs_to/core.rb
CHANGED
@@ -56,7 +56,7 @@ module PolyBelongsTo
|
|
56
56
|
|
57
57
|
def pbt_parent
|
58
58
|
val = pbt
|
59
|
-
if val
|
59
|
+
if val && !pbt_id.nil?
|
60
60
|
if poly?
|
61
61
|
"#{pbt_type}".constantize.find(pbt_id)
|
62
62
|
else
|
@@ -67,6 +67,17 @@ module PolyBelongsTo
|
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
+
def pbt_top_parent
|
71
|
+
record = self
|
72
|
+
return nil unless record.pbt_parent
|
73
|
+
no_repeat = PolyBelongsTo::SingletonSet.new
|
74
|
+
while !no_repeat.include?(record.pbt_parent) && !record.pbt_parent.nil?
|
75
|
+
no_repeat.add?(record)
|
76
|
+
record = record.pbt_parent
|
77
|
+
end
|
78
|
+
record
|
79
|
+
end
|
80
|
+
|
70
81
|
def pbt_parents
|
71
82
|
if poly?
|
72
83
|
Array[pbt_parent].compact
|
@@ -18,15 +18,20 @@ module PolyBelongsTo
|
|
18
18
|
result
|
19
19
|
end
|
20
20
|
|
21
|
+
# <b>DEPRECATED:</b> Please use <tt>add?</tt> instead. Behavior changing in 0.2.0.
|
21
22
|
def add(record)
|
23
|
+
warn "[DEPRECATION] `add` will revert to normal behavior in 0.2.0. Please maintain behavior with `add?` instead."
|
22
24
|
add?(record)
|
23
25
|
end
|
24
26
|
|
27
|
+
# <b>DEPRECATED:</b> Please use <tt>add?</tt> instead. Behavior changing in 0.2.0.
|
25
28
|
def <<(record)
|
29
|
+
warn "[DEPRECATION] `<<` will revert to normal behavior in 0.2.0. Please maintain behavior with `add?` instead."
|
26
30
|
add?(record)
|
27
31
|
end
|
28
32
|
|
29
33
|
def include?(record)
|
34
|
+
return nil unless record
|
30
35
|
@set.include?(formatted_name(record))
|
31
36
|
end
|
32
37
|
|
@@ -35,10 +40,12 @@ module PolyBelongsTo
|
|
35
40
|
end
|
36
41
|
|
37
42
|
def flagged?(record)
|
43
|
+
return nil unless record
|
38
44
|
@flagged.include?(formatted_name(record))
|
39
45
|
end
|
40
46
|
|
41
47
|
def method_missing(mthd)
|
48
|
+
# TODO Add parameter support and substiture record parameters with formatted_name(record)
|
42
49
|
@set.send(mthd)
|
43
50
|
end
|
44
51
|
end
|
data/test/core_test.rb
CHANGED
@@ -179,6 +179,31 @@ class CoreTest < ActiveSupport::TestCase
|
|
179
179
|
profile.pbt_parent.id.must_be_same_as user.id
|
180
180
|
end
|
181
181
|
|
182
|
+
it "#pbt_parent returns nil if no ID is set for parent relationship" do
|
183
|
+
alpha = Alpha.new; alpha.save
|
184
|
+
alpha.pbt_parent.must_be_nil
|
185
|
+
end
|
186
|
+
|
187
|
+
it "#pbt_top_parent climbs up belongs_to hierarchy to the top; polymorphic relationships first" do
|
188
|
+
alpha = Alpha.new; alpha.save
|
189
|
+
beta = alpha.betas.build; beta.save
|
190
|
+
capa = beta.capas.build; capa.save
|
191
|
+
delta = capa.deltas.build; delta.save
|
192
|
+
delta.pbt_top_parent.must_equal alpha
|
193
|
+
end
|
194
|
+
|
195
|
+
it "#pbt_top_parent with circular relation goes till the repeat" do
|
196
|
+
alpha = Alpha.new; alpha.save
|
197
|
+
beta = alpha.betas.build; beta.save
|
198
|
+
capa = beta.capas.build; capa.save
|
199
|
+
delta = capa.deltas.build; delta.save
|
200
|
+
alpha.update(delta_id: delta.id)
|
201
|
+
delta.pbt_top_parent.must_equal alpha
|
202
|
+
alpha.pbt_top_parent.must_equal beta
|
203
|
+
beta. pbt_top_parent.must_equal capa
|
204
|
+
capa. pbt_top_parent.must_equal delta
|
205
|
+
end
|
206
|
+
|
182
207
|
it "#pbt_parents can show multiple parents" do
|
183
208
|
user = User.new(id: 1)
|
184
209
|
user.cars.build
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|