Skip to content

Commit ced3ea0

Browse files
Rails 8 support (#67)
* Update to allow for rails 8 compatibility * update gem version * modify rakefile to support both sqlite3 and mysql * modify the test matrix * cleanup Rakefile * Use sqlite3 for tests * update readme * set version to 3.4 * remove rails 6.x support * Test rails 7 with ruby 3.0
1 parent f86162e commit ced3ea0

File tree

12 files changed

+88
-82
lines changed

12 files changed

+88
-82
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
gemfile:
17-
- Gemfile.rails-6.1-stable
1817
- Gemfile.rails-7.0-stable
1918
- Gemfile.rails-7.2-stable
20-
ruby-version: ['3.1', '3.0']
19+
- Gemfile.rails-8.0-stable
20+
ruby-version: ['3.0', '3.1', '3.2', '3.3']
2121
exclude:
22+
# Rails 7.2 doesn't work with Ruby 3.0 (requires Ruby 3.1+)
2223
- gemfile: Gemfile.rails-7.2-stable
23-
ruby-version: "3.0"
24+
ruby-version: '3.0'
25+
# Rails 8.0 doesn't work with Ruby 3.0 or 3.1 (requires Ruby 3.2+)
26+
- gemfile: Gemfile.rails-8.0-stable
27+
ruby-version: '3.0'
28+
- gemfile: Gemfile.rails-8.0-stable
29+
ruby-version: '3.1'
2430
env:
2531
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
2632
steps:

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,11 @@
2626

2727
- Remove unsupported rails versions(5.0, 5.2, 6.0) and ruby version(2.7)
2828

29+
## v3.4.0 (2024-XX-XX)
30+
31+
### Added
32+
33+
- Add Rails 8.0 compatibility (requires Ruby 3.2+)
34+
35+
### Removed
36+
- Remove unsupported rails versions 6.x

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,23 @@ Lastly, you will need to be careful of any place where you are doing raw SQL que
138138

139139
## Setup
140140

141-
You'll need to have git, Ruby, and MySQL. Then get up and running with a few commands:
141+
You'll need to have git and Ruby. Then get up and running with a few commands:
142142

143143
```bash
144144
$ git clone ...
145145
$ bundle install
146146
$ vim spec/support/database.yml # Update username and password
147-
$ bin/setup
147+
$ bin/setup # Uses SQLite3 for testing (no additional setup required)
148148
$ bundle exec rspec
149149
```
150150

151+
## Database Compatibility
152+
153+
This gem works with any database supported by ActiveRecord (SQLite3, MySQL, PostgreSQL, etc.).
154+
The gem extends ActiveRecord's polymorphic associations and doesn't use database-specific features.
155+
156+
Development and testing uses SQLite3 for simplicity.
157+
151158
## Contributing
152159

153160
1. Fork it

Rakefile

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,52 @@
1-
require "bundler/gem_tasks"
2-
require "yaml"
3-
require "active_record"
1+
# frozen_string_literal: true
2+
3+
require 'bundler/gem_tasks'
4+
require 'yaml'
5+
require 'active_record'
46

57
namespace :test do
68
task :all do
7-
Dir.glob("./gemfiles/Gemfile*").each do |gemfile|
8-
next if gemfile.end_with?(".lock")
9+
Dir.glob('./gemfiles/Gemfile*').each do |gemfile|
10+
next if gemfile.end_with?('.lock')
11+
912
puts "Running specs for #{Pathname.new(gemfile).basename}"
1013
system("BUNDLE_GEMFILE=#{gemfile} bundle install > /dev/null && BUNDLE_GEMFILE=#{gemfile} bundle exec rspec")
11-
puts ""
14+
puts ''
1215
end
1316
end
1417
end
1518

1619
namespace :db do
17-
database_config = YAML.load(File.open("./spec/support/database.yml"))
18-
admin_database_config = database_config.merge(database: "mysql")
19-
migration_path = File.expand_path("./spec/support/migrations")
20+
database_config = YAML.load(File.open('./spec/support/database.yml'))
21+
migration_path = File.expand_path('./spec/support/migrations')
2022

21-
desc "Create the database"
23+
desc 'Create the database'
2224
task :create do
23-
ActiveRecord::Base.establish_connection(admin_database_config)
24-
ActiveRecord::Base.connection.create_database(database_config.fetch(:database))
25-
puts "Database created."
25+
# SQLite3 creates the database file automatically, just ensure directory exists
26+
db_file = database_config.fetch(:database)
27+
FileUtils.mkdir_p(File.dirname(db_file)) unless File.dirname(db_file) == '.'
28+
puts 'Database ready (SQLite3).'
2629
end
2730

28-
desc "Migrate the database"
31+
desc 'Migrate the database'
2932
task :migrate do
3033
ActiveRecord::Base.establish_connection(database_config)
31-
ActiveRecord::Migrator.migrate(migration_path)
32-
Rake::Task["db:schema"].invoke
33-
puts "Database migrated."
34+
ActiveRecord::MigrationContext.new(migration_path).migrate
35+
Rake::Task['db:schema'].invoke
36+
puts 'Database migrated.'
3437
end
3538

36-
desc "Drop the database"
39+
desc 'Drop the database'
3740
task :drop do
38-
ActiveRecord::Base.establish_connection(admin_database_config)
39-
ActiveRecord::Base.connection.drop_database(database_config.fetch(:database))
40-
puts "Database deleted."
41+
# For SQLite3, just delete the file
42+
db_file = database_config.fetch(:database)
43+
File.delete(db_file) if File.exist?(db_file)
44+
puts 'Database deleted.'
4145
end
4246

43-
desc "Reset the database"
47+
desc 'Reset the database'
4448
task reset: [:drop, :create, :migrate]
45-
desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
49+
desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
4650

4751
task :schema do
4852
# Noop to make ActiveRecord happy

gemfiles/Gemfile.rails-6.1-stable

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

gemfiles/Gemfile.rails-8.0-stable

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source "https://rubygems.org"
4+
5+
gemspec path: ".."
6+
7+
gem "activerecord", github: "rails/rails", branch: "8-0-stable"

lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ module PolymorphicArrayValueExtension
1010
# end
1111

1212
def type_to_ids_mapping
13-
if ACTIVE_RECORD_VERSION < Gem::Version.new("6.1")
14-
association = @associated_table.send(:association)
15-
else
16-
association = @associated_table.send(:reflection)
17-
end
13+
association = @associated_table.send(:reflection)
1814

1915
name = association.name
2016
default_hash = Hash.new { |hsh, key| hsh[key] = [] }

lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@ module Associations
33
class BelongsToPolymorphicAssociation < BelongsToAssociation
44
private
55

6-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
7-
def replace_keys(record)
8-
super
9-
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
10-
end
11-
elsif
12-
def replace_keys(record, force: false)
13-
super
14-
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
15-
end
6+
def replace_keys(record, force: false)
7+
super
8+
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
169
end
1710
end
1811
end

lib/polymorphic_integer_type/extensions.rb

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,7 @@ def remove_integer_type_and_set_attributes_and_extension(integer_type_values, re
118118
if is_polymorphic_integer
119119
reflection.foreign_integer_type = foreign_integer_type
120120
reflection.integer_type = integer_type
121-
122-
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
123-
ActiveRecord::Associations::Association.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
124-
else
125-
ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
126-
end
121+
ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
127122
end
128123
end
129124

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
module PolymorphicIntegerType
2-
VERSION = "3.3.0"
4+
VERSION = '3.4.0'
35
end

0 commit comments

Comments
 (0)