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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6db0bb5452d0a17b0a99a45f875cd30532ade12a
4
- data.tar.gz: 976a70a95737ad12583cde8401dc30411b85c1a8
3
+ metadata.gz: 9fef63bc76533283e3fa03444fb55e430cbfed81
4
+ data.tar.gz: d19cf96a1ad099257eb1396eb84587cd8e983965
5
5
  SHA512:
6
- metadata.gz: 6d49e5970ec7375eddfc93752569b611c11b741c1caf9181da5e58ac91dea9a702cf28c167d2b87be4347863636d565553983584add6bd9ac21e2f263f782040
7
- data.tar.gz: cde4c040599f9cd89c41ed96141d1ed811a8420996cf01e812b31b4ad7ced1d886a9aa6e4867799278d514a85e90d66131b7ea6408a97c748cbb7659ccfab100
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
- # Polymorphic Belongs To Relation Table
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
- # Polymorphic DB field names
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
- # Polymorphic Belongs To Relations ID
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
- # Polymorphic Belongs To Relation Table
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
- # Polymorphic DB field names
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: This returns a collection proxy for has_many.
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. Thank you!
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
@@ -32,6 +32,10 @@ module PolyBelongsTo
32
32
  ancestors.include? thing
33
33
  end
34
34
 
35
+ def is_a?(thing)
36
+ kind_of?(thing)
37
+ end
38
+
35
39
  def count
36
40
  size
37
41
  end
@@ -1,3 +1,3 @@
1
1
  module PolyBelongsTo
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -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
@@ -0,0 +1,4 @@
1
+ class Car < ActiveRecord::Base
2
+ belongs_to :user
3
+ has_many :tires, dependent: :destroy
4
+ end
@@ -0,0 +1,4 @@
1
+ class Tire < ActiveRecord::Base
2
+ belongs_to :user
3
+ belongs_to :car
4
+ end
@@ -3,4 +3,6 @@ class User < ActiveRecord::Base
3
3
  has_many :tags, dependent: :destroy
4
4
  has_many :contacts, dependent: :destroy
5
5
  has_many :profiles, as: :profileable, dependent: :destroy
6
+ has_many :cars, dependent: :destroy
7
+ has_many :tires, dependent: :destroy
6
8
  end
@@ -0,0 +1,11 @@
1
+ class CreateTires < ActiveRecord::Migration
2
+ def change
3
+ create_table :tires do |t|
4
+ t.references :user, index: true
5
+ t.references :car, index: true
6
+ t.string :content
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class CreateCars < ActiveRecord::Migration
2
+ def change
3
+ create_table :cars do |t|
4
+ t.references :user, index: true
5
+ t.string :content
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -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: 20150220230146) do
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
Binary file