poly_belongs_to 0.1.4 → 0.1.5

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: 295e7bd5fc23bc91323e6bf9d8650e1e4b6f59ab
4
- data.tar.gz: 9cfbad7a6779856a5950b7410737ee524b8ef29c
3
+ metadata.gz: 070f2956135ec77c1f784a3291f8aa3d0af97a4e
4
+ data.tar.gz: 6e8a8b16efb10063d55460b609d1239bc7f956cb
5
5
  SHA512:
6
- metadata.gz: b3421897cbce5956b73d3ff9339c2ec3755d36ed03c830eef435dce662e83f2ece03604b9bfa50357023267c66d7fb82dba76a0ac7845712dcaa76a1b8634b93
7
- data.tar.gz: babae7be384227bb9ee4b181f08598d97b3963d926bae0da03a80d89ccb2758fccfbabdd07a1ca95b223a2c7bdb11e2a1cc705d7828fb0f9e1f1e2ad645a17c8
6
+ metadata.gz: c74a9a9dd7082aa50375bb07fc488ecf5b7c02513f5a29c7aec2d3690aaa3d19551b85bf60b1b0a4d3b9fe16fa05d610584d5470e7c5456c4314b281c57e6bd0
7
+ data.tar.gz: a86708d178b640ab44c96514c192b4f335d0ad1c755c9d601105cff1de6ea1c101a04ff038ad59b438e27a3ee7d40db580c88b08102e1778c36f152ab356e4b6
data/README.md CHANGED
@@ -1,7 +1,7 @@
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)
4
3
  [![Build Status](https://travis-ci.org/danielpclark/PolyBelongsTo.svg)](https://travis-ci.org/danielpclark/PolyBelongsTo)
4
+ [![Test Coverage](https://codeclimate.com/github/danielpclark/PolyBelongsTo/badges/coverage.svg)](https://codeclimate.com/github/danielpclark/PolyBelongsTo)
5
5
 
6
6
  A standard way to check belongs_to relations on any belongs_to Object and let you check your DB Objects polymorphism in a more across-the-board meta-programatically friendly way.
7
7
 
@@ -57,7 +57,7 @@ MyObject.first.pbt_parent
57
57
  # => #<User id: 123 ... >
58
58
  ```
59
59
 
60
- ##Also Availabe
60
+ ##Also Available
61
61
  ```ruby
62
62
  # --- Model Instances ---
63
63
  # NOTE: touches db if object isn't already instantiated
@@ -90,7 +90,7 @@ MyObject.new.pbt_type_sym # nil for non polymorphic Objects
90
90
  ##Internal Methods Available
91
91
 
92
92
  ```ruby
93
- # For cleaning attributs for use with build
93
+ # For cleaning attributes for use with build
94
94
  PolyBelongsTo::Pbt::AttrSanitizer[ obj ]
95
95
 
96
96
  # Returns string of either 'child.build' or 'build_child'
@@ -114,7 +114,9 @@ PolyBelongsTo::Pbt::IsSingular[ obj, child ]
114
114
  # Returns true if obj->child relationship is has_many
115
115
  PolyBelongsTo::Pbt::IsPlural[ obj, child ]
116
116
 
117
- # Returns the simbol for the CollectionProxy the child belongs to in relation to obj
117
+ # 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.
118
120
  PolyBelongsTo::Pbt::CollectionProxy[ obj, child ]
119
121
 
120
122
  ```
@@ -130,8 +132,9 @@ know what's involved when using this. It's purposefully done this way to lead t
130
132
  the documentation for PolyBelongsTo's duplication methods.
131
133
 
132
134
  ####Known Issues
133
- - Carrierwave records won't duplicate. To ensure other records to still save and prevent
134
- rollback use .save(validate: false) ... I'm considering possible options to remedy this and
135
+ - Carrierwave records won't duplicate. To ensure that other records will still save and
136
+ prevent any rollback issues use .save(validate: false) ... I'm considering possible options
137
+ to remedy this and
135
138
  other scenarios.
136
139
  - For deep duplication you need to be very aware of the potential for infinite loops with
137
140
  your records if there are any circular references.
@@ -16,14 +16,25 @@ module PolyBelongsTo
16
16
  pbt_dup_build(item_to_build_on, item_to_duplicate)
17
17
  PolyBelongsTo::Pbt::Reflects[item_to_duplicate].each do |ref|
18
18
  child = eval("item_to_duplicate.#{ref}")
19
+ core = eval("item_to_build_on.#{PolyBelongsTo::Pbt::CollectionProxy[item_to_build_on, item_to_duplicate]}")
19
20
  if child.respond_to?(:build)
20
- child.each do |obj|
21
- eval("item_to_build_on.#{PolyBelongsTo::Pbt::CollectionProxy[item_to_build_on, item_to_duplicate]}").
22
- pbt_deep_dup_build(obj)
21
+ child.each do |spawn|
22
+ if core.respond_to?(:build)
23
+ core.each do |subscore|
24
+ subscore.pbt_deep_dup_build(spawn)
25
+ end
26
+ else
27
+ core.pbt_deep_dup_build(spawn)
28
+ end
23
29
  end
24
30
  else
25
- eval("item_to_build_on.#{PolyBelongsTo::Pbt::CollectionProxy[item_to_build_on, item_to_duplicate]}").
26
- pbt_deep_dup_build(child)
31
+ if core.respond_to?(:build)
32
+ core.each do |subcore|
33
+ subcore.pbt_deep_dup_build(child)
34
+ end
35
+ else
36
+ core.pbt_deep_dup_build(child)
37
+ end
27
38
  end
28
39
  end
29
40
  item_to_build_on
@@ -40,27 +40,19 @@ module PolyBelongsTo
40
40
  }
41
41
 
42
42
  IsSingular = lambda {|obj, child|
43
- eval(obj.class.name).reflect_on_all_associations(:has_one).
44
- map(&:name).map(&:to_sym).include? ActiveModel::Naming.singular(child).to_sym
43
+ SingularOrPlural[obj, child] == :singular
45
44
  }
46
45
 
47
46
  IsPlural = lambda {|obj,child|
48
- eval(obj.class.name).reflect_on_all_associations(:has_many).
49
- map(&:name).map(&:to_sym).include? ActiveModel::Naming.plural(child).to_sym
47
+ SingularOrPlural[obj, child] == :plural
50
48
  }
51
49
 
52
50
  CollectionProxy = lambda {|obj, child|
53
51
  reflects = Reflects[obj]
54
52
  proxy = ActiveModel::Naming.singular(child).to_sym
55
- if reflects.include? proxy
56
- return proxy
57
- end
53
+ return proxy if reflects.include? proxy
58
54
  proxy = ActiveModel::Naming.plural(child).to_sym
59
- if reflects.include? proxy
60
- proxy
61
- else
62
- nil
63
- end
55
+ reflects.include?(proxy) ? proxy : nil
64
56
  }
65
57
  end
66
58
  end
@@ -1,3 +1,3 @@
1
1
  module PolyBelongsTo
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
@@ -4,7 +4,6 @@
4
4
  $: << File.join(File.dirname(__FILE__), "/poly_belongs_to")
5
5
  require 'poly_belongs_to/version'
6
6
  require 'poly_belongs_to/dup'
7
- require 'poly_belongs_to/hierarchy'
8
7
  require 'poly_belongs_to/poly_belongs_to'
9
8
  require 'active_support/concern'
10
9
 
@@ -1,3 +1,5 @@
1
1
  class Address < ActiveRecord::Base
2
2
  belongs_to :addressable, polymorphic: true
3
+ has_one :geo_location
4
+ has_many :squishies, as: :squishable, dependent: :destroy
3
5
  end
@@ -0,0 +1,4 @@
1
+ class GeoLocation < ActiveRecord::Base
2
+ belongs_to :address
3
+ has_many :squishies, as: :squishable, dependent: :destroy
4
+ end
@@ -0,0 +1,3 @@
1
+ class Squishy < ActiveRecord::Base
2
+ belongs_to :squishable, polymorphic: true
3
+ end
@@ -0,0 +1,11 @@
1
+ class CreateGeoLocations < ActiveRecord::Migration
2
+ def change
3
+ create_table :geo_locations do |t|
4
+ t.integer :address_id
5
+ t.string :content
6
+
7
+ t.timestamps
8
+ end
9
+ add_index :geo_locations, :address_id
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class CreateSquishies < ActiveRecord::Migration
2
+ def change
3
+ create_table :squishies do |t|
4
+ t.string :content
5
+ t.references :squishable, polymorphic: true, index: true
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: 20150216092519) do
14
+ ActiveRecord::Schema.define(version: 20150220230146) do
15
15
 
16
16
  create_table "addresses", force: true do |t|
17
17
  t.integer "addressable_id"
@@ -32,6 +32,15 @@ ActiveRecord::Schema.define(version: 20150216092519) do
32
32
 
33
33
  add_index "contacts", ["user_id"], name: "index_contacts_on_user_id"
34
34
 
35
+ create_table "geo_locations", force: true do |t|
36
+ t.integer "address_id"
37
+ t.string "content"
38
+ t.datetime "created_at"
39
+ t.datetime "updated_at"
40
+ end
41
+
42
+ add_index "geo_locations", ["address_id"], name: "index_geo_locations_on_address_id"
43
+
35
44
  create_table "phones", force: true do |t|
36
45
  t.integer "phoneable_id"
37
46
  t.string "phoneable_type"
@@ -62,6 +71,16 @@ ActiveRecord::Schema.define(version: 20150216092519) do
62
71
 
63
72
  add_index "profiles", ["profileable_id", "profileable_type"], name: "index_profiles_on_profileable_id_and_profileable_type"
64
73
 
74
+ create_table "squishies", force: true do |t|
75
+ t.string "content"
76
+ t.integer "squishable_id"
77
+ t.string "squishable_type"
78
+ t.datetime "created_at"
79
+ t.datetime "updated_at"
80
+ end
81
+
82
+ add_index "squishies", ["squishable_id", "squishable_type"], name: "index_squishies_on_squishable_id_and_squishable_type"
83
+
65
84
  create_table "ssns", force: true do |t|
66
85
  t.integer "user_id"
67
86
  t.string "content"
Binary file
@@ -53,3 +53,119 @@ Migrating to CreateSsns (20150216092519)
53
53
  ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
54
54
  User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
55
55
  SQLite3::SQLException: no such table: users: SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
56
+  (26.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
57
+  (15.6ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
58
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
59
+ Migrating to CreateUsers (20150211224139)
60
+  (0.1ms) begin transaction
61
+  (0.8ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
62
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224139"]]
63
+  (13.0ms) commit transaction
64
+ Migrating to CreateTags (20150211224157)
65
+  (0.1ms) begin transaction
66
+  (0.6ms) CREATE TABLE "tags" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
67
+  (0.3ms) CREATE INDEX "index_tags_on_user_id" ON "tags" ("user_id")
68
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224157"]]
69
+  (17.4ms) commit transaction
70
+ Migrating to CreatePhones (20150211224225)
71
+  (0.1ms) begin transaction
72
+  (0.7ms) CREATE TABLE "phones" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "phoneable_id" integer, "phoneable_type" varchar(255), "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
73
+  (0.3ms) CREATE INDEX "index_phones_on_phoneable_id_and_phoneable_type" ON "phones" ("phoneable_id", "phoneable_type")
74
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224225"]]
75
+  (30.0ms) commit transaction
76
+ Migrating to CreateAddresses (20150216092218)
77
+  (0.1ms) begin transaction
78
+  (0.3ms) CREATE TABLE "addresses" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "addressable_id" integer, "addressable_type" varchar(255), "content" varchar(255), "created_at" datetime, "updated_at" datetime) 
79
+  (0.1ms) CREATE INDEX "index_addresses_on_addressable_id_and_addressable_type" ON "addresses" ("addressable_id", "addressable_type")
80
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092218"]]
81
+  (31.0ms) commit transaction
82
+ Migrating to CreateProfiles (20150216092338)
83
+  (0.1ms) begin transaction
84
+  (0.3ms) CREATE TABLE "profiles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "profileable_id" integer, "profileable_type" varchar(255), "content" varchar(255), "created_at" datetime, "updated_at" datetime)
85
+  (0.1ms) CREATE INDEX "index_profiles_on_profileable_id_and_profileable_type" ON "profiles" ("profileable_id", "profileable_type")
86
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092338"]]
87
+  (15.5ms) commit transaction
88
+ Migrating to CreatePhotos (20150216092411)
89
+  (0.1ms) begin transaction
90
+  (0.3ms) CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photoable_id" integer, "photoable_type" varchar(255), "content" varchar(255), "created_at" datetime, "updated_at" datetime) 
91
+  (0.2ms) CREATE INDEX "index_photos_on_photoable_id_and_photoable_type" ON "photos" ("photoable_id", "photoable_type")
92
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092411"]]
93
+  (13.2ms) commit transaction
94
+ Migrating to CreateContacts (20150216092449)
95
+  (0.1ms) begin transaction
96
+  (0.3ms) CREATE TABLE "contacts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "content" varchar(255), "created_at" datetime, "updated_at" datetime)
97
+  (0.1ms) CREATE INDEX "index_contacts_on_user_id" ON "contacts" ("user_id")
98
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092449"]]
99
+  (12.9ms) commit transaction
100
+ Migrating to CreateSsns (20150216092519)
101
+  (0.1ms) begin transaction
102
+  (0.6ms) CREATE TABLE "ssns" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "content" varchar(255), "created_at" datetime, "updated_at" datetime) 
103
+  (0.3ms) CREATE INDEX "index_ssns_on_user_id" ON "ssns" ("user_id")
104
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092519"]]
105
+  (18.5ms) commit transaction
106
+ Migrating to CreateGeoLocations (20150220213422)
107
+  (0.1ms) begin transaction
108
+  (0.4ms) CREATE TABLE "geo_locations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "address_id" integer, "content" varchar(255), "created_at" datetime, "updated_at" datetime)
109
+  (0.4ms) CREATE INDEX "index_geo_locations_on_address_id" ON "geo_locations" ("address_id")
110
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150220213422"]]
111
+  (12.6ms) commit transaction
112
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
113
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
114
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
115
+  (17.9ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
116
+  (25.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
117
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
118
+ Migrating to CreateUsers (20150211224139)
119
+  (0.1ms) begin transaction
120
+  (0.8ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
121
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224139"]]
122
+  (12.1ms) commit transaction
123
+ Migrating to CreateTags (20150211224157)
124
+  (0.1ms) begin transaction
125
+  (0.4ms) CREATE TABLE "tags" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
126
+  (0.3ms) CREATE INDEX "index_tags_on_user_id" ON "tags" ("user_id")
127
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224157"]]
128
+  (12.3ms) commit transaction
129
+ Migrating to CreatePhones (20150211224225)
130
+  (0.1ms) begin transaction
131
+  (0.5ms) CREATE TABLE "phones" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "phoneable_id" integer, "phoneable_type" varchar(255), "content" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
132
+  (0.3ms) CREATE INDEX "index_phones_on_phoneable_id_and_phoneable_type" ON "phones" ("phoneable_id", "phoneable_type")
133
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224225"]]
134
+  (12.1ms) commit transaction
135
+ Migrating to CreateAddresses (20150216092218)
136
+  (0.1ms) begin transaction
137
+  (0.6ms) CREATE TABLE "addresses" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "addressable_id" integer, "addressable_type" varchar(255), "content" varchar(255), "created_at" datetime, "updated_at" datetime) 
138
+  (0.4ms) CREATE INDEX "index_addresses_on_addressable_id_and_addressable_type" ON "addresses" ("addressable_id", "addressable_type")
139
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092218"]]
140
+  (12.5ms) commit transaction
141
+ Migrating to CreateProfiles (20150216092338)
142
+  (0.1ms) begin transaction
143
+  (0.6ms) CREATE TABLE "profiles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "profileable_id" integer, "profileable_type" varchar(255), "content" varchar(255), "created_at" datetime, "updated_at" datetime)
144
+  (0.2ms) CREATE INDEX "index_profiles_on_profileable_id_and_profileable_type" ON "profiles" ("profileable_id", "profileable_type")
145
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092338"]]
146
+  (12.4ms) commit transaction
147
+ Migrating to CreatePhotos (20150216092411)
148
+  (0.1ms) begin transaction
149
+  (0.4ms) CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photoable_id" integer, "photoable_type" varchar(255), "content" varchar(255), "created_at" datetime, "updated_at" datetime) 
150
+  (0.2ms) CREATE INDEX "index_photos_on_photoable_id_and_photoable_type" ON "photos" ("photoable_id", "photoable_type")
151
+ SQL (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092411"]]
152
+  (12.6ms) commit transaction
153
+ Migrating to CreateContacts (20150216092449)
154
+  (0.1ms) begin transaction
155
+  (0.8ms) CREATE TABLE "contacts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "content" varchar(255), "created_at" datetime, "updated_at" datetime)
156
+  (0.4ms) CREATE INDEX "index_contacts_on_user_id" ON "contacts" ("user_id")
157
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092449"]]
158
+  (18.4ms) commit transaction
159
+ Migrating to CreateSsns (20150216092519)
160
+  (0.2ms) begin transaction
161
+  (0.6ms) CREATE TABLE "ssns" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "content" varchar(255), "created_at" datetime, "updated_at" datetime) 
162
+  (0.3ms) CREATE INDEX "index_ssns_on_user_id" ON "ssns" ("user_id")
163
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150216092519"]]
164
+  (12.6ms) commit transaction
165
+ Migrating to CreateGeoLocations (20150220213422)
166
+  (0.1ms) begin transaction
167
+  (0.7ms) CREATE TABLE "geo_locations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "address_id" integer, "content" varchar(255), "created_at" datetime, "updated_at" datetime)
168
+  (0.5ms) CREATE INDEX "index_geo_locations_on_address_id" ON "geo_locations" ("address_id")
169
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150220213422"]]
170
+  (13.0ms) commit transaction
171
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"