Skip to content

Commit 9665168

Browse files
committed
Improve setup steps for new contributors
I felt some pain while trying to get the tests running. These changes are intended to ease that pain for others in the future.
1 parent e37bbee commit 9665168

7 files changed

Lines changed: 66 additions & 27 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# PolymorphicIntegerType
22

33
Rails' polymorphic associations are pretty useful. The example they give to set it up looks like:
4+
45
```ruby
56
class Picture < ActiveRecord::Base
67
belongs_to :imageable, polymorphic: true
@@ -16,6 +17,7 @@ end
1617
```
1718

1819
With a migration that looks like:
20+
1921
```ruby
2022
class CreatePictures < ActiveRecord::Migration
2123
def change
@@ -52,6 +54,7 @@ For Rails 3.2 use version < 2. Version >= 2 has been tested on Rails 4.2 and Rub
5254
The gem is pretty straightforward to use.
5355

5456
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.)
57+
5558
```ruby
5659
class Picture < ActiveRecord::Base
5760
include PolymorphicIntegerType::Extensions
@@ -70,6 +73,7 @@ end
7073
```
7174

7275
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`)
76+
7377
```ruby
7478
PolymorphicIntegerType::Mapping.configuration do |config|
7579

@@ -81,6 +85,7 @@ end
8185
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.
8286

8387
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.)
88+
8489
```ruby
8590
class PictureToPolymorphicIntegerType < ActiveRecord::Migration
8691

@@ -125,8 +130,18 @@ end
125130
```
126131

127132
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-
129133

134+
## Setup
135+
136+
You'll need to have git, Ruby, and MySQL. Then get up and running with a few commands:
137+
138+
```bash
139+
$ git clone ...
140+
$ bundle install
141+
$ vim spec/support/database.yml # Update username and password
142+
$ bin/setup
143+
$ bundle exec rspec
144+
```
130145

131146
## Contributing
132147

Rakefile

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
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

bin/setup

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
bundle exec rake db:drop db:create db:migrate

polymorphic_integer_type.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ Gem::Specification.new do |spec|
2424
spec.add_development_dependency "activerecord", "4.2.0"
2525
spec.add_development_dependency "mysql2", "0.3.16"
2626
spec.add_development_dependency "byebug"
27-
2827
end

spec/spec_helper.rb

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'support/active_record'
1+
require 'yaml'
2+
require 'active_record'
23
require 'polymorphic_integer_type'
34
require 'support/configuration'
45
require 'support/link'
@@ -8,26 +9,16 @@
89
require 'support/food'
910
require 'support/drink'
1011

11-
12-
1312
RSpec.configure do |config|
14-
1513
config.before(:suite) do
16-
ActiveRecord::Migrator.up "#{File.dirname(__FILE__)}/support/migrations"
14+
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
15+
ActiveRecord::Base.establish_connection(database_config)
1716
end
1817

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-
2618
config.around do |example|
2719
ActiveRecord::Base.transaction do
2820
example.run
2921
raise ActiveRecord::Rollback
3022
end
3123
end
32-
3324
end

spec/support/active_record.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.

spec/support/database.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
:adapter: mysql2
3+
:host: localhost
4+
:database: polymorphic_integer_type_test
5+
:username: root
6+
:password: ''

0 commit comments

Comments
 (0)