poly_belongs_to 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -0
- data/lib/poly_belongs_to/dup.rb +7 -23
- data/lib/poly_belongs_to/faked_collection.rb +64 -0
- data/lib/poly_belongs_to/poly_belongs_to.rb +10 -0
- data/lib/poly_belongs_to/version.rb +1 -1
- data/lib/poly_belongs_to.rb +1 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +28311 -0
- data/test/dup_test.rb +0 -2
- data/test/faked_collection_test.rb +75 -0
- data/test/pbt_test.rb +33 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6db0bb5452d0a17b0a99a45f875cd30532ade12a
|
4
|
+
data.tar.gz: 976a70a95737ad12583cde8401dc30411b85c1a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d49e5970ec7375eddfc93752569b611c11b741c1caf9181da5e58ac91dea9a702cf28c167d2b87be4347863636d565553983584add6bd9ac21e2f263f782040
|
7
|
+
data.tar.gz: cde4c040599f9cd89c41ed96141d1ed811a8420996cf01e812b31b4ad7ced1d886a9aa6e4867799278d514a85e90d66131b7ea6408a97c748cbb7659ccfab100
|
data/README.md
CHANGED
@@ -119,6 +119,11 @@ PolyBelongsTo::Pbt::IsPlural[ obj, child ]
|
|
119
119
|
# For has_one it's the object ref itself.
|
120
120
|
PolyBelongsTo::Pbt::CollectionProxy[ obj, child ]
|
121
121
|
|
122
|
+
# Always returns a collection proxy; fakes a collection proxy for has_one.
|
123
|
+
PolyBelongsTo::Pbt::AsCollectionProxy[ obj, child ]
|
124
|
+
|
125
|
+
# Wrapper for has_one objects to be a collection proxy
|
126
|
+
PolyBelongsTo::FakedCollection.new(obj, child)
|
122
127
|
```
|
123
128
|
##Record Duplication
|
124
129
|
|
data/lib/poly_belongs_to/dup.rb
CHANGED
@@ -5,10 +5,8 @@ module PolyBelongsTo
|
|
5
5
|
included do
|
6
6
|
def self.pbt_dup_build(item_to_build_on, item_to_duplicate)
|
7
7
|
if PolyBelongsTo::Pbt::IsReflected[item_to_build_on, item_to_duplicate]
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
build_cmd ? eval("item_to_build_on.#{build_cmd}(#{dup_attrs})") : nil
|
8
|
+
PolyBelongsTo::Pbt::AsCollectionProxy[item_to_build_on, item_to_duplicate].
|
9
|
+
build PolyBelongsTo::Pbt::AttrSanitizer[item_to_duplicate]
|
12
10
|
end
|
13
11
|
end
|
14
12
|
|
@@ -16,26 +14,12 @@ module PolyBelongsTo
|
|
16
14
|
pbt_dup_build(item_to_build_on, item_to_duplicate)
|
17
15
|
PolyBelongsTo::Pbt::Reflects[item_to_duplicate].each do |ref|
|
18
16
|
child = eval("item_to_duplicate.#{ref}")
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
subscore.pbt_deep_dup_build(spawn)
|
25
|
-
end
|
26
|
-
else
|
27
|
-
core.pbt_deep_dup_build(spawn)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
else
|
31
|
-
if core.respond_to?(:build)
|
32
|
-
core.each do |subcore|
|
33
|
-
subcore.pbt_deep_dup_build(child)
|
34
|
-
end
|
35
|
-
else
|
36
|
-
core.pbt_deep_dup_build(child)
|
17
|
+
PolyBelongsTo::Pbt::AsCollectionProxy[item_to_build_on, item_to_duplicate].
|
18
|
+
each do |builder|
|
19
|
+
child.respond_to?(:build) ? child.each {|spawn|
|
20
|
+
builder.pbt_deep_dup_build(spawn)
|
21
|
+
} : builder.pbt_deep_dup_build(child)
|
37
22
|
end
|
38
|
-
end
|
39
23
|
end
|
40
24
|
item_to_build_on
|
41
25
|
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module PolyBelongsTo
|
2
|
+
class FakedCollection
|
3
|
+
def initialize(obj, child)
|
4
|
+
raise "Not a has_one rleationship for FakedCollection" unless PolyBelongsTo::Pbt::IsSingular[obj,child]
|
5
|
+
@obj = obj
|
6
|
+
@child = child
|
7
|
+
@instance = eval("@obj.#{PolyBelongsTo::Pbt::CollectionProxy[@obj,@child]}")
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def all
|
12
|
+
Array[@instance].compact
|
13
|
+
end
|
14
|
+
|
15
|
+
def first
|
16
|
+
@instance
|
17
|
+
end
|
18
|
+
|
19
|
+
def last
|
20
|
+
@instance
|
21
|
+
end
|
22
|
+
|
23
|
+
def size
|
24
|
+
all.size
|
25
|
+
end
|
26
|
+
|
27
|
+
def ancestors
|
28
|
+
klass.ancestors.unshift(PolyBelongsTo::FakedCollection)
|
29
|
+
end
|
30
|
+
|
31
|
+
def kind_of?(thing)
|
32
|
+
ancestors.include? thing
|
33
|
+
end
|
34
|
+
|
35
|
+
def count
|
36
|
+
size
|
37
|
+
end
|
38
|
+
|
39
|
+
def klass
|
40
|
+
@instance.class
|
41
|
+
end
|
42
|
+
|
43
|
+
def build(*args)
|
44
|
+
@instance = eval("@obj.#{PolyBelongsTo::Pbt::BuildCmd[@obj, @child]}(#{args.join(',')})")
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def each(*args, &block)
|
49
|
+
all.each(*args, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
def respond_to?(method_name)
|
53
|
+
@instance.respond_to?(method_name) || super
|
54
|
+
end
|
55
|
+
|
56
|
+
def method_missing(method_name, *args, &block)
|
57
|
+
if @instance.respond_to?(method_name)
|
58
|
+
@instance.send method_name, *args, &block
|
59
|
+
else
|
60
|
+
super
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -58,5 +58,15 @@ module PolyBelongsTo
|
|
58
58
|
proxy = ActiveModel::Naming.plural(child).to_sym
|
59
59
|
reflects.include?(proxy) ? proxy : nil
|
60
60
|
}
|
61
|
+
|
62
|
+
AsCollectionProxy = lambda {|obj, child|
|
63
|
+
return [] unless obj && child
|
64
|
+
reflects = Reflects[obj]
|
65
|
+
proxy = ActiveModel::Naming.singular(child).to_sym
|
66
|
+
return PolyBelongsTo::FakedCollection.new(obj, child) if reflects.include? proxy
|
67
|
+
proxy = ActiveModel::Naming.plural(child).to_sym
|
68
|
+
reflects.include?(proxy) ? eval("obj.#{PolyBelongsTo::Pbt::CollectionProxy[obj, child]}") : []
|
69
|
+
}
|
61
70
|
end
|
71
|
+
|
62
72
|
end
|
data/lib/poly_belongs_to.rb
CHANGED
@@ -5,6 +5,7 @@ $: << File.join(File.dirname(__FILE__), "/poly_belongs_to")
|
|
5
5
|
require 'poly_belongs_to/version'
|
6
6
|
require 'poly_belongs_to/dup'
|
7
7
|
require 'poly_belongs_to/poly_belongs_to'
|
8
|
+
require 'poly_belongs_to/faked_collection'
|
8
9
|
require 'active_support/concern'
|
9
10
|
|
10
11
|
module PolyBelongsTo
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|