poly_belongs_to 0.2.1 → 0.2.2

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: ab96e43c85c346057343a22a9e06b8cf532eef4c
4
- data.tar.gz: af27f3c4ec9facc1577fdace270ebd3bbbb9f9ff
3
+ metadata.gz: de25b801e7764ce33e370776d68fc4ca06971f5f
4
+ data.tar.gz: 941c8126368dc1b1f0b9c3242ddbcea9d70bee18
5
5
  SHA512:
6
- metadata.gz: 4b3fb493356381a13b006f927f2c13c28bde9ae004c0bdff342f7ad5cd224a080e76776d21cb28317e707ca419afba8cf41e3aee66c2357cda753eb4b58746fc
7
- data.tar.gz: 778008ce2888fc121386f93bb71242d33f92048164451dda3ee5db436894235334bfab5c15dcd09c3ea2de15bf2e133199ac8387e272bb133fa318b3e3418bb2
6
+ metadata.gz: 6ae16e08acceadb341463dc9202f0a5880f513f415886f0282f6e5a708ee4136b8e6fc02311c12b8478de32cd4e24558854d3e19d882bc8f2b1c2fe92f1e2d29
7
+ data.tar.gz: c9ffe3f630bf0cf408418dcb75da63f606c0151db66effb3d37b865e03dd4ee7bfe06f4314eacba3764e51cb5333fdab7f5667193fb2d239481d09eafa009f66
data/README.md CHANGED
@@ -36,6 +36,10 @@ User.pbt
36
36
  Tire.pbts
37
37
  # => [:user, :car]
38
38
 
39
+ # Get orphaned objects for records of class type
40
+ MyObject.pbt_orphans
41
+ # => #<ActiveRecord::Relation []> # nil for objects without belongs_to
42
+
39
43
  # Params name
40
44
  MyObject.pbt_params_name
41
45
  # => :my_objectable_attributes
@@ -44,6 +44,23 @@ module PolyBelongsTo
44
44
  def self.pbt_type_sym
45
45
  poly? ? "#{pbt}_type".to_sym : nil
46
46
  end
47
+
48
+ # Return Array of current Class records that are orphaned from parents
49
+ # @return [Object] ActiveRecord orphan objects
50
+ def self.pbt_orphans
51
+ return nil unless self.pbts.present?
52
+ if self.poly?
53
+ # self.where(["#{self.pbt_id_sym} NOT IN (?) AND #{self.pbt_type_sym} = (?)", type.constantize.pluck(:id), type])
54
+ accumalitive = nil
55
+ self.pluck(self.pbt_type_sym).uniq.each do |type|
56
+ arel_part = self.arel_table[self.pbt_id_sym].not_in(type.constantize.pluck(:id)).and(self.arel_table[self.pbt_type_sym].eq(type))
57
+ accumalitive = accumalitive.present? ? accumalitive.or(arel_part) : arel_part
58
+ end
59
+ self.where(accumalitive)
60
+ else
61
+ self.where(["#{self.pbt_id_sym} NOT IN (?)", self.pbt.to_s.capitalize.constantize.pluck(:id)])
62
+ end
63
+ end
47
64
  end
48
65
 
49
66
  # @return [Symbol] first belongs_to relation
@@ -1,4 +1,4 @@
1
1
  module PolyBelongsTo
2
2
  # VERSION follows symantic versioning rules
3
- VERSION = "0.2.1"
3
+ VERSION = "0.2.2"
4
4
  end
data/test/core_test.rb CHANGED
@@ -223,6 +223,19 @@ class CoreTest < ActiveSupport::TestCase
223
223
  parents.size.must_equal 1
224
224
  parents.first.must_equal profiles(:bob_prof)
225
225
  end
226
- end
227
226
 
227
+ it "#pbt_orphans returns all parentless records for current Object" do
228
+ obj = Squishy.create(Squishy.pbt_id_sym => 12345, Squishy.pbt_type_sym => "Phone")
229
+ Squishy.pbt_orphans.to_a.must_equal [obj]
230
+ obj2 = Squishy.create(Squishy.pbt_id_sym => 12345, Squishy.pbt_type_sym => "Address")
231
+ Squishy.pbt_orphans.to_a.sort.must_equal [obj, obj2].sort
232
+ obj3 = Beta.create(Beta.pbt_id_sym => 12345)
233
+ Beta.pbt_orphans.to_a.must_equal [obj3]
234
+ end
235
+
236
+ it "#pbt_orphans returns nil for non parent type records" do
237
+ User.create()
238
+ User.pbt_orphans.must_be_nil
239
+ end
240
+ end
228
241
  end
Binary file