poly_belongs_to 0.1.9 → 0.1.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2485663b8e0b1364a05b151975b527c56e95bada
4
- data.tar.gz: 33338ae33151a22083b535ea79a706b237cf64aa
3
+ metadata.gz: a87c53ed8f75c2ffbf32fba761b35e633bd8c0ba
4
+ data.tar.gz: 6483b2649d7128f5a8b6a6cef5c3a9ff040a1112
5
5
  SHA512:
6
- metadata.gz: 52ceb7ef74ed74c91012ae7ac3fa662da98653ee5258f538544911147ff873eaafad836b39bd095532ae28c79bbe07071ab62a7b636f4cc000a5748f69f0310c
7
- data.tar.gz: 5c0e6bd0b258619346e12753beb23a15ea830f884d19eee585a137b692ac1309575dfc2c9e7f7e041794c3044fae8ab4facf597e29fc2ab2e6f712cc908f72d8
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!
@@ -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
@@ -1,3 +1,3 @@
1
1
  module PolyBelongsTo
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  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
Binary file