poly_belongs_to 0.1.2 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fe03acf3ce4cdf6ed8409ecaedcca10bea5b010
4
- data.tar.gz: ade1466c5b6ec5482a13548b1a8ecb8d133e7f19
3
+ metadata.gz: a7bb57e153d2b318ee6809a57f925e3adda3ecea
4
+ data.tar.gz: 61110ab7c6b819e37f4c5b82b542f8edb6e123a6
5
5
  SHA512:
6
- metadata.gz: 4bdb66ab09855e35233159048a98d2b1d1d0b7e8ae5cd8f00b7af6b754079e0f7f6da938c9b048b31f87edc8c8f130f34c1b252dc16c7934df71a285d162090b
7
- data.tar.gz: 085cbf59ce738ffe8e8a26a9125b2e1baf7075fe354b6a58634ad11868adfc33dcea5e8a84c9a5b2f0f44a74d2fef3248ed990e976d679c2f39183021227f3ea
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
- # --- On Model Instances ---
57
-
58
+ ```
59
+ #####On model instances
60
+ ```ruby
58
61
  # Polymorphic Belongs To Relations ID
59
62
  MyObject.first.pbt_id
60
- # => 123 # nil for non polymorphic Objects
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: 1 ... >
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 # nil for non polymorphic Objects
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)
@@ -1,3 +1,3 @@
1
1
  module PolyBelongsTo
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -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,3 @@
1
+ class Phone < ActiveRecord::Base
2
+ belongs_to :phoneable, polymorphic: true
3
+ end
@@ -0,0 +1,3 @@
1
+ class Tag < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :tags, dependent: :destroy
3
+ has_many :phones, as: :phoneable, dependent: :destroy
4
+ end
@@ -0,0 +1,8 @@
1
+ class CreateUsers < ActiveRecord::Migration
2
+ def change
3
+ create_table :users do |t|
4
+
5
+ t.timestamps null: false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class CreateTags < ActiveRecord::Migration
2
+ def change
3
+ create_table :tags do |t|
4
+ t.integer :user_id
5
+
6
+ t.timestamps null: false
7
+ end
8
+ add_index :tags, :user_id
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ class CreatePhones < ActiveRecord::Migration
2
+ def change
3
+ create_table :phones do |t|
4
+ t.references :phoneable, polymorphic: true, index: true
5
+
6
+ t.timestamps null: false
7
+ end
8
+ end
9
+ end
@@ -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
Binary file
@@ -0,0 +1,56 @@
1
+  (14.7ms) CREATE TABLE "schema_migrations" ("version" varchar NOT NULL) 
2
+  (0.1ms) select sqlite_version(*)
3
+  (17.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
4
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
5
+ Migrating to CreateUsers (20150211224139)
6
+  (0.1ms) begin transaction
7
+  (0.3ms) CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
8
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224139"]]
9
+  (25.7ms) commit transaction
10
+ Migrating to CreateTags (20150211224157)
11
+  (0.2ms) begin transaction
12
+  (0.5ms) CREATE TABLE "tags" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "user_id" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
13
+  (0.3ms) CREATE INDEX "index_tags_on_user_id" ON "tags" ("user_id")
14
+ SQL (0.3ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224157"]]
15
+  (25.1ms) commit transaction
16
+ Migrating to CreatePhones (20150211224225)
17
+  (0.1ms) begin transaction
18
+  (0.4ms) CREATE 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) 
19
+  (0.2ms) CREATE INDEX "index_phones_on_phoneable_type_and_phoneable_id" ON "phones" ("phoneable_type", "phoneable_id")
20
+ SQL (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES (?) [["version", "20150211224225"]]
21
+  (20.3ms) commit transaction
22
+ ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
23
+  (0.2ms) 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
+  (0.2ms)  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
+ 
39
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
40
+ ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations"
41
+  (0.1ms)  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
+ 
49
+  (0.1ms) 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
+