Skip to content

Commit 2164bf3

Browse files
committed
comments' refactor
1 parent f77fc1e commit 2164bf3

File tree

6 files changed

+40
-26
lines changed

6 files changed

+40
-26
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ tmp
2020
.byebug_history
2121
polymorphic_integer_type_test
2222
gemfiles/*.lock
23+
.idea/

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### Changed
2+
3+
## v3.2.1 (2023-12-14)
4+
5+
### Fixed
6+
7+
- Not proper assigning polymorphic value with `has_many` and `has_one` reflection.
8+
9+
### Added
10+
11+
- Added .idea/ folder to .gitignore
12+
13+
### Changed

lib/polymorphic_integer_type/extensions.rb

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module PolymorphicIntegerType
22

33
module Extensions
44
module ClassMethods
5+
ActiveRecord::Reflection::HasManyReflection.attr_accessor(:foreign_integer_type)
6+
ActiveRecord::Reflection::HasManyReflection.attr_accessor(:integer_type)
7+
ActiveRecord::Reflection::HasOneReflection.attr_accessor(:foreign_integer_type)
8+
ActiveRecord::Reflection::HasOneReflection.attr_accessor(:integer_type)
59

610
def belongs_to(name, scope = nil, **options)
711
options = scope if scope.kind_of? Hash
@@ -58,16 +62,16 @@ def remove_type_and_establish_mapping(name, options, scope)
5862

5963
options[:foreign_key] ||= "#{poly_type}_id"
6064
foreign_type = options.delete(:foreign_type) || "#{poly_type}_type"
61-
options[:foreign_integer_type] = foreign_type
62-
options[:integer_type] = klass_mapping.to_i
6365

6466
options[:scope] ||= -> {
6567
condition = where(foreign_type => klass_mapping.to_i)
6668
condition = instance_exec(&scope).merge(condition) if scope.is_a?(Proc)
6769
condition
6870
}
71+
return foreign_type, klass_mapping.to_i
6972
else
7073
options[:scope] ||= scope
74+
return nil, nil
7175
end
7276
end
7377

@@ -88,9 +92,9 @@ def has_many(name, scope = nil, **options, &extension)
8892
scope = nil
8993
end
9094

91-
remove_type_and_establish_mapping(name, options, scope)
92-
super(name, options.delete(:scope), **options.except(:foreign_integer_type, :integer_type), &extension).tap do |_|
93-
remove_integer_type_and_set_attributes_and_extension(options, ActiveRecord::Reflection::HasManyReflection, reflections[name.to_s])
95+
integer_type_values = remove_type_and_establish_mapping(name, options, scope)
96+
super(name, options.delete(:scope), **options, &extension).tap do
97+
remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflections[name.to_s])
9498
end
9599
end
96100

@@ -100,27 +104,25 @@ def has_one(name, scope = nil, **options)
100104
scope = nil
101105
end
102106

103-
remove_type_and_establish_mapping(name, options, scope)
104-
super(name, options.delete(:scope), **options.except(:foreign_integer_type, :integer_type)).tap do |_|
105-
remove_integer_type_and_set_attributes_and_extension(options, ActiveRecord::Reflection::HasOneReflection, reflections[name.to_s])
107+
integer_type_values = remove_type_and_establish_mapping(name, options, scope)
108+
super(name, options.delete(:scope), **options).tap do
109+
remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflections[name.to_s])
106110
end
107111
end
108112

109-
def remove_integer_type_and_set_attributes_and_extension(options, klass, reflection)
110-
foreign_integer_type = options.delete :foreign_integer_type
111-
integer_type = options.delete :integer_type
113+
def remove_integer_type_and_set_attributes_and_extension(integer_type_values, reflection)
114+
foreign_integer_type = integer_type_values[0]
115+
integer_type = integer_type_values[1]
112116
is_polymorphic_integer = foreign_integer_type && integer_type
113117

114118
if is_polymorphic_integer
115-
klass.attr_accessor(:foreign_integer_type)
116-
klass.attr_accessor(:integer_type)
117119
reflection.foreign_integer_type = foreign_integer_type
118120
reflection.integer_type = integer_type
119121

120122
if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
121-
ActiveRecord::Associations::Association.prepend(ActiveRecord::Associations::PolymorphicForeignAssociationExtension)
123+
ActiveRecord::Associations::Association.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
122124
else
123-
ActiveRecord::Associations::ForeignAssociation.prepend(ActiveRecord::Associations::PolymorphicForeignAssociationExtension)
125+
ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
124126
end
125127
end
126128
end

lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
module ActiveRecord
2-
module Associations
3-
module PolymorphicForeignAssociationExtension
1+
module PolymorphicIntegerType
2+
module PolymorphicForeignAssociationExtension
43

5-
def set_owner_attributes(record)
6-
super
7-
if reflection.foreign_integer_type && reflection.integer_type
8-
record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
9-
end
4+
def set_owner_attributes(record)
5+
super
6+
if reflection.foreign_integer_type && reflection.integer_type
7+
record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
108
end
119
end
1210
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module PolymorphicIntegerType
2-
VERSION = "3.2.0"
2+
VERSION = "3.2.1"
33
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
end
2828

2929
context "from HasManyReflection" do
30-
it "sets the source properly from HasManyReflection" do
30+
it "sets the source properly HasManyReflection" do
3131
link_1 = Link.create()
3232
link_2 = Link.create()
3333
dog.source_links = [link_1, link_2]
@@ -39,7 +39,7 @@
3939
end
4040

4141
context "from HasOneReflection" do
42-
it "sets the source properly from HasManyReflection" do
42+
it "sets the source properly HasOneReflection" do
4343
link = Link.create()
4444
dog.source_link = link
4545

0 commit comments

Comments
 (0)