poly_belongs_to 0.2.5 → 0.2.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 63f10835bb5c79fc10a255c58bca817c7f58caad
4
- data.tar.gz: 20ac9975f50671b344766aa0c2c078df199cad1f
3
+ metadata.gz: d945fa7f779711426e5f3f4cf5cea436255a2403
4
+ data.tar.gz: 2bfbbeec531d122fa8520523b4b9fae25f4e52a3
5
5
  SHA512:
6
- metadata.gz: 03f1ee0381c43c5f4712f11af56ccbf638ad34d79fa03a25e6a7d642c4e215f6fc6a8ca3028913c92d92c969936ad9016d636e3b12dd21dd3e2043a2f6f50636
7
- data.tar.gz: 63422eb302602eb8630a4dac497694138a6e6a68cedbe231bb190938879557a9c058792a92574f80092729a1275fef928a2589baedf006c0a56250bd628d1928
6
+ metadata.gz: 49341ba09d46570a56bfce7368927931fe3ab5787a412c1983544f30bcec6b74061f1165f6e3d6641bf783077bd66685de7ef50f9d53e81fed464159bc12deae
7
+ data.tar.gz: 710b7e8a888401bdf849969e84e9d7570ae88183f716d0428c02ff9d3574a4b0b7b3a07d9e20122b3e60a89dd0da03e84d751e719b66dbd0eb005bf8daf236bf
@@ -5,7 +5,7 @@ module PolyBelongsTo
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- # @return [Symbol] first belongs_to relation
8
+ # @return [Symbol, nil] first belongs_to relation
9
9
  def self.pbt
10
10
  reflect_on_all_associations(:belongs_to).first.try(:name)
11
11
  end
@@ -84,7 +84,7 @@ module PolyBelongsTo
84
84
 
85
85
  private
86
86
  # Return Array of current Class polymorphic records that are orphaned from parents
87
- # @return [Array<Object>, nil] ActiveRecord orphan objects
87
+ # @return [Array<Object>] ActiveRecord orphan objects
88
88
  def self._pbt_polymorphic_orphans
89
89
  accumalitive = nil
90
90
  pbt_valid_types.each do |type|
@@ -95,7 +95,7 @@ module PolyBelongsTo
95
95
  end
96
96
 
97
97
  # Return Array of current Class nonpolymorphic records that are orphaned from parents
98
- # @return [Array<Object>, nil] ActiveRecord orphan objects
98
+ # @return [Array<Object>] ActiveRecord orphan objects
99
99
  def self._pbt_nonpolymorphic_orphans
100
100
  where(["#{pbt_id_sym} NOT IN (?)", pbt.to_s.capitalize.constantize.pluck(:id)])
101
101
  end
@@ -105,7 +105,7 @@ module PolyBelongsTo
105
105
  end
106
106
  end
107
107
 
108
- # @return [Symbol] first belongs_to relation
108
+ # @return [Symbol, nil] first belongs_to relation
109
109
  def pbt
110
110
  self.class.pbt
111
111
  end
@@ -135,14 +135,14 @@ module PolyBelongsTo
135
135
  end
136
136
 
137
137
  # Get the parent relation. Polymorphic relations are prioritized first.
138
- # @return [Object] ActiveRecord object instasnce
138
+ # @return [Object, nil] ActiveRecord object instasnce
139
139
  def pbt_parent
140
140
  val = pbt
141
141
  if val && !pbt_id.nil?
142
142
  if poly?
143
- "#{pbt_type}".constantize.find(pbt_id)
143
+ "#{pbt_type}".constantize.where(id: pbt_id).first
144
144
  else
145
- "#{val.capitalize}".constantize.find(pbt_id)
145
+ "#{val.capitalize}".constantize.where(id: pbt_id).first
146
146
  end
147
147
  else
148
148
  nil
@@ -151,7 +151,7 @@ module PolyBelongsTo
151
151
 
152
152
  # Climb up each parent object in the hierarchy until the top is reached.
153
153
  # This has a no-repeat safety built in. Polymorphic parents have priority.
154
- # @return [Object] top parent ActiveRecord object instace
154
+ # @return [Object, nil] top parent ActiveRecord object instace
155
155
  def pbt_top_parent
156
156
  record = self
157
157
  return nil unless record.pbt_parent
@@ -193,5 +193,11 @@ module PolyBelongsTo
193
193
  def pbt_params_name(allow_as_nested = true)
194
194
  self.class.pbt_params_name(allow_as_nested)
195
195
  end
196
+
197
+ # Return true or false on whether the record is orphaned
198
+ # @return [Boolean
199
+ def orphan?
200
+ pbts.present? && !pbt_parent.present?
201
+ end
196
202
  end
197
203
  end
@@ -1,4 +1,4 @@
1
1
  module PolyBelongsTo
2
2
  # VERSION follows symantic versioning rules
3
- VERSION = "0.2.5"
3
+ VERSION = "0.2.6"
4
4
  end
data/test/core_test.rb CHANGED
@@ -180,8 +180,13 @@ class CoreTest < ActiveSupport::TestCase
180
180
  end
181
181
 
182
182
  it "#pbt_parent returns nil if no ID is set for parent relationship" do
183
- alpha = Alpha.new; alpha.save
183
+ alpha = Alpha.new; alpha.save
184
+ beta = alpha.betas.build; beta.save
184
185
  alpha.pbt_parent.must_be_nil
186
+ beta.pbt_parent.wont_be_nil
187
+ beta.pbt_parent.must_equal alpha
188
+ squishy = Squishy.create(Squishy.pbt_id_sym => 12345, Squishy.pbt_type_sym => "Address")
189
+ squishy.pbt_parent.must_be_nil
185
190
  end
186
191
 
187
192
  it "#pbt_top_parent climbs up belongs_to hierarchy to the top; polymorphic relationships first" do
@@ -287,5 +292,19 @@ class CoreTest < ActiveSupport::TestCase
287
292
  Squishy.singleton_class.method_defined?(:_pbt_nonpolymorphic_orphans).must_be_same_as false
288
293
  Squishy.method_defined?(:_pbt_nonpolymorphic_orphans).must_be_same_as false
289
294
  end
295
+
296
+ it "#orphan? gives boolean if record is orphaned" do
297
+ Squishy.create(Squishy.pbt_id_sym => 12345, Squishy.pbt_type_sym => "Phone").must_be :orphan?
298
+ Squishy.create(Squishy.pbt_id_sym => 12345, Squishy.pbt_type_sym => "Address").must_be :orphan?
299
+ User.create().wont_be :orphan?
300
+ user = users(:bob)
301
+ profile = user.profiles.build
302
+ phone = profile.phones.build
303
+ profile.save
304
+ profile.wont_be :orphan?
305
+ phone.wont_be :orphan?
306
+ user.wont_be :orphan?
307
+ Alpha.create.must_be :orphan?
308
+ end
290
309
  end
291
310
  end
Binary file