Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 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
b53d82c
Fix Rakefile
WilsonMinFong Jun 26, 2025
edbf6c9
Add spec to reproduce bug
WilsonMinFong Jun 26, 2025
a501d33
Avoid running library code for unrelated belongs_to polymorphic assoc…
WilsonMinFong Jun 26, 2025
2bfccb3
Merge branch 'main' into fix-belongs_to-polymorphic-name-bug
WilsonMinFong Jul 1, 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
11 changes: 8 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,27 @@ namespace :db do
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."
end

desc "Migrate the database"
task :migrate do
ActiveRecord::Base.establish_connection(database_config)
ActiveRecord::Migrator.migrate(migration_path)
if defined?(ActiveRecord::MigrationContext)
migration_context = ActiveRecord::MigrationContext.new(migration_path, ActiveRecord::SchemaMigration)
migration_context.migrate
else
ActiveRecord::Migrator.migrate(migration_path)
end
Rake::Task["db:schema"].invoke
puts "Database migrated."
end

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

Expand Down
1 change: 1 addition & 0 deletions lib/polymorphic_integer_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "polymorphic_integer_type/extensions"
require "polymorphic_integer_type/mapping"
require "polymorphic_integer_type/module_generator"
require "polymorphic_integer_type/belongs_to_valid_options_extension"
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"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
module ActiveRecord
module Associations
class BelongsToPolymorphicAssociation < BelongsToAssociation
private
module PolymorphicIntegerType
module BelongsToPolymorphicAssociationExtension
private

if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
def replace_keys(record)
super
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
def replace_keys(record)
super

if reflection.options[:integer_type] || reflection.options[:polymorphic].is_a?(Hash)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clauses here and on L17 are similar to the conditionals added to the belongs_to here to prevent polymorphic_integer_type behavior from leaking into all belongs_to associations.

owner[reflection.foreign_type] = record.class.base_class unless record.nil?
end
elsif
def replace_keys(record, force: false)
super
end
else
def replace_keys(record, force: false)
super

if reflection.options[:integer_type] || reflection.options[:polymorphic].is_a?(Hash)
owner[reflection.foreign_type] = record.class.base_class unless record.nil?
end
end
end
end
end

ActiveRecord::Associations::BelongsToPolymorphicAssociation.prepend(PolymorphicIntegerType::BelongsToPolymorphicAssociationExtension)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to have super above appropriately use the Rails-defined method in ActiveRecord::Associations::BelongsToPolymorphicAssociation, we need to prepend this code instead of override the method altogether.

11 changes: 11 additions & 0 deletions lib/polymorphic_integer_type/belongs_to_valid_options_extension.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module PolymorphicIntegerType
module BelongsToValidOptionsExtension
def valid_options(options)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without adding :integer_type to valid_options, Rails throws an unsupported argument error. Previously, this library was simply deleting this option before building the association, but I can't find any way to distinguish belongs_to associations that utilize this gem vs those that don't without the option. Curious if the reviewer can think of other ways to achieve this though.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense - we need to be able to determine whether the association uses some polymorphic_integer_type-specific config at the time replace_keys is called, so persisting that information on reflection options feels appropriate.

valid = super
valid << :integer_type if options[:polymorphic]
valid
end
end
end

ActiveRecord::Associations::Builder::BelongsTo.singleton_class.prepend(PolymorphicIntegerType::BelongsToValidOptionsExtension)
7 changes: 3 additions & 4 deletions lib/polymorphic_integer_type/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ module ClassMethods

def belongs_to(name, scope = nil, **options)
options = scope if scope.kind_of? Hash
integer_type = options.delete :integer_type
super
if options[:polymorphic] && (integer_type || options[:polymorphic].is_a?(Hash))
if options[:polymorphic] && (options[:integer_type] || options[:polymorphic].is_a?(Hash))
mapping =
case integer_type
case options[:integer_type]
when true then PolymorphicIntegerType::Mapping[name]
when nil then options[:polymorphic]
else
raise ArgumentError, "Unknown integer_type value: #{integer_type.inspect}"
raise ArgumentError, "Unknown integer_type value: #{options[:integer_type].inspect}"
end.dup

foreign_type = reflections[name.to_s].foreign_type
Expand Down
12 changes: 12 additions & 0 deletions spec/polymorphic_integer_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
end
end
end

context "When a link is given polymorphic record" do
let(:link) { Link.create(source: source) }
let(:source) { cat }
Expand All @@ -193,6 +194,17 @@
end
end

context "When a link is given polymorphic record via a non-integer type association" do

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec mimic the behavior we're seeing in the main rma repo and fails on the main branch, but passes on this branch

let(:link) { Link.create(legacy_source: source) }
let(:source) { Place.create(name: "Main Street") }

it "appropriately sets the source_id and source_type to the polymorphic_name" do
expect(link.legacy_source_id).to eql source.id
expect(link.legacy_source_type).to eql Place.polymorphic_name
expect(link.legacy_source).to eql source
end
end

context "When a link is given polymorphic id and type" do
let(:link) { Link.create(source_id: source.id, source_type: source.class.to_s) }
let(:source) { cat }
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'support/profile'
require 'support/profile_history'
require 'support/namespaced_activity'
require 'support/place'
require 'byebug'
require 'pry'

Expand Down
6 changes: 6 additions & 0 deletions spec/support/link.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
class Link < ActiveRecord::Base
include PolymorphicIntegerType::Extensions

def self.polymorphic_class_for(name)
name == 'Country' ? Place : super
end

belongs_to :source, polymorphic: true, integer_type: true
belongs_to :target, polymorphic: true, integer_type: true

belongs_to :legacy_source, polymorphic: true
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddLegacySourceReferenceToLinksTable < ActiveRecord::Migration[5.0]
def change
add_reference :links, :legacy_source, polymorphic: true
end
end


7 changes: 7 additions & 0 deletions spec/support/place.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Place < ActiveRecord::Base
has_many :source_links, as: :target, inverse_of: :legacy_source, class_name: "Link"

def self.polymorphic_name
'Country'
end
end