polymorphic_integer_type 2.0.0 → 2.1.0
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 +4 -4
- data/README.md +32 -10
- data/Rakefile +38 -0
- data/bin/setup +2 -0
- data/lib/polymorphic_integer_type/extensions.rb +42 -7
- data/lib/polymorphic_integer_type/version.rb +1 -1
- data/polymorphic_integer_type.gemspec +0 -1
- data/spec/polymorphic_integer_type_spec.rb +127 -26
- data/spec/spec_helper.rb +4 -13
- data/spec/support/animal.rb +2 -6
- data/spec/support/configuration.rb +0 -2
- data/spec/support/database.yml +6 -0
- data/spec/support/dog.rb +0 -2
- data/spec/support/drink.rb +1 -5
- data/spec/support/food.rb +1 -4
- data/spec/support/link.rb +2 -4
- data/spec/support/migrations/1_create_link_table.rb +2 -0
- data/spec/support/person.rb +3 -6
- metadata +8 -6
- data/spec/support/active_record.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84ff86a8b1c1b2d27d9834ba6bfb1a5713968e42
|
4
|
+
data.tar.gz: 72f17ee895418f4a1a7db952ce135ebadb0521d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a333e4a0da4f90c6a7ee1e0b304303b17d29fe8be6745f17e02b6e5ddf6cd1d2eb7c77d9ee26281106f5c066cbbea6be89c28ee31974fa9b5642adba4687675e
|
7
|
+
data.tar.gz: 9c4ccc617103c6b818ae219bacc43c5513dac9995617fa3078db10bdfab1eb78c0d632b68d9a73fd628d5c7349031aaf08c63f367614adce6c1e59876f43ae7b
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# PolymorphicIntegerType
|
2
2
|
|
3
3
|
Rails' polymorphic associations are pretty useful. The example they give to set it up looks like:
|
4
|
+
|
4
5
|
```ruby
|
5
6
|
class Picture < ActiveRecord::Base
|
6
7
|
belongs_to :imageable, polymorphic: true
|
@@ -16,6 +17,7 @@ end
|
|
16
17
|
```
|
17
18
|
|
18
19
|
With a migration that looks like:
|
20
|
+
|
19
21
|
```ruby
|
20
22
|
class CreatePictures < ActiveRecord::Migration
|
21
23
|
def change
|
@@ -49,38 +51,48 @@ For Rails 3.2 use version < 2. Version >= 2 has been tested on Rails 4.2 and Rub
|
|
49
51
|
|
50
52
|
## Usage
|
51
53
|
|
52
|
-
|
54
|
+
For the model where the `belongs_to` is defined, include `PolymorphicIntegerType::Extensions` and set the `polymorphic:` option to a hash that maps an integer stored in the database to the name of a Ruby class.
|
53
55
|
|
54
|
-
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.)
|
55
56
|
```ruby
|
56
57
|
class Picture < ActiveRecord::Base
|
57
58
|
include PolymorphicIntegerType::Extensions
|
58
|
-
|
59
|
+
|
60
|
+
belongs_to :imageable, polymorphic: {1 => "Employee", 2 => "Product"}
|
59
61
|
end
|
62
|
+
```
|
60
63
|
|
64
|
+
Next, include `PolymorphicIntegerType::Extensions` into any of the models that point back to the polymorphic integer type association (e.g., `Picture#imageable`) and add a [polymorphic association using `as:`](http://guides.rubyonrails.org/association_basics.html#polymorphic-associations).
|
65
|
+
|
66
|
+
```ruby
|
61
67
|
class Employee < ActiveRecord::Base
|
62
68
|
include PolymorphicIntegerType::Extensions
|
63
|
-
|
69
|
+
|
70
|
+
has_many :pictures, as: :imageable
|
64
71
|
end
|
65
72
|
|
66
73
|
class Product < ActiveRecord::Base
|
67
74
|
include PolymorphicIntegerType::Extensions
|
68
|
-
|
75
|
+
|
76
|
+
has_many :pictures, as: :imageable
|
69
77
|
end
|
70
78
|
```
|
71
79
|
|
72
|
-
|
73
|
-
```ruby
|
74
|
-
PolymorphicIntegerType::Mapping.configuration do |config|
|
80
|
+
### External mappings
|
75
81
|
|
76
|
-
|
82
|
+
You can also store polymorphic type mappings separate from your models. This should be loaded before the models. Putting it in an initializer is one way to do this (e.g., `config/initializers/polymorphic_type_mapping.rb`)
|
77
83
|
|
84
|
+
```ruby
|
85
|
+
PolymorphicIntegerType::Mapping.configuration do |config|
|
86
|
+
config.add :imageable, {1 => "Employee", 2 => "Product" }
|
78
87
|
end
|
79
88
|
```
|
80
89
|
|
81
90
|
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.
|
82
91
|
|
92
|
+
### Migrating an existing association
|
93
|
+
|
83
94
|
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.)
|
95
|
+
|
84
96
|
```ruby
|
85
97
|
class PictureToPolymorphicIntegerType < ActiveRecord::Migration
|
86
98
|
|
@@ -125,8 +137,18 @@ end
|
|
125
137
|
```
|
126
138
|
|
127
139
|
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
|
-
|
129
140
|
|
141
|
+
## Setup
|
142
|
+
|
143
|
+
You'll need to have git, Ruby, and MySQL. Then get up and running with a few commands:
|
144
|
+
|
145
|
+
```bash
|
146
|
+
$ git clone ...
|
147
|
+
$ bundle install
|
148
|
+
$ vim spec/support/database.yml # Update username and password
|
149
|
+
$ bin/setup
|
150
|
+
$ bundle exec rspec
|
151
|
+
```
|
130
152
|
|
131
153
|
## Contributing
|
132
154
|
|
data/Rakefile
CHANGED
@@ -1 +1,39 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
require "yaml"
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
namespace :db do
|
6
|
+
database_config = YAML.load(File.open("./spec/support/database.yml"))
|
7
|
+
admin_database_config = database_config.merge(database: "mysql")
|
8
|
+
migration_path = File.expand_path("./spec/support/migrations")
|
9
|
+
|
10
|
+
desc "Create the database"
|
11
|
+
task :create do
|
12
|
+
ActiveRecord::Base.establish_connection(admin_database_config)
|
13
|
+
ActiveRecord::Base.connection.create_database(database_config.fetch(:database))
|
14
|
+
puts "Database created."
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Migrate the database"
|
18
|
+
task :migrate do
|
19
|
+
ActiveRecord::Base.establish_connection(database_config)
|
20
|
+
ActiveRecord::Migrator.migrate(migration_path)
|
21
|
+
Rake::Task["db:schema"].invoke
|
22
|
+
puts "Database migrated."
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Drop the database"
|
26
|
+
task :drop do
|
27
|
+
ActiveRecord::Base.establish_connection(admin_database_config)
|
28
|
+
ActiveRecord::Base.connection.drop_database(database_config.fetch(:database))
|
29
|
+
puts "Database deleted."
|
30
|
+
end
|
31
|
+
|
32
|
+
desc "Reset the database"
|
33
|
+
task reset: [:drop, :create, :migrate]
|
34
|
+
desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
|
35
|
+
|
36
|
+
task :schema do
|
37
|
+
# Noop to make ActiveRecord happy
|
38
|
+
end
|
39
|
+
end
|
data/bin/setup
ADDED
@@ -7,10 +7,22 @@ module PolymorphicIntegerType
|
|
7
7
|
options = scope if scope.kind_of? Hash
|
8
8
|
integer_type = options.delete :integer_type
|
9
9
|
super
|
10
|
-
if options[:polymorphic] && integer_type
|
11
|
-
mapping =
|
10
|
+
if options[:polymorphic] && (integer_type || options[:polymorphic].is_a?(Hash))
|
11
|
+
mapping =
|
12
|
+
case integer_type
|
13
|
+
when true then PolymorphicIntegerType::Mapping[name]
|
14
|
+
when nil then options[:polymorphic]
|
15
|
+
else
|
16
|
+
raise ArgumentError, "Unknown integer_type value: #{integer_type.inspect}"
|
17
|
+
end.dup
|
18
|
+
|
12
19
|
foreign_type = reflections[name.to_s].foreign_type
|
13
|
-
|
20
|
+
_polymorphic_foreign_types << foreign_type
|
21
|
+
|
22
|
+
# Required way to dynamically define a class method on the model
|
23
|
+
singleton_class.__send__(:define_method, "#{foreign_type}_mapping") do
|
24
|
+
mapping
|
25
|
+
end
|
14
26
|
|
15
27
|
define_method foreign_type do
|
16
28
|
t = super()
|
@@ -40,11 +52,23 @@ module PolymorphicIntegerType
|
|
40
52
|
|
41
53
|
def remove_type_and_establish_mapping(name, options, scope)
|
42
54
|
integer_type = options.delete :integer_type
|
43
|
-
|
55
|
+
polymorphic_type_mapping = retrieve_polymorphic_type_mapping(
|
56
|
+
polymorphic_type: options[:as],
|
57
|
+
class_name: options[:class_name] || name.to_s.classify
|
58
|
+
)
|
59
|
+
|
60
|
+
if options[:as] && (polymorphic_type_mapping || integer_type)
|
44
61
|
poly_type = options.delete(:as)
|
45
|
-
|
46
|
-
|
47
|
-
|
62
|
+
polymorphic_type_mapping ||= PolymorphicIntegerType::Mapping[poly_type]
|
63
|
+
if polymorphic_type_mapping == nil
|
64
|
+
raise "Polymorphic type mapping missing for #{poly_type.inspect}"
|
65
|
+
end
|
66
|
+
|
67
|
+
klass_mapping = (polymorphic_type_mapping || {}).key(sti_name)
|
68
|
+
|
69
|
+
if klass_mapping == nil
|
70
|
+
raise "Class not found for #{sti_name.inspect} in polymorphic type mapping: #{polymorphic_type_mapping}"
|
71
|
+
end
|
48
72
|
|
49
73
|
options[:foreign_key] ||= "#{poly_type}_id"
|
50
74
|
foreign_type = options.delete(:foreign_type) || "#{poly_type}_type"
|
@@ -59,6 +83,17 @@ module PolymorphicIntegerType
|
|
59
83
|
end
|
60
84
|
end
|
61
85
|
|
86
|
+
def retrieve_polymorphic_type_mapping(polymorphic_type:, class_name:)
|
87
|
+
return if polymorphic_type.nil?
|
88
|
+
|
89
|
+
belongs_to_class = class_name.safe_constantize
|
90
|
+
method_name = "#{polymorphic_type}_type_mapping"
|
91
|
+
|
92
|
+
if belongs_to_class && belongs_to_class.respond_to?(method_name)
|
93
|
+
belongs_to_class.public_send(method_name)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
62
97
|
def has_many(name, scope = nil, options = {}, &extension)
|
63
98
|
if scope.kind_of? Hash
|
64
99
|
options = scope
|
@@ -1,21 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PolymorphicIntegerType do
|
4
|
+
let(:owner) { Person.create(name: "Kyle") }
|
5
|
+
let(:dog) { Animal.create(name: "Bela", kind: "Dog", owner: owner) }
|
6
|
+
let(:cat) { Animal.create(name: "Alexi", kind: "Cat") }
|
4
7
|
|
5
|
-
let(:
|
6
|
-
let(:
|
7
|
-
let(:cat) { Animal.create(:name => "Alexi", :kind => "Cat") }
|
8
|
+
let(:kibble) { Food.create(name: "Kibble") }
|
9
|
+
let(:chocolate) { Food.create(name: "Choclate") }
|
8
10
|
|
11
|
+
let(:milk) { Drink.create(name: "milk") }
|
12
|
+
let(:water) { Drink.create(name: "Water") }
|
13
|
+
let(:whiskey) { Drink.create(name: "Whiskey") }
|
9
14
|
|
10
|
-
let(:
|
11
|
-
let(:chocolate) { Food.create(:name => "Choclate") }
|
12
|
-
|
13
|
-
|
14
|
-
let(:milk) { Drink.create(:name => "milk") }
|
15
|
-
let(:water) { Drink.create(:name => "Water") }
|
16
|
-
let(:whiskey) { Drink.create(:name => "Whiskey") }
|
17
|
-
|
18
|
-
let(:link) { Link.create(:source => source, :target => target) }
|
15
|
+
let(:link) { Link.create(source: source, target: target) }
|
19
16
|
|
20
17
|
context "when the source is nil" do
|
21
18
|
let(:source) { nil }
|
@@ -58,13 +55,13 @@ describe PolymorphicIntegerType do
|
|
58
55
|
|
59
56
|
end
|
60
57
|
context "When a link is given polymorphic record" do
|
61
|
-
let(:link) { Link.create(:
|
58
|
+
let(:link) { Link.create(source: source) }
|
62
59
|
let(:source) { cat }
|
63
60
|
include_examples "proper source"
|
64
61
|
|
65
62
|
context "and when it already has a polymorphic record" do
|
66
63
|
let(:target) { kibble }
|
67
|
-
before { link.update_attributes(:
|
64
|
+
before { link.update_attributes(target: target) }
|
68
65
|
|
69
66
|
include_examples "proper source"
|
70
67
|
include_examples "proper target"
|
@@ -74,13 +71,13 @@ describe PolymorphicIntegerType do
|
|
74
71
|
end
|
75
72
|
|
76
73
|
context "When a link is given polymorphic id and type" do
|
77
|
-
let(:link) { Link.create(:
|
74
|
+
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
|
78
75
|
let(:source) { cat }
|
79
76
|
include_examples "proper source"
|
80
77
|
|
81
78
|
context "and when it already has a polymorphic id and type" do
|
82
79
|
let(:target) { kibble }
|
83
|
-
before { link.update_attributes(:
|
80
|
+
before { link.update_attributes(target_id: target.id, target_type: target.class.to_s) }
|
84
81
|
include_examples "proper source"
|
85
82
|
include_examples "proper target"
|
86
83
|
|
@@ -90,8 +87,8 @@ describe PolymorphicIntegerType do
|
|
90
87
|
|
91
88
|
context "When using a relation to the links with eagar loading" do
|
92
89
|
let!(:links){
|
93
|
-
[Link.create(:
|
94
|
-
Link.create(:
|
90
|
+
[Link.create(source: source, target: kibble),
|
91
|
+
Link.create(source: source, target: water)]
|
95
92
|
}
|
96
93
|
let(:source) { cat }
|
97
94
|
|
@@ -105,8 +102,8 @@ describe PolymorphicIntegerType do
|
|
105
102
|
|
106
103
|
context "When using a through relation to the links with eagar loading" do
|
107
104
|
let!(:links){
|
108
|
-
[Link.create(:
|
109
|
-
Link.create(:
|
105
|
+
[Link.create(source: source, target: kibble),
|
106
|
+
Link.create(source: source, target: water)]
|
110
107
|
}
|
111
108
|
let(:source) { dog }
|
112
109
|
|
@@ -119,14 +116,14 @@ describe PolymorphicIntegerType do
|
|
119
116
|
end
|
120
117
|
|
121
118
|
context "When eagar loading the polymorphic association" do
|
122
|
-
let(:link) { Link.create(:
|
119
|
+
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
|
123
120
|
let(:source) { cat }
|
124
121
|
|
125
122
|
context "and when there are multiples sources" do
|
126
|
-
let(:link_2) { Link.create(:
|
123
|
+
let(:link_2) { Link.create(source_id: source_2.id, source_type: source_2.class.to_s) }
|
127
124
|
let(:source_2) { dog }
|
128
125
|
it "should be able to preload both associations" do
|
129
|
-
links = Link.includes(:source).where(:
|
126
|
+
links = Link.includes(:source).where(id: [link.id, link_2.id]).order(:id)
|
130
127
|
expect(links.first.source).to eql cat
|
131
128
|
expect(links.last.source).to eql dog
|
132
129
|
end
|
@@ -134,7 +131,7 @@ describe PolymorphicIntegerType do
|
|
134
131
|
end
|
135
132
|
|
136
133
|
it "should be able to preload the association" do
|
137
|
-
l = Link.includes(:source).where(:
|
134
|
+
l = Link.includes(:source).where(id: link.id).first
|
138
135
|
expect(l.source).to eql cat
|
139
136
|
end
|
140
137
|
|
@@ -142,8 +139,8 @@ describe PolymorphicIntegerType do
|
|
142
139
|
end
|
143
140
|
|
144
141
|
context "when the association is an STI table" do
|
145
|
-
let(:link) { Link.create(:
|
146
|
-
let(:source) { Dog.create(:
|
142
|
+
let(:link) { Link.create(source: source, target: whiskey) }
|
143
|
+
let(:source) { Dog.create(name: "Bela", kind: "Dog", owner: owner) }
|
147
144
|
it "should have the proper id, type and object for the source" do
|
148
145
|
expect(link.source_id).to eql source.id
|
149
146
|
expect(link.source_type).to eql "Animal"
|
@@ -151,5 +148,109 @@ describe PolymorphicIntegerType do
|
|
151
148
|
end
|
152
149
|
end
|
153
150
|
|
151
|
+
context "when mapping is given inline in the belongs_to model" do
|
152
|
+
class InlineLink < ActiveRecord::Base
|
153
|
+
include PolymorphicIntegerType::Extensions
|
154
|
+
|
155
|
+
self.table_name = "links"
|
156
|
+
|
157
|
+
belongs_to :source, polymorphic: {10 => "Person", 11 => "InlineAnimal"}
|
158
|
+
belongs_to :target, polymorphic: {10 => "Food", 13 => "InlineDrink"}
|
159
|
+
belongs_to :normal_target, polymorphic: true
|
160
|
+
end
|
161
|
+
|
162
|
+
class InlineAnimal < ActiveRecord::Base
|
163
|
+
include PolymorphicIntegerType::Extensions
|
164
|
+
|
165
|
+
self.table_name = "animals"
|
166
|
+
|
167
|
+
belongs_to :owner, class_name: "Person"
|
168
|
+
has_many :source_links, as: :source, class_name: "InlineLink"
|
169
|
+
end
|
170
|
+
|
171
|
+
class InlineDrink < ActiveRecord::Base
|
172
|
+
include PolymorphicIntegerType::Extensions
|
173
|
+
|
174
|
+
self.table_name = "drinks"
|
154
175
|
|
176
|
+
has_many :inline_links, as: :target
|
177
|
+
end
|
178
|
+
|
179
|
+
let!(:animal) { InlineAnimal.create!(name: "Lucy") }
|
180
|
+
let!(:drink) { InlineDrink.create!(name: "Water") }
|
181
|
+
let!(:link) { InlineLink.create!(source: animal, target: drink, normal_target: drink) }
|
182
|
+
|
183
|
+
let(:source) { animal }
|
184
|
+
let(:target) { drink }
|
185
|
+
|
186
|
+
include_examples "proper source"
|
187
|
+
include_examples "proper target"
|
188
|
+
|
189
|
+
it "creates foreign_type mapping method" do
|
190
|
+
expect(Link.source_type_mapping).to eq({0 => "Person", 1 => "Animal"})
|
191
|
+
expect(InlineLink.source_type_mapping).to eq({10 => "Person", 11 => "InlineAnimal"})
|
192
|
+
end
|
193
|
+
|
194
|
+
it "pulls mapping from given hash" do
|
195
|
+
expect(link.source_id).to eq(animal.id)
|
196
|
+
expect(link[:source_type]).to eq(11)
|
197
|
+
expect(link.target_id).to eq(drink.id)
|
198
|
+
expect(link[:target_type]).to eq(13)
|
199
|
+
end
|
200
|
+
|
201
|
+
it "doesn't break string type polymorphic associations" do
|
202
|
+
expect(link.normal_target).to eq(drink)
|
203
|
+
expect(link.normal_target_type).to eq("InlineDrink")
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context "when mapping assigned to `polymorphic` option on belongs_to model" do
|
208
|
+
class InlineLink2 < ActiveRecord::Base
|
209
|
+
include PolymorphicIntegerType::Extensions
|
210
|
+
|
211
|
+
self.table_name = "links"
|
212
|
+
|
213
|
+
belongs_to :source, polymorphic: {10 => "Person", 11 => "InlineAnimal2"}
|
214
|
+
belongs_to :target, polymorphic: {10 => "Food", 13 => "InlineDrink2"}
|
215
|
+
belongs_to :normal_target, polymorphic: true
|
216
|
+
end
|
217
|
+
|
218
|
+
class InlineAnimal2 < ActiveRecord::Base
|
219
|
+
include PolymorphicIntegerType::Extensions
|
220
|
+
|
221
|
+
self.table_name = "animals"
|
222
|
+
|
223
|
+
has_many :source_links, as: :source, class_name: "InlineLink2"
|
224
|
+
end
|
225
|
+
|
226
|
+
class InlineDrink2 < ActiveRecord::Base
|
227
|
+
include PolymorphicIntegerType::Extensions
|
228
|
+
|
229
|
+
self.table_name = "drinks"
|
230
|
+
|
231
|
+
has_many :inline_links2, as: :target
|
232
|
+
end
|
233
|
+
|
234
|
+
let!(:animal) { InlineAnimal2.create!(name: "Lucy") }
|
235
|
+
let!(:drink) { InlineDrink2.create!(name: "Water") }
|
236
|
+
let!(:link) { InlineLink2.create!(source: animal, target: drink, normal_target: drink) }
|
237
|
+
|
238
|
+
let(:source) { animal }
|
239
|
+
let(:target) { drink }
|
240
|
+
|
241
|
+
include_examples "proper source"
|
242
|
+
include_examples "proper target"
|
243
|
+
|
244
|
+
it "pulls mapping from given hash" do
|
245
|
+
expect(link.source_id).to eq(animal.id)
|
246
|
+
expect(link[:source_type]).to eq(11)
|
247
|
+
expect(link.target_id).to eq(drink.id)
|
248
|
+
expect(link[:target_type]).to eq(13)
|
249
|
+
end
|
250
|
+
|
251
|
+
it "doesn't break string type polymorphic associations" do
|
252
|
+
expect(link.normal_target).to eq(drink)
|
253
|
+
expect(link.normal_target_type).to eq("InlineDrink2")
|
254
|
+
end
|
255
|
+
end
|
155
256
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
|
-
require '
|
1
|
+
require 'yaml'
|
2
|
+
require 'active_record'
|
2
3
|
require 'polymorphic_integer_type'
|
3
4
|
require 'support/configuration'
|
4
5
|
require 'support/link'
|
@@ -8,26 +9,16 @@ require 'support/person'
|
|
8
9
|
require 'support/food'
|
9
10
|
require 'support/drink'
|
10
11
|
|
11
|
-
|
12
|
-
|
13
12
|
RSpec.configure do |config|
|
14
|
-
|
15
13
|
config.before(:suite) do
|
16
|
-
|
14
|
+
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
|
15
|
+
ActiveRecord::Base.establish_connection(database_config)
|
17
16
|
end
|
18
17
|
|
19
|
-
# No need to return the run the down migration after the test
|
20
|
-
# but useful while in development
|
21
|
-
# config.after(:suite) do
|
22
|
-
# ActiveRecord::Migrator.down "#{File.dirname(__FILE__)}/support/migrations"
|
23
|
-
# end
|
24
|
-
|
25
|
-
|
26
18
|
config.around do |example|
|
27
19
|
ActiveRecord::Base.transaction do
|
28
20
|
example.run
|
29
21
|
raise ActiveRecord::Rollback
|
30
22
|
end
|
31
23
|
end
|
32
|
-
|
33
24
|
end
|
data/spec/support/animal.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
class Animal < ActiveRecord::Base
|
2
|
-
|
3
2
|
include PolymorphicIntegerType::Extensions
|
4
3
|
|
5
|
-
belongs_to :owner, :
|
6
|
-
has_many :source_links, :
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
belongs_to :owner, class_name: "Person"
|
5
|
+
has_many :source_links, as: :source, integer_type: true, class_name: "Link"
|
10
6
|
end
|
data/spec/support/dog.rb
CHANGED
data/spec/support/drink.rb
CHANGED
data/spec/support/food.rb
CHANGED
data/spec/support/link.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
class Link < ActiveRecord::Base
|
2
|
-
|
3
2
|
include PolymorphicIntegerType::Extensions
|
4
3
|
|
5
|
-
belongs_to :source, :
|
6
|
-
belongs_to :target, :
|
7
|
-
|
4
|
+
belongs_to :source, polymorphic: true, integer_type: true
|
5
|
+
belongs_to :target, polymorphic: true, integer_type: true
|
8
6
|
end
|
data/spec/support/person.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
1
|
class Person < ActiveRecord::Base
|
2
|
-
|
3
2
|
include PolymorphicIntegerType::Extensions
|
4
3
|
|
5
|
-
has_many :pets, :
|
6
|
-
has_many :source_links, :
|
7
|
-
|
8
|
-
has_many :pet_source_links, :class_name => "Link", :through => :pets, :source => :source_links
|
9
|
-
|
4
|
+
has_many :pets, class_name: "Animal", foreign_key: :owner_id
|
5
|
+
has_many :source_links, as: :source, integer_type: true, class_name: "Link"
|
10
6
|
|
7
|
+
has_many :pet_source_links, class_name: "Link", through: :pets, source: :source_links
|
11
8
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polymorphic_integer_type
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kyle d'Oliveira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-11-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,7 +97,8 @@ dependencies:
|
|
97
97
|
description: Allows the *_type field in the DB to be an integer rather than a string
|
98
98
|
email:
|
99
99
|
- kyle@goclio.com
|
100
|
-
executables:
|
100
|
+
executables:
|
101
|
+
- setup
|
101
102
|
extensions: []
|
102
103
|
extra_rdoc_files: []
|
103
104
|
files:
|
@@ -106,6 +107,7 @@ files:
|
|
106
107
|
- LICENSE.txt
|
107
108
|
- README.md
|
108
109
|
- Rakefile
|
110
|
+
- bin/setup
|
109
111
|
- lib/polymorphic_integer_type.rb
|
110
112
|
- lib/polymorphic_integer_type/extensions.rb
|
111
113
|
- lib/polymorphic_integer_type/mapping.rb
|
@@ -113,9 +115,9 @@ files:
|
|
113
115
|
- polymorphic_integer_type.gemspec
|
114
116
|
- spec/polymorphic_integer_type_spec.rb
|
115
117
|
- spec/spec_helper.rb
|
116
|
-
- spec/support/active_record.rb
|
117
118
|
- spec/support/animal.rb
|
118
119
|
- spec/support/configuration.rb
|
120
|
+
- spec/support/database.yml
|
119
121
|
- spec/support/dog.rb
|
120
122
|
- spec/support/drink.rb
|
121
123
|
- spec/support/food.rb
|
@@ -146,16 +148,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
146
148
|
version: '0'
|
147
149
|
requirements: []
|
148
150
|
rubyforge_project:
|
149
|
-
rubygems_version: 2.
|
151
|
+
rubygems_version: 2.6.14
|
150
152
|
signing_key:
|
151
153
|
specification_version: 4
|
152
154
|
summary: Use integers rather than strings for the _type field
|
153
155
|
test_files:
|
154
156
|
- spec/polymorphic_integer_type_spec.rb
|
155
157
|
- spec/spec_helper.rb
|
156
|
-
- spec/support/active_record.rb
|
157
158
|
- spec/support/animal.rb
|
158
159
|
- spec/support/configuration.rb
|
160
|
+
- spec/support/database.yml
|
159
161
|
- spec/support/dog.rb
|
160
162
|
- spec/support/drink.rb
|
161
163
|
- spec/support/food.rb
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'active_record'
|
2
|
-
Dir["#{File.dirname(__FILE__)}/migrations/*.rb"].each {|f| require f}
|
3
|
-
|
4
|
-
config = {
|
5
|
-
:adapter => "mysql2",
|
6
|
-
:host => "localhost",
|
7
|
-
:database => "polymorphic_integer_type_test",
|
8
|
-
:username => "root",
|
9
|
-
:password => ""
|
10
|
-
}
|
11
|
-
ActiveRecord::Base.establish_connection(config)
|
12
|
-
|