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 +4 -4
- data/README.md +4 -0
- data/lib/poly_belongs_to/core.rb +17 -0
- data/lib/poly_belongs_to/version.rb +1 -1
- data/test/core_test.rb +14 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +6257 -0
- data/test/pbt_test.rb +6 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de25b801e7764ce33e370776d68fc4ca06971f5f
|
4
|
+
data.tar.gz: 941c8126368dc1b1f0b9c3242ddbcea9d70bee18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/poly_belongs_to/core.rb
CHANGED
@@ -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
|
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
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|