Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f547cd2
handle ruby 3.x keyword arguments delegation
rcugut Jan 6, 2022
46cdaa4
unlock activerecord < 7 dependency
olegantonyan Jan 21, 2022
c01f473
fix non-integer relation
olegantonyan Feb 2, 2022
5c56db4
Merge pull request #1 from olegantonyan/ruby-3-and-rails-7-support
rcugut May 20, 2022
592c057
Ruby 3 and Rails 7 support
rcugut May 20, 2022
03d89f9
Merge pull request #53 from rcugut/ruby-3-support
peerkleio Jun 9, 2023
a74a8ea
Update Actions for Rails v7 and Ruby 3
peerkleio Jun 9, 2023
1ee36f1
Update gitignore to ignore lockfiles
peerkleio Jun 9, 2023
957826d
Fix incorrect format for CI with multiple Rubys
peerkleio Jun 9, 2023
1172e9c
Add Ruby 3 CI exclustions
peerkleio Jun 9, 2023
ef12e69
Bump to v3.2.0
peerkleio Jun 9, 2023
3fc20e8
Merge pull request #58 from clio/ruby-3
peerkleio Jun 9, 2023
9200eb4
Fix gem publish action
peerkleio Jun 9, 2023
302a807
Merge pull request #59 from clio/update-publish-action
peerkleio Jun 9, 2023
498a584
Another pipeline fix for publishing
peerkleio Jun 9, 2023
40e2b27
Merge pull request #60 from clio/update-publish-action
peerkleio Jun 9, 2023
f77fc1e
write attribute with polymorphic integer type
wendy-clio Dec 12, 2023
2164bf3
comments' refactor
wendy-clio Dec 13, 2023
148a2cc
Merge pull request #61 from clio/fix_has_many_has_one_assign_logic
wendy-clio Dec 15, 2023
c9ad377
should still able to set other reflection successfully
wendy-clio Dec 19, 2023
246e6c9
Merge pull request #62 from clio/fix_set_own_attributes_extension
wendy-clio Dec 20, 2023
6f9bfc3
bump the version to 3.2.2
wendy-clio Dec 21, 2023
ebb1d76
Merge pull request #63 from clio/bump_version_to_3.2.2
wendy-clio Dec 21, 2023
6253f67
upgrade rails to 7.2.x version; clean up unsupported rails and ruby v…
wendy-clio Oct 29, 2024
f86162e
Merge pull request #64 from clio/upgrade_rails_to_7_2
wendy-clio Jan 13, 2025
5a90993
Merge remote-tracking branch 'upstream/master' into sync-fork-with-up…
WilsonMinFong Jun 26, 2025
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
16 changes: 9 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ jobs:
fail-fast: false
matrix:
gemfile:
- Gemfile.rails-5.0-stable
- Gemfile.rails-5.1-stable
- Gemfile.rails-5.2-stable
- Gemfile.rails-6.0-stable
- Gemfile.rails-6.1-stable
- Gemfile.rails-7.0-stable
- Gemfile.rails-7.2-stable
ruby-version: ['3.1', '3.0']
exclude:
- gemfile: Gemfile.rails-7.2-stable
ruby-version: "3.0"
env:
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
- uses: actions/checkout@v3
- name: Set up Ruby ${{ matrix.ruby-version }}
uses: ruby/setup-ruby@v1
with:
ruby-version: "2.6"
ruby-version: ${{ matrix.ruby-version }}
- name: Install dependencies
run: bundle install
- name: Run tests
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/gem-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ jobs:
contents: read

steps:
- uses: actions/checkout@v2
- name: Set up Ruby 2.7
uses: actions/setup-ruby@v1
- uses: actions/checkout@v3
- name: Set up Ruby 3.1
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.7.x
ruby-version: 3.1

- name: Publish to RubyGems
env:
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ test/version_tmp
tmp
.byebug_history
polymorphic_integer_type_test
gemfiles/*.lock
.idea/
.ruby-version
mysql
polymorphic_integer_type_test-*
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Changelog

## v3.2.1 (2023-12-14)

### Fixed

- Not proper assigning polymorphic value with `has_many` and `has_one` reflection.

### Added

- Added .idea/ folder to .gitignore

## v3.2.2 (2023-12-21)

### Fixed

- Fixed polymorphic_foreign_association_extension.rb to be compatible with other reflection than `has_many` and `has_one`.

## v3.3.0 (2024-10-29)

### Changed

- Upgrade rails support version to be compatible with 7.2

### Removed

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

8 changes: 0 additions & 8 deletions gemfiles/Gemfile.rails-5.0-stable

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/Gemfile.rails-5.2-stable

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/Gemfile.rails-6.0-stable

This file was deleted.

1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-6.1-stable
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ source "https://rubygems.org"
gemspec path: ".."

gem "activerecord", github: "rails/rails", branch: "6-1-stable"
gem "sqlite3", "~> 1.4"
8 changes: 8 additions & 0 deletions gemfiles/Gemfile.rails-7.0-stable
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source "https://rubygems.org"

gemspec path: ".."

gem "activerecord", github: "rails/rails", branch: "7-0-stable"
gem "sqlite3", "~> 1.4"
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ source "https://rubygems.org"

gemspec path: ".."

gem "activerecord", github: "rails/rails", branch: "5-1-stable"
gem "activerecord", github: "rails/rails", branch: "7-2-stable"
1 change: 1 addition & 0 deletions lib/polymorphic_integer_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require "polymorphic_integer_type/module_generator"
require "polymorphic_integer_type/belongs_to_polymorphic_association_extension"
require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
require "polymorphic_integer_type/polymorphic_foreign_association_extension"

if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
Expand Down
35 changes: 31 additions & 4 deletions lib/polymorphic_integer_type/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ module PolymorphicIntegerType

module Extensions
module ClassMethods
ActiveRecord::Reflection::HasManyReflection.attr_accessor(:foreign_integer_type)
ActiveRecord::Reflection::HasManyReflection.attr_accessor(:integer_type)
ActiveRecord::Reflection::HasOneReflection.attr_accessor(:foreign_integer_type)
ActiveRecord::Reflection::HasOneReflection.attr_accessor(:integer_type)

def belongs_to(name, scope = nil, **options)
options = scope if scope.kind_of? Hash
Expand Down Expand Up @@ -64,8 +68,10 @@ def remove_type_and_establish_mapping(name, options, scope)
condition = instance_exec(&scope).merge(condition) if scope.is_a?(Proc)
condition
}
return foreign_type, klass_mapping.to_i
else
options[:scope] ||= scope
return nil, nil
end
end

Expand All @@ -86,8 +92,10 @@ def has_many(name, scope = nil, **options, &extension)
scope = nil
end

remove_type_and_establish_mapping(name, options, scope)
super(name, options.delete(:scope), **options, &extension)
integer_type_values = remove_type_and_establish_mapping(name, options, scope)
super(name, options.delete(:scope), **options, &extension).tap do
remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflections[name.to_s])
end
end

def has_one(name, scope = nil, **options)
Expand All @@ -96,8 +104,27 @@ def has_one(name, scope = nil, **options)
scope = nil
end

remove_type_and_establish_mapping(name, options, scope)
super(name, options.delete(:scope), **options)
integer_type_values = remove_type_and_establish_mapping(name, options, scope)
super(name, options.delete(:scope), **options).tap do
remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflections[name.to_s])
end
end

def remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflection)
foreign_integer_type = integer_type_values[0]
integer_type = integer_type_values[1]
is_polymorphic_integer = foreign_integer_type && integer_type

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
end
end


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module PolymorphicIntegerType
module PolymorphicForeignAssociationExtension

def set_owner_attributes(record)
super
if reflection.try(:foreign_integer_type) && reflection.try(:integer_type)
record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/polymorphic_integer_type/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module PolymorphicIntegerType
VERSION = "3.1.1"
VERSION = "3.3.0"
end
2 changes: 1 addition & 1 deletion polymorphic_integer_type.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "activerecord"
spec.add_dependency "activerecord", "< 8"
spec.add_development_dependency "bundler"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
Expand Down
37 changes: 36 additions & 1 deletion spec/polymorphic_integer_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@
expect(link.target_type).to eq("Food")
end

context "from HasManyReflection" do
it "sets the source properly HasManyReflection" do
link_1 = Link.create()
link_2 = Link.create()
dog.source_links = [link_1, link_2]
expect(link_1.source_type).to eq("Animal")
expect(link_1.source_id).to eq(dog.id)
expect(link_2.source_type).to eq("Animal")
expect(link_1.source_id).to eq(dog.id)
end
end

context "from HasOneReflection" do
it "sets the source properly HasOneReflection" do
link = Link.create()
dog.source_link = link

expect(link.source_type).to eq("Animal")
expect(link.source_id).to eq(dog.id)
end
end

context "when models are namespaced" do
context "and mappings include namespaces" do
it "sets the source_type" do
Expand Down Expand Up @@ -153,7 +175,7 @@
before { link }

it "should have the proper source" do
expect(source.source_links[0].source).to eql source
expect(source.reload.source_links[0].source).to eql source
end
end
end
Expand Down Expand Up @@ -340,9 +362,22 @@ class InlineDrink2 < ActiveRecord::Base
expect(link[:target_type]).to eq(13)
end

it "pulls mapping from given hash" do
animal.source_links.new
end

it "doesn't break string type polymorphic associations" do
expect(link.normal_target).to eq(drink)
expect(link.normal_target_type).to eq("InlineDrink2")
end
end

context "when using other reflection" do
it "owner able to association ActiveRecord::Reflection::ThroughReflection successfully" do
profile_history = ProfileHistory.new
owner.profile_histories << profile_history

expect(owner.profile_histories).to eq([profile_history])
end
end
end
16 changes: 7 additions & 9 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
require 'support/person'
require 'support/food'
require 'support/drink'
require 'support/profile'
require 'support/profile_history'
require 'support/namespaced_activity'
require 'byebug'
require 'pry'
Expand All @@ -21,18 +23,14 @@
active_record_version = Gem::Version.new(ActiveRecord::VERSION::STRING)

ActiveRecord::Base.establish_connection(database_config)

if active_record_version < Gem::Version.new("5.2")
ActiveRecord::Migrator.migrate(migrations_path)
end

if active_record_version >= Gem::Version.new("5.2") && active_record_version < Gem::Version.new("6.0")
ActiveRecord::MigrationContext.new(migrations_path).migrate
end

if active_record_version >= Gem::Version.new("6.0")
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
end

config.around do |example|
Expand Down
2 changes: 1 addition & 1 deletion spec/support/animal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ class Animal < ActiveRecord::Base

belongs_to :owner, class_name: "Person"
has_many :source_links, as: :source, integer_type: true, class_name: "Link"

has_one :source_link, as: :source, integer_type: true, class_name: "Link"
end
16 changes: 16 additions & 0 deletions spec/support/migrations/8_create_profile_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateProfileTable < ActiveRecord::Migration[5.0]

def up
create_table :profiles do |t|
t.integer :person_id
t.integer :profile_history_id
end
end

def down
drop_table :profiles
end

end


14 changes: 14 additions & 0 deletions spec/support/migrations/9_create_profile_history_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateProfileHistoryTable < ActiveRecord::Migration[5.0]

def up
create_table :profile_histories do |t|
end
end

def down
drop_table :profile_histories
end

end


2 changes: 2 additions & 0 deletions spec/support/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ class Person < ActiveRecord::Base
has_many :source_links, as: :source, integer_type: true, class_name: "Link"

has_many :pet_source_links, class_name: "Link", through: :pets, source: :source_links
has_many :profiles
has_many :profile_histories, class_name: "ProfileHistory", through: :profiles
end
4 changes: 4 additions & 0 deletions spec/support/profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Profile < ActiveRecord::Base
belongs_to :person
belongs_to :profile_history
end
3 changes: 3 additions & 0 deletions spec/support/profile_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ProfileHistory < ActiveRecord::Base
has_many :profiles
end