poly_belongs_to 0.1.1 → 0.1.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 +51 -31
- data/lib/poly_belongs_to.rb +15 -4
- data/lib/poly_belongs_to/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2fe03acf3ce4cdf6ed8409ecaedcca10bea5b010
|
4
|
+
data.tar.gz: ade1466c5b6ec5482a13548b1a8ecb8d133e7f19
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4bdb66ab09855e35233159048a98d2b1d1d0b7e8ae5cd8f00b7af6b754079e0f7f6da938c9b048b31f87edc8c8f130f34c1b252dc16c7934df71a285d162090b
|
7
|
+
data.tar.gz: 085cbf59ce738ffe8e8a26a9125b2e1baf7075fe354b6a58634ad11868adfc33dcea5e8a84c9a5b2f0f44a74d2fef3248ed990e976d679c2f39183021227f3ea
|
data/README.md
CHANGED
@@ -24,60 +24,80 @@ git merge pbt
|
|
24
24
|
|
25
25
|
And then enter a description for this merge into your project. Save the message, exit, and you're done!
|
26
26
|
|
27
|
-
##
|
27
|
+
##Recommended Usage
|
28
28
|
|
29
29
|
```ruby
|
30
|
+
# --- On Model Class ---
|
31
|
+
|
30
32
|
# Is Polymorphic?
|
31
|
-
|
33
|
+
MyOject.poly?
|
32
34
|
# => true
|
33
|
-
|
35
|
+
User.poly?
|
36
|
+
# => false
|
37
|
+
|
38
|
+
# Polymorphic Belongs To Relation Table
|
39
|
+
MyObject.pbt
|
40
|
+
# => :my_objectable
|
41
|
+
User.pbt
|
42
|
+
# => nil
|
43
|
+
|
44
|
+
# Params name
|
45
|
+
MyObject.pbt_params_name
|
46
|
+
# => :my_objectable_attributes
|
47
|
+
User.pbt_params_name
|
48
|
+
# => :user
|
49
|
+
|
50
|
+
# Polymorphic DB field names
|
51
|
+
MyObject.pbt_id_sym
|
52
|
+
# => :my_objectable_id
|
53
|
+
MyObject.pbt_type_sym
|
54
|
+
# => :my_objectable_type
|
55
|
+
|
56
|
+
# --- On Model Instances ---
|
57
|
+
|
58
|
+
# Polymorphic Belongs To Relations ID
|
59
|
+
MyObject.first.pbt_id
|
60
|
+
# => 123 # nil for non polymorphic Objects
|
61
|
+
|
62
|
+
# Polymorphic Belongs To Relations Type
|
63
|
+
MyObject.first.pbt_type
|
64
|
+
"User" # nil for non polymorphic Objects
|
65
|
+
|
66
|
+
# Get Parent Object (Works on all belongs_to Objects)
|
67
|
+
MyObject.first.pbt_parent
|
68
|
+
# => #<User id: 1 ... >
|
69
|
+
```
|
70
|
+
|
71
|
+
##Also Availabe
|
72
|
+
```ruby
|
73
|
+
# --- Model Instances ---
|
74
|
+
# NOTE: touches db if object isn't already instantiated
|
75
|
+
|
76
|
+
# Is Polymorphic?
|
77
|
+
MyObject.new.poly?
|
34
78
|
# => true
|
35
79
|
User.first.poly?
|
36
80
|
# => false
|
37
|
-
|
38
|
-
# => false
|
39
|
-
|
81
|
+
|
40
82
|
# Polymorphic Belongs To Relation Table
|
41
83
|
MyObject.new.pbt
|
42
84
|
# => :my_objectable
|
43
|
-
MyObject.pbt # Recommended usage on class name
|
44
|
-
# => :my_objectable
|
45
85
|
User.first.pbt
|
46
86
|
# => nil
|
47
|
-
User.pbt # Recommended usage on class name
|
48
|
-
# => nil
|
49
87
|
|
50
88
|
# Params name
|
51
89
|
MyObject.new.pbt_params_name
|
52
90
|
# => :my_objectable_attributes
|
53
|
-
MyObject.pbt_params_name # Recommended usage on class name
|
54
|
-
# => :my_objectable_attributes
|
55
91
|
User.first.pbt_params_name
|
56
92
|
# => :user
|
57
|
-
User.pbt_params_name # Recommended usage on class name
|
58
|
-
# => :user
|
59
93
|
|
60
94
|
# Polymorphic DB field names
|
61
|
-
MyObject.new.pbt_id_sym
|
62
|
-
# => :my_objectable_id
|
63
|
-
MyObject.pbt_id_sym # Recommended usage on class name
|
95
|
+
MyObject.new.pbt_id_sym # nil for non polymorphic Objects
|
64
96
|
# => :my_objectable_id
|
65
|
-
MyObject.new.pbt_type_sym
|
97
|
+
MyObject.new.pbt_type_sym # nil for non polymorphic Objects
|
66
98
|
# => :my_objectable_type
|
67
|
-
|
68
|
-
# => :my_objectable_type
|
69
|
-
# The above methods return nil for non polymorphic Objects
|
99
|
+
```
|
70
100
|
|
71
|
-
# Polymorphic Belongs To Relations ID
|
72
|
-
MyObject.first.pbt_id # Retrieve instance value
|
73
|
-
# => 123
|
74
|
-
# The above method returns nil for non polymorphic Objects
|
75
|
-
|
76
|
-
# Polymorphic Belongs To Relations Type
|
77
|
-
MyObject.first.pbt_type # Retrieve instance value
|
78
|
-
"User"
|
79
|
-
# The above method returns nil for non polymorphic Objects
|
80
|
-
```
|
81
101
|
|
82
102
|
And that's that!
|
83
103
|
|
data/lib/poly_belongs_to.rb
CHANGED
@@ -27,12 +27,11 @@ module PolyBelongsTo
|
|
27
27
|
|
28
28
|
def self.pbt_id_sym
|
29
29
|
val = pbt
|
30
|
-
val ? "#{
|
30
|
+
val ? "#{val}_id".to_sym : nil
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.pbt_type_sym
|
34
|
-
|
35
|
-
val ? "#{pbt}_type".to_sym : nil
|
34
|
+
poly? ? "#{pbt}_type".to_sym : nil
|
36
35
|
end
|
37
36
|
end
|
38
37
|
|
@@ -50,8 +49,20 @@ module PolyBelongsTo
|
|
50
49
|
end
|
51
50
|
|
52
51
|
def pbt_type
|
52
|
+
poly? ? eval("self.#{pbt}_type") : nil
|
53
|
+
end
|
54
|
+
|
55
|
+
def pbt_parent
|
53
56
|
val = pbt
|
54
|
-
|
57
|
+
if pbt
|
58
|
+
if poly?
|
59
|
+
eval "#{pbt_type}.find(#{pbt_id})"
|
60
|
+
else
|
61
|
+
eval "#{val.capitalize.to_s}.find(#{pbt_id})"
|
62
|
+
end
|
63
|
+
else
|
64
|
+
nil
|
65
|
+
end
|
55
66
|
end
|
56
67
|
|
57
68
|
def pbt_id_sym
|