diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4523ebb..5d13f30 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e66bf5..47b6efe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file diff --git a/README.md b/README.md index 5d72bd9..14573c1 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Rakefile b/Rakefile index cb6b440..39eb26e 100644 --- a/Rakefile +++ b/Rakefile @@ -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 diff --git a/gemfiles/Gemfile.rails-6.1-stable b/gemfiles/Gemfile.rails-6.1-stable deleted file mode 100644 index 5056859..0000000 --- a/gemfiles/Gemfile.rails-6.1-stable +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -source "https://rubygems.org" - -gemspec path: ".." - -gem "activerecord", github: "rails/rails", branch: "6-1-stable" -gem "sqlite3", "~> 1.4" diff --git a/gemfiles/Gemfile.rails-8.0-stable b/gemfiles/Gemfile.rails-8.0-stable new file mode 100644 index 0000000..c486345 --- /dev/null +++ b/gemfiles/Gemfile.rails-8.0-stable @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gemspec path: ".." + +gem "activerecord", github: "rails/rails", branch: "8-0-stable" diff --git a/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb b/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb index df57836..1f44ce2 100644 --- a/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb +++ b/lib/polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension.rb @@ -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] = [] } diff --git a/lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb b/lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb index 8840fc2..5671294 100644 --- a/lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb +++ b/lib/polymorphic_integer_type/belongs_to_polymorphic_association_extension.rb @@ -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 diff --git a/lib/polymorphic_integer_type/extensions.rb b/lib/polymorphic_integer_type/extensions.rb index c6c1389..8e2144f 100644 --- a/lib/polymorphic_integer_type/extensions.rb +++ b/lib/polymorphic_integer_type/extensions.rb @@ -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 diff --git a/lib/polymorphic_integer_type/version.rb b/lib/polymorphic_integer_type/version.rb index febae78..c1e9c4d 100644 --- a/lib/polymorphic_integer_type/version.rb +++ b/lib/polymorphic_integer_type/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module PolymorphicIntegerType - VERSION = "3.3.0" + VERSION = '3.4.0' end diff --git a/polymorphic_integer_type.gemspec b/polymorphic_integer_type.gemspec index 52bb61a..babacda 100644 --- a/polymorphic_integer_type.gemspec +++ b/polymorphic_integer_type.gemspec @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fdcbd3d..192de66 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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|