Skip to content

Commit

Permalink
write attribute with polymorphic integer type
Browse files Browse the repository at this point in the history
  • Loading branch information
wendy-clio committed Dec 12, 2023
1 parent 40e2b27 commit dd95d02
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
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
29 changes: 27 additions & 2 deletions lib/polymorphic_integer_type/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def remove_type_and_establish_mapping(name, options, scope)

options[:foreign_key] ||= "#{poly_type}_id"
foreign_type = options.delete(:foreign_type) || "#{poly_type}_type"
options[:foreign_integer_type] = foreign_type
options[:integer_type] = klass_mapping.to_i

options[:scope] ||= -> {
condition = where(foreign_type => klass_mapping.to_i)
Expand Down Expand Up @@ -87,7 +89,9 @@ def has_many(name, scope = nil, **options, &extension)
end

remove_type_and_establish_mapping(name, options, scope)
super(name, options.delete(:scope), **options, &extension)
super(name, options.delete(:scope), **options.except(:foreign_integer_type, :integer_type), &extension).tap do |_|
remove_integer_type_and_set_attributes_and_extension(options, ActiveRecord::Reflection::HasManyReflection, reflections[name.to_s])
end
end

def has_one(name, scope = nil, **options)
Expand All @@ -97,7 +101,28 @@ def has_one(name, scope = nil, **options)
end

remove_type_and_establish_mapping(name, options, scope)
super(name, options.delete(:scope), **options)
super(name, options.delete(:scope), **options.except(:foreign_integer_type, :integer_type)).tap do |_|
remove_integer_type_and_set_attributes_and_extension(options, ActiveRecord::Reflection::HasOneReflection, reflections[name.to_s])
end
end

def remove_integer_type_and_set_attributes_and_extension(options, klass, reflection)
foreign_integer_type = options.delete :foreign_integer_type
integer_type = options.delete :integer_type
is_polymorphic_integer = foreign_integer_type && integer_type

if is_polymorphic_integer
klass.attr_accessor(:foreign_integer_type)
klass.attr_accessor(:integer_type)
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(ActiveRecord::Associations::PolymorphicForeignAssociationExtension)
else
ActiveRecord::Associations::ForeignAssociation.prepend(ActiveRecord::Associations::PolymorphicForeignAssociationExtension)
end
end
end


Expand Down
27 changes: 26 additions & 1 deletion spec/polymorphic_integer_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
let(:water) { Drink.create(name: "Water") }
let(:whiskey) { Drink.create(name: "Whiskey") }

let(:link) { Link.create(source: source, target: target) }
let(:link) { Link.create() }


context "when creating associations" do
Expand All @@ -26,6 +26,27 @@
expect(link.target_type).to eq("Food")
end

context "from HasManyReflection" do
it "sets the source properly from 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 from HasManyReflection" do
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 @@ -340,6 +361,10 @@ 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")
Expand Down

0 comments on commit dd95d02

Please sign in to comment.