poly_belongs_to 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +61 -27
- data/lib/poly_belongs_to/version.rb +1 -1
- data/lib/poly_belongs_to.rb +29 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +5 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 429c2eee4201eee8c86e3272ead316fd2c5babfe
|
4
|
+
data.tar.gz: a471110519e11c8ac2f0ea33f058a71231353379
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8fd4f92b85e7092c5a5287a47206bc0a1deeb4368453e135acab5f9280983ecae04c80911f1d6b94217c94488241a9fe5deb85297967a4bbf80c7a229dbf7328
|
7
|
+
data.tar.gz: 2289fb23dd61bc46beef6b989a3408d402169eaf8887a5bac80d04206a401f0951ec2233eaa1dc367ced7b1c1ba5149508784e9f6c21692fe50aec2212f5fa84
|
data/README.md
CHANGED
@@ -1,55 +1,89 @@
|
|
1
|
-
#
|
1
|
+
#PolyBelongsTo
|
2
|
+
[](http://badge.fury.io/rb/poly_belongs_to)
|
2
3
|
[](https://codeclimate.com/github/danielpclark/PolyBelongsTo)
|
3
4
|
|
4
5
|
Is an ActiveRecord library which will let you check your DB Objects polymorphism in a more across-the-board
|
5
6
|
meta-programatically friendly way.
|
6
7
|
|
8
|
+
#Installation
|
9
|
+
|
10
|
+
### Gem
|
11
|
+
|
12
|
+
Just include it in your Gemfile and then run bundle:
|
13
|
+
```ruby
|
14
|
+
gem 'poly_belongs_to'
|
15
|
+
```
|
16
|
+
|
17
|
+
###Merge via git
|
18
|
+
Be in your Rails project directory. Make sure you git is up to date with all your latest changes. Then:
|
19
|
+
|
20
|
+
```shell
|
21
|
+
git fetch git@github.com:danielpclark/PolyBelongsTo.git install:pbt
|
22
|
+
git merge pbt
|
23
|
+
```
|
24
|
+
|
25
|
+
And then enter a description for this merge into your project. Save the message, exit, and you're done!
|
26
|
+
|
27
|
+
##Example Usage
|
28
|
+
|
7
29
|
```ruby
|
8
30
|
# Is Polymorphic?
|
9
31
|
MyObject.new.poly?
|
10
32
|
# => true
|
11
|
-
MyOject.poly?
|
33
|
+
MyOject.poly? # Recommended usage on class name
|
12
34
|
# => true
|
35
|
+
User.first.poly?
|
36
|
+
# => false
|
37
|
+
User.poly? # Recommended usage on class name
|
38
|
+
# => false
|
13
39
|
|
14
40
|
# Polymorphic Belongs To Relation Table
|
15
41
|
MyObject.new.pbt
|
16
42
|
# => :my_objectable
|
17
|
-
MyObject.pbt
|
43
|
+
MyObject.pbt # Recommended usage on class name
|
18
44
|
# => :my_objectable
|
19
|
-
|
45
|
+
User.first.pbt
|
46
|
+
# => nil
|
47
|
+
User.pbt # Recommended usage on class name
|
48
|
+
# => nil
|
49
|
+
|
50
|
+
# Params name
|
51
|
+
MyObject.new.pbt_params_name
|
52
|
+
# => :my_objectable_attributes
|
53
|
+
MyObject.pbt_params_name # Recommended usage on class name
|
54
|
+
# => :my_objectable_attributes
|
55
|
+
User.first.pbt_params_name
|
56
|
+
# => :user
|
57
|
+
User.pbt_params_name # Recommended usage on class name
|
58
|
+
# => :user
|
59
|
+
|
60
|
+
# Polymorphic DB field names
|
61
|
+
MyObject.new.pbt_id_sym
|
62
|
+
# => :my_objectable_id
|
63
|
+
MyObject.pbt_id_sym # Recommended usage on class name
|
64
|
+
# => :my_objectable_id
|
65
|
+
MyObject.new.pbt_type_sym
|
66
|
+
# => :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
|
70
|
+
|
20
71
|
# Polymorphic Belongs To Relations ID
|
21
|
-
MyObject.first.pbt_id
|
72
|
+
MyObject.first.pbt_id # Retrieve instance value
|
22
73
|
# => 123
|
74
|
+
# The above method returns nil for non polymorphic Objects
|
23
75
|
|
24
76
|
# Polymorphic Belongs To Relations Type
|
25
|
-
MyObject.first.pbt_type
|
77
|
+
MyObject.first.pbt_type # Retrieve instance value
|
26
78
|
"User"
|
79
|
+
# The above method returns nil for non polymorphic Objects
|
27
80
|
```
|
28
81
|
|
29
82
|
And that's that!
|
30
83
|
|
31
|
-
|
32
|
-
|
33
|
-
### Gem
|
34
|
-
Install the gem via your command line:
|
35
|
-
```shell
|
36
|
-
gem install poly_belongs_to
|
37
|
-
```
|
38
|
-
|
39
|
-
Or just include it in your Gemfile and then run bundle:
|
40
|
-
```ruby
|
41
|
-
gem 'poly_belongs_to'
|
42
|
-
```
|
43
|
-
|
44
|
-
###Merge via git
|
45
|
-
Be in your Rails project directory. Make sure you git is up to date with all your latest changes. Then:
|
46
|
-
|
47
|
-
```shell
|
48
|
-
git fetch git@github.com:danielpclark/PolyBelongsTo.git install:pbt
|
49
|
-
git merge pbt
|
50
|
-
```
|
84
|
+
##TODO
|
51
85
|
|
52
|
-
|
86
|
+
Basic specs
|
53
87
|
|
54
88
|
#License
|
55
89
|
|
data/lib/poly_belongs_to.rb
CHANGED
@@ -16,6 +16,24 @@ module PolyBelongsTo
|
|
16
16
|
def self.poly?
|
17
17
|
!!reflect_on_all_associations(:belongs_to).first.try {|i| i.options[:polymorphic] }
|
18
18
|
end
|
19
|
+
|
20
|
+
def self.pbt_params_name
|
21
|
+
if poly?
|
22
|
+
"#{table_name}_attributes".to_sym
|
23
|
+
else
|
24
|
+
name.downcase.to_sym
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.pbt_id_sym
|
29
|
+
val = pbt
|
30
|
+
val ? "#{pbt}_id".to_sym : nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.pbt_type_sym
|
34
|
+
val = pbt
|
35
|
+
val ? "#{pbt}_type".to_sym : nil
|
36
|
+
end
|
19
37
|
end
|
20
38
|
|
21
39
|
def pbt
|
@@ -36,6 +54,17 @@ module PolyBelongsTo
|
|
36
54
|
val ? eval("self.#{val}_type") : nil
|
37
55
|
end
|
38
56
|
|
57
|
+
def pbt_id_sym
|
58
|
+
self.class.pbt_id_sym
|
59
|
+
end
|
60
|
+
|
61
|
+
def pbt_type_sym
|
62
|
+
self.class.pbt_type_sym
|
63
|
+
end
|
64
|
+
|
65
|
+
def pbt_params_name
|
66
|
+
self.class.pbt_params_name
|
67
|
+
end
|
39
68
|
end
|
40
69
|
|
41
70
|
ActiveRecord::Base.send(:include, PolyBelongsTo)
|
File without changes
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: poly_belongs_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel P. Clark
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -88,6 +88,8 @@ files:
|
|
88
88
|
- test/dummy/config/locales/en.yml
|
89
89
|
- test/dummy/config/routes.rb
|
90
90
|
- test/dummy/config/secrets.yml
|
91
|
+
- test/dummy/db/test.sqlite3
|
92
|
+
- test/dummy/log/test.log
|
91
93
|
- test/dummy/public/404.html
|
92
94
|
- test/dummy/public/422.html
|
93
95
|
- test/dummy/public/500.html
|
@@ -121,6 +123,7 @@ summary: Shorthand polymorphic testing and universal identifiers.
|
|
121
123
|
test_files:
|
122
124
|
- test/poly_belongs_to_test.rb
|
123
125
|
- test/test_helper.rb
|
126
|
+
- test/dummy/db/test.sqlite3
|
124
127
|
- test/dummy/config/initializers/mime_types.rb
|
125
128
|
- test/dummy/config/initializers/session_store.rb
|
126
129
|
- test/dummy/config/initializers/assets.rb
|
@@ -151,6 +154,7 @@ test_files:
|
|
151
154
|
- test/dummy/public/500.html
|
152
155
|
- test/dummy/Rakefile
|
153
156
|
- test/dummy/config.ru
|
157
|
+
- test/dummy/log/test.log
|
154
158
|
- test/dummy/bin/rake
|
155
159
|
- test/dummy/bin/rails
|
156
160
|
- test/dummy/bin/bundle
|