Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@ jobs:
fail-fast: false
matrix:
gemfile:
- Gemfile.rails-6.1-stable
- Gemfile.rails-7.0-stable
- Gemfile.rails-7.2-stable
ruby-version: ['3.1', '3.0']
- Gemfile.rails-8.0-stable
ruby-version: ['3.0', '3.1', '3.2', '3.3']
exclude:
# Rails 7.2 doesn't work with Ruby 3.0 (requires Ruby 3.1+)
- gemfile: Gemfile.rails-7.2-stable
ruby-version: "3.0"
ruby-version: '3.0'
# Rails 8.0 doesn't work with Ruby 3.0 or 3.1 (requires Ruby 3.2+)
- gemfile: Gemfile.rails-8.0-stable
ruby-version: '3.0'
- gemfile: Gemfile.rails-8.0-stable
ruby-version: '3.1'
env:
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
steps:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@

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

## v3.4.0 (2024-XX-XX)

### Added

- Add Rails 8.0 compatibility (requires Ruby 3.2+)

### Removed
- Remove unsupported rails versions 6.x
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,23 @@ Lastly, you will need to be careful of any place where you are doing raw SQL que

## Setup

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

```bash
$ git clone ...
$ bundle install
$ vim spec/support/database.yml # Update username and password
$ bin/setup
$ bin/setup # Uses SQLite3 for testing (no additional setup required)
$ bundle exec rspec
```

## Database Compatibility

This gem works with any database supported by ActiveRecord (SQLite3, MySQL, PostgreSQL, etc.).
The gem extends ActiveRecord's polymorphic associations and doesn't use database-specific features.

Development and testing uses SQLite3 for simplicity.

## Contributing

1. Fork it
Expand Down
50 changes: 27 additions & 23 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
require "bundler/gem_tasks"
require "yaml"
require "active_record"
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'yaml'
require 'active_record'

namespace :test do
task :all do
Dir.glob("./gemfiles/Gemfile*").each do |gemfile|
next if gemfile.end_with?(".lock")
Dir.glob('./gemfiles/Gemfile*').each do |gemfile|
next if gemfile.end_with?('.lock')

puts "Running specs for #{Pathname.new(gemfile).basename}"
system("BUNDLE_GEMFILE=#{gemfile} bundle install > /dev/null && BUNDLE_GEMFILE=#{gemfile} bundle exec rspec")
puts ""
puts ''
end
end
end

namespace :db do
database_config = YAML.load(File.open("./spec/support/database.yml"))
admin_database_config = database_config.merge(database: "mysql")
migration_path = File.expand_path("./spec/support/migrations")
database_config = YAML.load(File.open('./spec/support/database.yml'))
migration_path = File.expand_path('./spec/support/migrations')

desc "Create the database"
desc 'Create the database'
task :create do
ActiveRecord::Base.establish_connection(admin_database_config)
ActiveRecord::Base.connection.create_database(database_config.fetch(:database))
puts "Database created."
# SQLite3 creates the database file automatically, just ensure directory exists
db_file = database_config.fetch(:database)
FileUtils.mkdir_p(File.dirname(db_file)) unless File.dirname(db_file) == '.'
puts 'Database ready (SQLite3).'
end

desc "Migrate the database"
desc 'Migrate the database'
task :migrate do
ActiveRecord::Base.establish_connection(database_config)
ActiveRecord::Migrator.migrate(migration_path)
Rake::Task["db:schema"].invoke
puts "Database migrated."
ActiveRecord::MigrationContext.new(migration_path).migrate
Rake::Task['db:schema'].invoke
puts 'Database migrated.'
end

desc "Drop the database"
desc 'Drop the database'
task :drop do
ActiveRecord::Base.establish_connection(admin_database_config)
ActiveRecord::Base.connection.drop_database(database_config.fetch(:database))
puts "Database deleted."
# For SQLite3, just delete the file
db_file = database_config.fetch(:database)
File.delete(db_file) if File.exist?(db_file)
puts 'Database deleted.'
end

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

task :schema do
# Noop to make ActiveRecord happy
Expand Down
8 changes: 0 additions & 8 deletions gemfiles/Gemfile.rails-6.1-stable

This file was deleted.

7 changes: 7 additions & 0 deletions gemfiles/Gemfile.rails-8.0-stable
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec path: ".."

gem "activerecord", github: "rails/rails", branch: "8-0-stable"
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ module PolymorphicArrayValueExtension
# end

def type_to_ids_mapping
if ACTIVE_RECORD_VERSION < Gem::Version.new("6.1")
association = @associated_table.send(:association)
else
association = @associated_table.send(:reflection)
end
association = @associated_table.send(:reflection)

name = association.name
default_hash = Hash.new { |hsh, key| hsh[key] = [] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@ module Associations
class BelongsToPolymorphicAssociation < BelongsToAssociation
private

if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
def replace_keys(record)
super
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
end
elsif
def replace_keys(record, force: false)
super
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
end
def replace_keys(record, force: false)
super
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
end
end
end
Expand Down
7 changes: 1 addition & 6 deletions lib/polymorphic_integer_type/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,7 @@ def remove_integer_type_and_set_attributes_and_extension(integer_type_values, re
if is_polymorphic_integer
reflection.foreign_integer_type = foreign_integer_type
reflection.integer_type = integer_type

if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
ActiveRecord::Associations::Association.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
else
ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
end
ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/polymorphic_integer_type/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module PolymorphicIntegerType
VERSION = "3.3.0"
VERSION = '3.4.0'
end
35 changes: 19 additions & 16 deletions polymorphic_integer_type.gemspec
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'polymorphic_integer_type/version'

Gem::Specification.new do |spec|
spec.name = "polymorphic_integer_type"
spec.name = 'polymorphic_integer_type'
spec.version = PolymorphicIntegerType::VERSION
spec.authors = ["Kyle d'Oliveira"]
spec.email = ["kyle@goclio.com"]
spec.description = %q{Allows the *_type field in the DB to be an integer rather than a string}
spec.summary = %q{Use integers rather than strings for the _type field}
spec.homepage = ""
spec.license = "MIT"
spec.authors = ['Kyle d\'Oliveira']
spec.email = ['kyle@goclio.com']
spec.description = 'Allows the *_type field in the DB to be an integer rather than a string'
spec.summary = 'Use integers rather than strings for the _type field'
spec.homepage = ''
spec.license = 'MIT'

spec.required_ruby_version = '>= 3.0'

spec.files = `git ls-files -- . ':!.github/'`.split($/)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency "activerecord", "< 8"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "pry-byebug"
spec.add_dependency 'activerecord', '< 9.0'
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'sqlite3'
end
9 changes: 1 addition & 8 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,10 @@
config.before(:suite) do
database_config = YAML.load(File.open("#{File.dirname(__FILE__)}/support/database.yml"))
migrations_path = "#{File.dirname(__FILE__)}/support/migrations"
active_record_version = Gem::Version.new(ActiveRecord::VERSION::STRING)

ActiveRecord::Base.establish_connection(database_config)

if active_record_version >= Gem::Version.new("6.1") && active_record_version < Gem::Version.new("7.0")
ActiveRecord::MigrationContext.new(migrations_path, ActiveRecord::SchemaMigration).migrate
end

if active_record_version >= Gem::Version.new("7.0")
ActiveRecord::MigrationContext.new(migrations_path).migrate
end
ActiveRecord::MigrationContext.new(migrations_path).migrate
end

config.around do |example|
Expand Down