poly_belongs_to 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -9
- data/lib/poly_belongs_to/faked_collection.rb +4 -0
- data/lib/poly_belongs_to/version.rb +1 -1
- data/lib/poly_belongs_to.rb +18 -0
- data/test/dummy/app/models/car.rb +4 -0
- data/test/dummy/app/models/tire.rb +4 -0
- data/test/dummy/app/models/user.rb +2 -0
- data/test/dummy/db/migrate/20150301100658_create_tires.rb +11 -0
- data/test/dummy/db/migrate/20150301100722_create_cars.rb +10 -0
- data/test/dummy/db/schema.rb +21 -1
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/test.log +23983 -0
- data/test/faked_collection_test.rb +17 -1
- data/test/fixtures/cars.yml +6 -0
- data/test/fixtures/tires.yml +22 -0
- data/test/fixtures/users.yml +1 -0
- data/test/poly_belongs_to_test.rb +32 -0
- metadata +14 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fef63bc76533283e3fa03444fb55e430cbfed81
|
4
|
+
data.tar.gz: d19cf96a1ad099257eb1396eb84587cd8e983965
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4f503a3cfc275e89ac94915d2b53fb5f4bba1d2c03fcdfda31da38112da9c211216b6f4505691cae6e5ee01d76df61d4d5b09de9a380026ef4a7fac5f70125a0
|
7
|
+
data.tar.gz: ff310a8b501f3c21133f720eb55f2d66655ac9740370dd2b44e1eab0b27fbce1c946682e7c9a49e297fdb9d5192971ab159159235fed0f60af2b7e90fa512a1b
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
#PolyBelongsTo
|
2
2
|
[![Gem Version](https://badge.fury.io/rb/poly_belongs_to.svg)](http://badge.fury.io/rb/poly_belongs_to)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/danielpclark/PolyBelongsTo/badges/gpa.svg)](https://codeclimate.com/github/danielpclark/PolyBelongsTo)
|
3
4
|
[![Build Status](https://travis-ci.org/danielpclark/PolyBelongsTo.svg)](https://travis-ci.org/danielpclark/PolyBelongsTo)
|
4
5
|
[![Test Coverage](https://codeclimate.com/github/danielpclark/PolyBelongsTo/badges/coverage.svg)](https://codeclimate.com/github/danielpclark/PolyBelongsTo)
|
5
6
|
|
@@ -22,12 +23,16 @@ MyOject.poly?
|
|
22
23
|
User.poly?
|
23
24
|
# => false
|
24
25
|
|
25
|
-
#
|
26
|
+
# Belongs To Relation Table
|
26
27
|
MyObject.pbt
|
27
28
|
# => :my_objectable
|
28
29
|
User.pbt
|
29
30
|
# => nil
|
30
31
|
|
32
|
+
# Multiple Belongs To Relations
|
33
|
+
Tire.pbts
|
34
|
+
# => [:user, :car]
|
35
|
+
|
31
36
|
# Params name
|
32
37
|
MyObject.pbt_params_name
|
33
38
|
# => :my_objectable_attributes
|
@@ -36,15 +41,15 @@ MyObject.pbt_params_name(false)
|
|
36
41
|
User.pbt_params_name
|
37
42
|
# => :user
|
38
43
|
|
39
|
-
#
|
44
|
+
# DB column names
|
40
45
|
MyObject.pbt_id_sym
|
41
46
|
# => :my_objectable_id
|
42
47
|
MyObject.pbt_type_sym
|
43
|
-
# => :my_objectable_type
|
48
|
+
# => :my_objectable_type # nil for non polymorphic Objects
|
44
49
|
```
|
45
50
|
#####On model instances
|
46
51
|
```ruby
|
47
|
-
#
|
52
|
+
# Belongs To Relations ID
|
48
53
|
MyObject.first.pbt_id
|
49
54
|
# => 123
|
50
55
|
|
@@ -55,6 +60,10 @@ MyObject.first.pbt_type
|
|
55
60
|
# Get Parent Object (Works on all belongs_to Objects)
|
56
61
|
MyObject.first.pbt_parent
|
57
62
|
# => #<User id: 123 ... >
|
63
|
+
|
64
|
+
# Mutliple Parent Objects (List of one item for Polymorphic, full list otherwise.)
|
65
|
+
Tire.first.pbt_parents
|
66
|
+
# => [#<User id: 123 ... >, #<Car id: 234 ... >]
|
58
67
|
```
|
59
68
|
|
60
69
|
##Also Available
|
@@ -68,19 +77,23 @@ MyObject.new.poly?
|
|
68
77
|
User.first.poly?
|
69
78
|
# => false
|
70
79
|
|
71
|
-
#
|
80
|
+
# Belongs To Relation Table
|
72
81
|
MyObject.new.pbt
|
73
82
|
# => :my_objectable
|
74
83
|
User.first.pbt
|
75
84
|
# => nil
|
76
85
|
|
86
|
+
# Multiple Belongs To Relations
|
87
|
+
Tire.first.pbts
|
88
|
+
# => [:user, :car]
|
89
|
+
|
77
90
|
# Params name
|
78
91
|
MyObject.new.pbt_params_name
|
79
92
|
# => :my_objectable_attributes
|
80
93
|
User.first.pbt_params_name
|
81
94
|
# => :user
|
82
95
|
|
83
|
-
#
|
96
|
+
# DB column names
|
84
97
|
MyObject.new.pbt_id_sym
|
85
98
|
# => :my_objectable_id
|
86
99
|
MyObject.new.pbt_type_sym # nil for non polymorphic Objects
|
@@ -115,8 +128,7 @@ PolyBelongsTo::Pbt::IsSingular[ obj, child ]
|
|
115
128
|
PolyBelongsTo::Pbt::IsPlural[ obj, child ]
|
116
129
|
|
117
130
|
# Returns the symbol for the CollectionProxy the child belongs to in relation to obj
|
118
|
-
# NOTE:
|
119
|
-
# For has_one it's the object ref itself.
|
131
|
+
# NOTE: For has_one the symbol is not a CollectionProxy, but the instance
|
120
132
|
PolyBelongsTo::Pbt::CollectionProxy[ obj, child ]
|
121
133
|
|
122
134
|
# Always returns a collection proxy; fakes a collection proxy for has_one.
|
@@ -179,7 +191,10 @@ it will add recognition for circular references.
|
|
179
191
|
##Contributing
|
180
192
|
|
181
193
|
Feel free to fork and make pull requests. Please bring up an issue before a pull
|
182
|
-
request if it's a non-fix change.
|
194
|
+
request if it's a non-fix change. Please add applicable fixtures and tests for
|
195
|
+
any new features/implementations you add.
|
196
|
+
|
197
|
+
Thank You!
|
183
198
|
|
184
199
|
|
185
200
|
#License
|
data/lib/poly_belongs_to.rb
CHANGED
@@ -16,6 +16,10 @@ module PolyBelongsTo
|
|
16
16
|
def self.pbt
|
17
17
|
reflect_on_all_associations(:belongs_to).first.try(:name)
|
18
18
|
end
|
19
|
+
|
20
|
+
def self.pbts
|
21
|
+
reflect_on_all_associations(:belongs_to).map(&:name)
|
22
|
+
end
|
19
23
|
|
20
24
|
def self.poly?
|
21
25
|
!!reflect_on_all_associations(:belongs_to).first.try {|i| i.options[:polymorphic] }
|
@@ -43,6 +47,10 @@ module PolyBelongsTo
|
|
43
47
|
self.class.pbt
|
44
48
|
end
|
45
49
|
|
50
|
+
def pbts
|
51
|
+
self.class.pbts
|
52
|
+
end
|
53
|
+
|
46
54
|
def poly?
|
47
55
|
self.class.poly?
|
48
56
|
end
|
@@ -69,6 +77,16 @@ module PolyBelongsTo
|
|
69
77
|
end
|
70
78
|
end
|
71
79
|
|
80
|
+
def pbt_parents
|
81
|
+
if poly?
|
82
|
+
Array[pbt_parent].compact
|
83
|
+
else
|
84
|
+
self.class.pbts.map {|i|
|
85
|
+
try{ eval("#{i.capitalize}").find eval("self.#{i}_id") }
|
86
|
+
}.compact
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
72
90
|
def pbt_id_sym
|
73
91
|
self.class.pbt_id_sym
|
74
92
|
end
|
data/test/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20150301100722) do
|
15
15
|
|
16
16
|
create_table "addresses", force: true do |t|
|
17
17
|
t.integer "addressable_id"
|
@@ -23,6 +23,15 @@ ActiveRecord::Schema.define(version: 20150220230146) do
|
|
23
23
|
|
24
24
|
add_index "addresses", ["addressable_id", "addressable_type"], name: "index_addresses_on_addressable_id_and_addressable_type"
|
25
25
|
|
26
|
+
create_table "cars", force: true do |t|
|
27
|
+
t.integer "user_id"
|
28
|
+
t.string "content"
|
29
|
+
t.datetime "created_at"
|
30
|
+
t.datetime "updated_at"
|
31
|
+
end
|
32
|
+
|
33
|
+
add_index "cars", ["user_id"], name: "index_cars_on_user_id"
|
34
|
+
|
26
35
|
create_table "contacts", force: true do |t|
|
27
36
|
t.integer "user_id"
|
28
37
|
t.string "content"
|
@@ -99,6 +108,17 @@ ActiveRecord::Schema.define(version: 20150220230146) do
|
|
99
108
|
|
100
109
|
add_index "tags", ["user_id"], name: "index_tags_on_user_id"
|
101
110
|
|
111
|
+
create_table "tires", force: true do |t|
|
112
|
+
t.integer "user_id"
|
113
|
+
t.integer "car_id"
|
114
|
+
t.string "content"
|
115
|
+
t.datetime "created_at"
|
116
|
+
t.datetime "updated_at"
|
117
|
+
end
|
118
|
+
|
119
|
+
add_index "tires", ["car_id"], name: "index_tires_on_car_id"
|
120
|
+
add_index "tires", ["user_id"], name: "index_tires_on_user_id"
|
121
|
+
|
102
122
|
create_table "users", force: true do |t|
|
103
123
|
t.string "content"
|
104
124
|
t.datetime "created_at", null: false
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|