You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+110-2Lines changed: 110 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,35 @@
1
1
# PolymorphicIntegerType
2
2
3
-
TODO: Write a gem description
3
+
Rails' polymorphic assocaitions are pretty useful. The example they give to set it up looks like:
4
+
```ruby
5
+
classPicture < ActiveRecord::Base
6
+
belongs_to :imageable, polymorphic:true
7
+
end
8
+
9
+
classEmployee < ActiveRecord::Base
10
+
has_many :pictures, as::imageable
11
+
end
12
+
13
+
classProduct < ActiveRecord::Base
14
+
has_many :pictures, as::imageable
15
+
end
16
+
```
17
+
18
+
With a migration that looks like:
19
+
```ruby
20
+
classCreatePictures < ActiveRecord::Migration
21
+
defchange
22
+
create_table :picturesdo |t|
23
+
t.string :name
24
+
t.integer :imageable_id
25
+
t.string :imageable_type
26
+
t.timestamps
27
+
end
28
+
end
29
+
end
30
+
```
31
+
32
+
The problem with this approach is the `imageable_type` is a string (and by default it is 255 characters). This is a little rediculous. For comparison, if we had a state machine with X states, would be describe the states with strings `"State1", "State2", etc` or would be just enumerate the state column and make it an integer. This gem will make it so we can use an integer for the `imageable_type` column.
4
33
5
34
## Installation
6
35
@@ -18,7 +47,86 @@ Or install it yourself as:
18
47
19
48
## Usage
20
49
21
-
TODO: Write usage instructions here
50
+
The gem is pretty straightforward to use.
51
+
52
+
First, include the extensions module and add the `integer_type` option to the assocaitions that are going to be using this. (That way it will play nicely with polymorphic association you would rather the type remain as a string)
Second, you need to create a mapping for the polymorphic associations. This should be loaded before the models. Putting it in an initializer is good (`config/initializers/polymorphic_type_mapping.rb`)
71
+
```ruby
72
+
PolymorphicIntegerType::Mapping.configuration do |config|
Note: The mapping here can start from whatever integer you wish, but I would advise not to use 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
+
81
+
If you want to migrate from a polymorphic association that is already a string you'll need to setup a migration (assuming sql for the time being. But this should be pretty straightforward)
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
0 commit comments