polymorphic_integer_type 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/README.md +31 -33
- data/lib/polymorphic_integer_type/extensions.rb +1 -1
- data/lib/polymorphic_integer_type/version.rb +1 -1
- metadata +5 -19
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ODEyOTFmYmVhMTRmNmEwYTZmYTI5YzcwYmNkMTUyMGI5Yjk0ZjQ3NQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NDRkNzdiMjUwZjUxYWI3NWM4YTA0N2YzYzcwYTlkZTJkYjE1YTAyZg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjdhZDEwNjRjYzc3OWI4NGY2N2U5ZjEwNmUxNzRjMzkzMTVmZGI3Zjk4MzVh
|
10
|
+
NDhhYjYyNmRjMGJmOTI3NTcyZjMwNjU3Mjc2MGNjNzQ0ZjgzOWZhMzk4Nzg3
|
11
|
+
YTg0OGY4NThmNjI5MzE1ODJiY2FjYmExMTk1MGRlOTA3MmM3ZWE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NzM3ZTZiMGIwMTQwMTk1NTU1N2FmZDk2MGQ5MTRjYjBhZTBiMWUwOTE1ZTU3
|
14
|
+
MjVjYjA3YWJmMTZmY2YxNjViMTk4ZmI1YmRlMDFkZTg2NjRhZjdkYjRlNDhi
|
15
|
+
M2VjNzcxMGVmY2ZiNTg2YmI2YTg0MzI2YWI2ODY1ZTE0MzlmMDM=
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# PolymorphicIntegerType
|
2
2
|
|
3
|
-
Rails' polymorphic
|
3
|
+
Rails' polymorphic associations are pretty useful. The example they give to set it up looks like:
|
4
4
|
```ruby
|
5
5
|
class Picture < ActiveRecord::Base
|
6
6
|
belongs_to :imageable, polymorphic: true
|
@@ -29,7 +29,7 @@ class CreatePictures < ActiveRecord::Migration
|
|
29
29
|
end
|
30
30
|
```
|
31
31
|
|
32
|
-
The problem with this approach is
|
32
|
+
The problem with this approach is that `imageable_type` is a string (and by default it is 255 characters). This is a little ridiculous. For comparison, if we had a state machine with X states, would we describe the states with strings `"State1", "State2", etc` or would we just enumerate the state column and make it an integer? This gem will allow us to use an integer for the `imageable_type` column.
|
33
33
|
|
34
34
|
## Installation
|
35
35
|
|
@@ -49,21 +49,21 @@ Or install it yourself as:
|
|
49
49
|
|
50
50
|
The gem is pretty straightforward to use.
|
51
51
|
|
52
|
-
First, include the extensions module and add the `integer_type` option to the
|
52
|
+
First, include the extensions module and add the `integer_type` option to the associations that are going to be using this. (That way it will play nicely with polymorphic associations whose type you would rather leave as a string.)
|
53
53
|
```ruby
|
54
54
|
class Picture < ActiveRecord::Base
|
55
55
|
include PolymorphicIntegerType::Extensions
|
56
|
-
belongs_to :imageable, polymorphic: true, :
|
56
|
+
belongs_to :imageable, polymorphic: true, integer_type: true
|
57
57
|
end
|
58
58
|
|
59
59
|
class Employee < ActiveRecord::Base
|
60
60
|
include PolymorphicIntegerType::Extensions
|
61
|
-
has_many :pictures, as: :imageable, :
|
61
|
+
has_many :pictures, as: :imageable, integer_type: true
|
62
62
|
end
|
63
63
|
|
64
64
|
class Product < ActiveRecord::Base
|
65
65
|
include PolymorphicIntegerType::Extensions
|
66
|
-
has_many :pictures, as: :imageable, :
|
66
|
+
has_many :pictures, as: :imageable, integer_type: true
|
67
67
|
end
|
68
68
|
```
|
69
69
|
|
@@ -76,55 +76,53 @@ PolymorphicIntegerType::Mapping.configuration do |config|
|
|
76
76
|
end
|
77
77
|
```
|
78
78
|
|
79
|
-
Note: The mapping here can start from whatever integer you wish, but I would advise not
|
79
|
+
Note: The mapping here can start from whatever integer you wish, but I would advise not using 0. The reason being that if you had a new class, for instance `Avatar`, and also wanted to use this polymorphic association but forgot to include it in the mapping, it would effectively get `to_i` called on it and stored in the database. `"Avatar".to_i == 0`, so if your mapping included 0, this would create a weird bug.
|
80
80
|
|
81
|
-
If you want to
|
81
|
+
If you want to convert a polymorphic association that is already a string, you'll need to set up a migration. (Assuming SQL for the time being, but this should be pretty straightforward.)
|
82
82
|
```ruby
|
83
83
|
class PictureToPolymorphicIntegerType < ActiveRecord::Migration
|
84
84
|
|
85
85
|
def up
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
SQL
|
86
|
+
change_table :pictures do |t|
|
87
|
+
t.integer :new_imageable_type
|
88
|
+
end
|
90
89
|
|
91
90
|
execute <<-SQL
|
92
91
|
UPDATE reminders
|
93
92
|
SET new_imageable_type = CASE imageable_type
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
SQL
|
98
|
-
execute <<-SQL
|
99
|
-
ALTER TABLE pictures
|
100
|
-
DROP COLUMN imageable_type,
|
101
|
-
CHANGE COLUMN new_imageable_type imageable_type INTEGER
|
93
|
+
WHEN 'Employee' THEN 1
|
94
|
+
WHEN 'Product' THEN 2
|
95
|
+
END
|
102
96
|
SQL
|
97
|
+
|
98
|
+
change_table :pictures, :bulk => true do |t|
|
99
|
+
t.remove :imageable_type
|
100
|
+
t.rename :new_imageable_type, :imageable_type
|
101
|
+
end
|
103
102
|
end
|
104
103
|
|
105
104
|
def down
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
SQL
|
105
|
+
change_table :pictures do |t|
|
106
|
+
t.string :new_imageable_type
|
107
|
+
end
|
110
108
|
|
111
109
|
execute <<-SQL
|
112
110
|
UPDATE picture
|
113
111
|
SET new_imageable_type = CASE imageable_type
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
SQL
|
118
|
-
execute <<-SQL
|
119
|
-
ALTER TABLE picture
|
120
|
-
DROP COLUMN imageable_type,
|
121
|
-
CHANGE COLUMN new_imageable_type imageable_type VARCHAR(255)
|
112
|
+
WHEN 1 THEN 'Employee'
|
113
|
+
WHEN 2 THEN 'Product'
|
114
|
+
END
|
122
115
|
SQL
|
116
|
+
|
117
|
+
change_table :pictures, :bulk => true do |t|
|
118
|
+
t.remove :imageable_type
|
119
|
+
t.rename :new_imageable_type, :imageable_type
|
120
|
+
end
|
123
121
|
end
|
124
122
|
end
|
125
123
|
```
|
126
124
|
|
127
|
-
Lastly, you will need to be careful of any place where you are doing raw
|
125
|
+
Lastly, you will need to be careful of any place where you are doing raw SQL queries with the string (`imageable_type = 'Employee'`). They should use the integer instead.
|
128
126
|
|
129
127
|
|
130
128
|
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_integer_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Kyle d'Oliveira
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-
|
11
|
+
date: 2013-11-23 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: activerecord
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - '='
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - '='
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -78,7 +69,6 @@ dependencies:
|
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: mysql
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
73
|
- - '='
|
84
74
|
- !ruby/object:Gem::Version
|
@@ -86,7 +76,6 @@ dependencies:
|
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
80
|
- - '='
|
92
81
|
- !ruby/object:Gem::Version
|
@@ -94,7 +83,6 @@ dependencies:
|
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: debugger
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
87
|
- - ! '>='
|
100
88
|
- !ruby/object:Gem::Version
|
@@ -102,7 +90,6 @@ dependencies:
|
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
94
|
- - ! '>='
|
108
95
|
- !ruby/object:Gem::Version
|
@@ -141,27 +128,26 @@ files:
|
|
141
128
|
homepage: ''
|
142
129
|
licenses:
|
143
130
|
- MIT
|
131
|
+
metadata: {}
|
144
132
|
post_install_message:
|
145
133
|
rdoc_options: []
|
146
134
|
require_paths:
|
147
135
|
- lib
|
148
136
|
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
-
none: false
|
150
137
|
requirements:
|
151
138
|
- - ! '>='
|
152
139
|
- !ruby/object:Gem::Version
|
153
140
|
version: '0'
|
154
141
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
|
-
none: false
|
156
142
|
requirements:
|
157
143
|
- - ! '>='
|
158
144
|
- !ruby/object:Gem::Version
|
159
145
|
version: '0'
|
160
146
|
requirements: []
|
161
147
|
rubyforge_project:
|
162
|
-
rubygems_version: 1.
|
148
|
+
rubygems_version: 2.1.11
|
163
149
|
signing_key:
|
164
|
-
specification_version:
|
150
|
+
specification_version: 4
|
165
151
|
summary: Use integers rather than strings for the _type field
|
166
152
|
test_files:
|
167
153
|
- spec/polymorphic_integer_type_spec.rb
|