poly_belongs_to 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -12
- data/lib/poly_belongs_to/version.rb +1 -1
- data/lib/poly_belongs_to.rb +4 -4
- data/test/dummy/app/models/phone.rb +3 -0
- data/test/dummy/app/models/tag.rb +3 -0
- data/test/dummy/app/models/user.rb +4 -0
- data/test/dummy/db/migrate/20150211224139_create_users.rb +8 -0
- data/test/dummy/db/migrate/20150211224157_create_tags.rb +10 -0
- data/test/dummy/db/migrate/20150211224225_create_phones.rb +9 -0
- data/test/dummy/db/schema.rb +38 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/dummy/log/development.log +56 -0
- data/test/dummy/log/test.log +2522 -0
- data/test/dummy/test/models/phone_test.rb +7 -0
- data/test/dummy/test/models/tag_test.rb +7 -0
- data/test/dummy/test/models/user_test.rb +7 -0
- data/test/fixtures/phones.yml +5 -0
- data/test/fixtures/tags.yml +4 -0
- data/test/fixtures/users.yml +7 -0
- data/test/poly_belongs_to_test.rb +136 -2
- data/test/test_helper.rb +6 -1
- metadata +88 -7
- data/test/dummy/README.rdoc +0 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7bb57e153d2b318ee6809a57f925e3adda3ecea
|
4
|
+
data.tar.gz: 61110ab7c6b819e37f4c5b82b542f8edb6e123a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2ce49d807729c6e49318be14ee20f106b3f9fd971c3586dcddc064c3d29ada5130ba1cf4e81db5924b396cc9c9273ec70372e95a922956e609e6fb40b3120b7
|
7
|
+
data.tar.gz: d2cd49a7f5a62cbd6f8c2d4de20fcf8675f8a598312bae14d0b565557d8f4690a108b6de7270cefcf212412bc317598afc5828ace0fa71edb013e3db800b5a25
|
data/README.md
CHANGED
@@ -1,6 +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
3
|
[![Code Climate](https://codeclimate.com/github/danielpclark/PolyBelongsTo/badges/gpa.svg)](https://codeclimate.com/github/danielpclark/PolyBelongsTo)
|
4
|
+
[![Build Status](https://travis-ci.org/danielpclark/PolyBelongsTo.svg)](https://travis-ci.org/danielpclark/PolyBelongsTo)
|
4
5
|
|
5
6
|
Is an ActiveRecord library which will let you check your DB Objects polymorphism in a more across-the-board
|
6
7
|
meta-programatically friendly way.
|
@@ -26,9 +27,8 @@ And then enter a description for this merge into your project. Save the message
|
|
26
27
|
|
27
28
|
##Recommended Usage
|
28
29
|
|
30
|
+
#####On model class
|
29
31
|
```ruby
|
30
|
-
# --- On Model Class ---
|
31
|
-
|
32
32
|
# Is Polymorphic?
|
33
33
|
MyOject.poly?
|
34
34
|
# => true
|
@@ -44,6 +44,9 @@ User.pbt
|
|
44
44
|
# Params name
|
45
45
|
MyObject.pbt_params_name
|
46
46
|
# => :my_objectable_attributes
|
47
|
+
MyObject.pbt_params_name(false)
|
48
|
+
# => :my_object
|
49
|
+
User.pbt_params_name
|
47
50
|
User.pbt_params_name
|
48
51
|
# => :user
|
49
52
|
|
@@ -52,12 +55,12 @@ MyObject.pbt_id_sym
|
|
52
55
|
# => :my_objectable_id
|
53
56
|
MyObject.pbt_type_sym
|
54
57
|
# => :my_objectable_type
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
+
```
|
59
|
+
#####On model instances
|
60
|
+
```ruby
|
58
61
|
# Polymorphic Belongs To Relations ID
|
59
62
|
MyObject.first.pbt_id
|
60
|
-
# => 123
|
63
|
+
# => 123
|
61
64
|
|
62
65
|
# Polymorphic Belongs To Relations Type
|
63
66
|
MyObject.first.pbt_type
|
@@ -65,7 +68,7 @@ MyObject.first.pbt_type
|
|
65
68
|
|
66
69
|
# Get Parent Object (Works on all belongs_to Objects)
|
67
70
|
MyObject.first.pbt_parent
|
68
|
-
# => #<User id:
|
71
|
+
# => #<User id: 123 ... >
|
69
72
|
```
|
70
73
|
|
71
74
|
##Also Availabe
|
@@ -92,7 +95,7 @@ User.first.pbt_params_name
|
|
92
95
|
# => :user
|
93
96
|
|
94
97
|
# Polymorphic DB field names
|
95
|
-
MyObject.new.pbt_id_sym
|
98
|
+
MyObject.new.pbt_id_sym
|
96
99
|
# => :my_objectable_id
|
97
100
|
MyObject.new.pbt_type_sym # nil for non polymorphic Objects
|
98
101
|
# => :my_objectable_type
|
@@ -101,10 +104,6 @@ MyObject.new.pbt_type_sym # nil for non polymorphic Objects
|
|
101
104
|
|
102
105
|
And that's that!
|
103
106
|
|
104
|
-
##TODO
|
105
|
-
|
106
|
-
Basic specs
|
107
|
-
|
108
107
|
#License
|
109
108
|
|
110
109
|
The MIT License (MIT)
|
data/lib/poly_belongs_to.rb
CHANGED
@@ -17,9 +17,9 @@ module PolyBelongsTo
|
|
17
17
|
!!reflect_on_all_associations(:belongs_to).first.try {|i| i.options[:polymorphic] }
|
18
18
|
end
|
19
19
|
|
20
|
-
def self.pbt_params_name
|
20
|
+
def self.pbt_params_name(allow_as_nested = true)
|
21
21
|
if poly?
|
22
|
-
"#{table_name}_attributes".to_sym
|
22
|
+
allow_as_nested ? "#{table_name}_attributes".to_sym : name.downcase.to_sym
|
23
23
|
else
|
24
24
|
name.downcase.to_sym
|
25
25
|
end
|
@@ -73,8 +73,8 @@ module PolyBelongsTo
|
|
73
73
|
self.class.pbt_type_sym
|
74
74
|
end
|
75
75
|
|
76
|
-
def pbt_params_name
|
77
|
-
self.class.pbt_params_name
|
76
|
+
def pbt_params_name(allow_as_nested = true)
|
77
|
+
self.class.pbt_params_name(allow_as_nested)
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
# This file is auto-generated from the current state of the database. Instead
|
3
|
+
# of editing this file, please use the migrations feature of Active Record to
|
4
|
+
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
+
#
|
6
|
+
# Note that this schema.rb definition is the authoritative source for your
|
7
|
+
# database schema. If you need to create the application database on another
|
8
|
+
# system, you should be using db:schema:load, not running all the migrations
|
9
|
+
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
+
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
+
#
|
12
|
+
# It's strongly recommended that you check this file into your version control system.
|
13
|
+
|
14
|
+
ActiveRecord::Schema.define(version: 20150211224225) do
|
15
|
+
|
16
|
+
create_table "phones", force: :cascade do |t|
|
17
|
+
t.integer "phoneable_id"
|
18
|
+
t.string "phoneable_type"
|
19
|
+
t.datetime "created_at", null: false
|
20
|
+
t.datetime "updated_at", null: false
|
21
|
+
end
|
22
|
+
|
23
|
+
add_index "phones", ["phoneable_type", "phoneable_id"], name: "index_phones_on_phoneable_type_and_phoneable_id"
|
24
|
+
|
25
|
+
create_table "tags", force: :cascade do |t|
|
26
|
+
t.integer "user_id"
|
27
|
+
t.datetime "created_at", null: false
|
28
|
+
t.datetime "updated_at", null: false
|
29
|
+
end
|
30
|
+
|
31
|
+
add_index "tags", ["user_id"], name: "index_tags_on_user_id"
|
32
|
+
|
33
|
+
create_table "users", force: :cascade do |t|
|
34
|
+
t.datetime "created_at", null: false
|
35
|
+
t.datetime "updated_at", null: false
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -0,0 +1,56 @@
|
|
1
|
+
[1m[36m (14.7ms)[0m [1mCREATE TABLE "schema_migrations" ("version" varchar NOT NULL) [0m
|
2
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
3
|
+
[1m[36m (17.7ms)[0m [1mCREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")[0m
|
4
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.2ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
5
|
+
Migrating to CreateUsers (20150211224139)
|
6
|
+
[1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
7
|
+
[1m[35m (0.3ms)[0m CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
8
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20150211224139"]]
|
9
|
+
[1m[35m (25.7ms)[0m commit transaction
|
10
|
+
Migrating to CreateTags (20150211224157)
|
11
|
+
[1m[36m (0.2ms)[0m [1mbegin transaction[0m
|
12
|
+
[1m[35m (0.5ms)[0m CREATE TABLE "tags" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
|
13
|
+
[1m[36m (0.3ms)[0m [1mCREATE INDEX "index_tags_on_user_id" ON "tags" ("user_id")[0m
|
14
|
+
[1m[35mSQL (0.3ms)[0m INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224157"]]
|
15
|
+
[1m[36m (25.1ms)[0m [1mcommit transaction[0m
|
16
|
+
Migrating to CreatePhones (20150211224225)
|
17
|
+
[1m[35m (0.1ms)[0m begin transaction
|
18
|
+
[1m[36m (0.4ms)[0m [1mCREATE TABLE "phones" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "phoneable_id" integer, "phoneable_type" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) [0m
|
19
|
+
[1m[35m (0.2ms)[0m CREATE INDEX "index_phones_on_phoneable_type_and_phoneable_id" ON "phones" ("phoneable_type", "phoneable_id")
|
20
|
+
[1m[36mSQL (0.2ms)[0m [1mINSERT INTO "schema_migrations" ("version") VALUES (?)[0m [["version", "20150211224225"]]
|
21
|
+
[1m[35m (20.3ms)[0m commit transaction
|
22
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.2ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
23
|
+
[1m[35m (0.2ms)[0m SELECT sql
|
24
|
+
FROM sqlite_master
|
25
|
+
WHERE name='index_phones_on_phoneable_type_and_phoneable_id' AND type='index'
|
26
|
+
UNION ALL
|
27
|
+
SELECT sql
|
28
|
+
FROM sqlite_temp_master
|
29
|
+
WHERE name='index_phones_on_phoneable_type_and_phoneable_id' AND type='index'
|
30
|
+
|
31
|
+
[1m[36m (0.2ms)[0m [1m SELECT sql
|
32
|
+
FROM sqlite_master
|
33
|
+
WHERE name='index_tags_on_user_id' AND type='index'
|
34
|
+
UNION ALL
|
35
|
+
SELECT sql
|
36
|
+
FROM sqlite_temp_master
|
37
|
+
WHERE name='index_tags_on_user_id' AND type='index'
|
38
|
+
[0m
|
39
|
+
[1m[36mActiveRecord::SchemaMigration Load (0.1ms)[0m [1mSELECT "schema_migrations".* FROM "schema_migrations"[0m
|
40
|
+
[1m[35mActiveRecord::SchemaMigration Load (0.1ms)[0m SELECT "schema_migrations".* FROM "schema_migrations"
|
41
|
+
[1m[36m (0.1ms)[0m [1m SELECT sql
|
42
|
+
FROM sqlite_master
|
43
|
+
WHERE name='index_phones_on_phoneable_type_and_phoneable_id' AND type='index'
|
44
|
+
UNION ALL
|
45
|
+
SELECT sql
|
46
|
+
FROM sqlite_temp_master
|
47
|
+
WHERE name='index_phones_on_phoneable_type_and_phoneable_id' AND type='index'
|
48
|
+
[0m
|
49
|
+
[1m[35m (0.1ms)[0m SELECT sql
|
50
|
+
FROM sqlite_master
|
51
|
+
WHERE name='index_tags_on_user_id' AND type='index'
|
52
|
+
UNION ALL
|
53
|
+
SELECT sql
|
54
|
+
FROM sqlite_temp_master
|
55
|
+
WHERE name='index_tags_on_user_id' AND type='index'
|
56
|
+
|