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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 429c2eee4201eee8c86e3272ead316fd2c5babfe
4
- data.tar.gz: a471110519e11c8ac2f0ea33f058a71231353379
3
+ metadata.gz: 2fe03acf3ce4cdf6ed8409ecaedcca10bea5b010
4
+ data.tar.gz: ade1466c5b6ec5482a13548b1a8ecb8d133e7f19
5
5
  SHA512:
6
- metadata.gz: 8fd4f92b85e7092c5a5287a47206bc0a1deeb4368453e135acab5f9280983ecae04c80911f1d6b94217c94488241a9fe5deb85297967a4bbf80c7a229dbf7328
7
- data.tar.gz: 2289fb23dd61bc46beef6b989a3408d402169eaf8887a5bac80d04206a401f0951ec2233eaa1dc367ced7b1c1ba5149508784e9f6c21692fe50aec2212f5fa84
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
- ##Example Usage
27
+ ##Recommended Usage
28
28
 
29
29
  ```ruby
30
+ # --- On Model Class ---
31
+
30
32
  # Is Polymorphic?
31
- MyObject.new.poly?
33
+ MyOject.poly?
32
34
  # => true
33
- MyOject.poly? # Recommended usage on class name
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
- User.poly? # Recommended usage on class name
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
- MyObject.pbt_type_sym # Recommended usage on class name
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
 
@@ -27,12 +27,11 @@ module PolyBelongsTo
27
27
 
28
28
  def self.pbt_id_sym
29
29
  val = pbt
30
- val ? "#{pbt}_id".to_sym : nil
30
+ val ? "#{val}_id".to_sym : nil
31
31
  end
32
32
 
33
33
  def self.pbt_type_sym
34
- val = pbt
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
- val ? eval("self.#{val}_type") : nil
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
@@ -1,3 +1,3 @@
1
1
  module PolyBelongsTo
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poly_belongs_to
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel P. Clark