polymorphic_integer_type 3.2.0 → 3.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0a0c496105cf4f71bbb7d8e37f4b7d5464a155c6d69adc32860a42f57b54ce2
4
- data.tar.gz: 6b91ce4961f8ff169c8a106bc258d25ff8f59aff0c59a97ed7488c9d708851bd
3
+ metadata.gz: cfa2f9c68364a27168180b5dfeaae71b6ea2a9e74c313a8f42658b6e3a562dcd
4
+ data.tar.gz: b4f0b2225be4e6d8270f1e4d1755a15d791aec80a3f20afd3cd2c6d9f4923603
5
5
  SHA512:
6
- metadata.gz: 224efc3b41dc88ba440de3a87398a6422e817cc6adcedb216145d9fdfe004c9d3c5da733f81e547d27481dae3806290173dd4db65d4b72759084d0c4132a48d0
7
- data.tar.gz: 91168a3fa7ed06b8de0c51b15d5199a66ede9e0b3265a7ea23861a796219f2948e9eef18b51c8c349fe23706ebfb9c9131fbec181a175c703c6d701061205a84
6
+ metadata.gz: 12fa770fcb992eb05270b971a8f1ff88d881318d09bccc9f3e7d139e01396054873a68f1083238db0af4d7dc32bf3a715a51d848df2d9432e060580a6b524cf8
7
+ data.tar.gz: ca0d6e461f1543b2f47476d53e679d24de1420e60acaa8c51119f1bfa05ac962f5ac189912d9a7ae12e63f9568af91e6e0550dbc448efc3b2e75c6f655a8e0c5
data/.gitignore CHANGED
@@ -20,3 +20,4 @@ tmp
20
20
  .byebug_history
21
21
  polymorphic_integer_type_test
22
22
  gemfiles/*.lock
23
+ .idea/
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
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
+ ## v3.2.2 (2023-12-21)
14
+
15
+ ### Fixed
16
+
17
+ - Fixed polymorphic_foreign_association_extension.rb to be compatible with other reflection than `has_many` and `has_one`.
18
+
19
+ ### Changed
@@ -2,6 +2,10 @@ module PolymorphicIntegerType
2
2
 
3
3
  module Extensions
4
4
  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)
5
9
 
6
10
  def belongs_to(name, scope = nil, **options)
7
11
  options = scope if scope.kind_of? Hash
@@ -64,8 +68,10 @@ module PolymorphicIntegerType
64
68
  condition = instance_exec(&scope).merge(condition) if scope.is_a?(Proc)
65
69
  condition
66
70
  }
71
+ return foreign_type, klass_mapping.to_i
67
72
  else
68
73
  options[:scope] ||= scope
74
+ return nil, nil
69
75
  end
70
76
  end
71
77
 
@@ -86,8 +92,10 @@ module PolymorphicIntegerType
86
92
  scope = nil
87
93
  end
88
94
 
89
- remove_type_and_establish_mapping(name, options, scope)
90
- super(name, options.delete(:scope), **options, &extension)
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])
98
+ end
91
99
  end
92
100
 
93
101
  def has_one(name, scope = nil, **options)
@@ -96,8 +104,27 @@ module PolymorphicIntegerType
96
104
  scope = nil
97
105
  end
98
106
 
99
- remove_type_and_establish_mapping(name, options, scope)
100
- super(name, options.delete(:scope), **options)
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])
110
+ end
111
+ end
112
+
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]
116
+ is_polymorphic_integer = foreign_integer_type && integer_type
117
+
118
+ if is_polymorphic_integer
119
+ reflection.foreign_integer_type = foreign_integer_type
120
+ reflection.integer_type = integer_type
121
+
122
+ if Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new("6.1")
123
+ ActiveRecord::Associations::Association.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
124
+ else
125
+ ActiveRecord::Associations::ForeignAssociation.prepend(PolymorphicIntegerType::PolymorphicForeignAssociationExtension)
126
+ end
127
+ end
101
128
  end
102
129
 
103
130
 
@@ -0,0 +1,11 @@
1
+ module PolymorphicIntegerType
2
+ module PolymorphicForeignAssociationExtension
3
+
4
+ def set_owner_attributes(record)
5
+ super
6
+ if reflection.try(:foreign_integer_type) && reflection.try(:integer_type)
7
+ record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module PolymorphicIntegerType
2
- VERSION = "3.2.0"
2
+ VERSION = "3.2.2"
3
3
  end
@@ -6,6 +6,7 @@ require "polymorphic_integer_type/mapping"
6
6
  require "polymorphic_integer_type/module_generator"
7
7
  require "polymorphic_integer_type/belongs_to_polymorphic_association_extension"
8
8
  require "polymorphic_integer_type/activerecord_5_0_0/polymorphic_array_value_extension"
9
+ require "polymorphic_integer_type/polymorphic_foreign_association_extension"
9
10
 
10
11
  if ACTIVE_RECORD_VERSION < Gem::Version.new("5.2.0")
11
12
  require "polymorphic_integer_type/activerecord_5_0_0/association_query_handler_extension"
@@ -26,6 +26,28 @@ describe PolymorphicIntegerType do
26
26
  expect(link.target_type).to eq("Food")
27
27
  end
28
28
 
29
+ context "from HasManyReflection" do
30
+ it "sets the source properly HasManyReflection" do
31
+ link_1 = Link.create()
32
+ link_2 = Link.create()
33
+ dog.source_links = [link_1, link_2]
34
+ expect(link_1.source_type).to eq("Animal")
35
+ expect(link_1.source_id).to eq(dog.id)
36
+ expect(link_2.source_type).to eq("Animal")
37
+ expect(link_1.source_id).to eq(dog.id)
38
+ end
39
+ end
40
+
41
+ context "from HasOneReflection" do
42
+ it "sets the source properly HasOneReflection" do
43
+ link = Link.create()
44
+ dog.source_link = link
45
+
46
+ expect(link.source_type).to eq("Animal")
47
+ expect(link.source_id).to eq(dog.id)
48
+ end
49
+ end
50
+
29
51
  context "when models are namespaced" do
30
52
  context "and mappings include namespaces" do
31
53
  it "sets the source_type" do
@@ -340,9 +362,22 @@ describe PolymorphicIntegerType do
340
362
  expect(link[:target_type]).to eq(13)
341
363
  end
342
364
 
365
+ it "pulls mapping from given hash" do
366
+ animal.source_links.new
367
+ end
368
+
343
369
  it "doesn't break string type polymorphic associations" do
344
370
  expect(link.normal_target).to eq(drink)
345
371
  expect(link.normal_target_type).to eq("InlineDrink2")
346
372
  end
347
373
  end
374
+
375
+ context "when using other reflection" do
376
+ it "owner able to association ActiveRecord::Reflection::ThroughReflection successfully" do
377
+ profile_history = ProfileHistory.new
378
+ owner.profile_histories << profile_history
379
+
380
+ expect(owner.profile_histories).to eq([profile_history])
381
+ end
382
+ end
348
383
  end
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,8 @@ require 'support/dog'
10
10
  require 'support/person'
11
11
  require 'support/food'
12
12
  require 'support/drink'
13
+ require 'support/profile'
14
+ require 'support/profile_history'
13
15
  require 'support/namespaced_activity'
14
16
  require 'byebug'
15
17
  require 'pry'
@@ -3,5 +3,5 @@ class Animal < ActiveRecord::Base
3
3
 
4
4
  belongs_to :owner, class_name: "Person"
5
5
  has_many :source_links, as: :source, integer_type: true, class_name: "Link"
6
-
6
+ has_one :source_link, as: :source, integer_type: true, class_name: "Link"
7
7
  end
@@ -0,0 +1,16 @@
1
+ class CreateProfileTable < ActiveRecord::Migration[5.0]
2
+
3
+ def up
4
+ create_table :profiles do |t|
5
+ t.integer :person_id
6
+ t.integer :profile_history_id
7
+ end
8
+ end
9
+
10
+ def down
11
+ drop_table :profiles
12
+ end
13
+
14
+ end
15
+
16
+
@@ -0,0 +1,14 @@
1
+ class CreateProfileHistoryTable < ActiveRecord::Migration[5.0]
2
+
3
+ def up
4
+ create_table :profile_histories do |t|
5
+ end
6
+ end
7
+
8
+ def down
9
+ drop_table :profile_histories
10
+ end
11
+
12
+ end
13
+
14
+
@@ -5,4 +5,6 @@ class Person < ActiveRecord::Base
5
5
  has_many :source_links, as: :source, integer_type: true, class_name: "Link"
6
6
 
7
7
  has_many :pet_source_links, class_name: "Link", through: :pets, source: :source_links
8
+ has_many :profiles
9
+ has_many :profile_histories, class_name: "ProfileHistory", through: :profiles
8
10
  end
@@ -0,0 +1,4 @@
1
+ class Profile < ActiveRecord::Base
2
+ belongs_to :person
3
+ belongs_to :profile_history
4
+ end
@@ -0,0 +1,3 @@
1
+ class ProfileHistory < ActiveRecord::Base
2
+ has_many :profiles
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorphic_integer_type
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.0
4
+ version: 3.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle d'Oliveira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-09 00:00:00.000000000 Z
11
+ date: 2023-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -103,6 +103,7 @@ extensions: []
103
103
  extra_rdoc_files: []
104
104
  files:
105
105
  - ".gitignore"
106
+ - CHANGELOG.md
106
107
  - Gemfile
107
108
  - LICENSE.txt
108
109
  - README.md
@@ -121,6 +122,7 @@ files:
121
122
  - lib/polymorphic_integer_type/extensions.rb
122
123
  - lib/polymorphic_integer_type/mapping.rb
123
124
  - lib/polymorphic_integer_type/module_generator.rb
125
+ - lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb
124
126
  - lib/polymorphic_integer_type/version.rb
125
127
  - polymorphic_integer_type.gemspec
126
128
  - spec/polymorphic_integer_type_spec.rb
@@ -139,10 +141,14 @@ files:
139
141
  - spec/support/migrations/5_create_drink_table.rb
140
142
  - spec/support/migrations/6_create_plant_table.rb
141
143
  - spec/support/migrations/7_create_activity_table.rb
144
+ - spec/support/migrations/8_create_profile_table.rb
145
+ - spec/support/migrations/9_create_profile_history_table.rb
142
146
  - spec/support/namespaced_activity.rb
143
147
  - spec/support/namespaced_animal.rb
144
148
  - spec/support/namespaced_plant.rb
145
149
  - spec/support/person.rb
150
+ - spec/support/profile.rb
151
+ - spec/support/profile_history.rb
146
152
  homepage: ''
147
153
  licenses:
148
154
  - MIT
@@ -183,7 +189,11 @@ test_files:
183
189
  - spec/support/migrations/5_create_drink_table.rb
184
190
  - spec/support/migrations/6_create_plant_table.rb
185
191
  - spec/support/migrations/7_create_activity_table.rb
192
+ - spec/support/migrations/8_create_profile_table.rb
193
+ - spec/support/migrations/9_create_profile_history_table.rb
186
194
  - spec/support/namespaced_activity.rb
187
195
  - spec/support/namespaced_animal.rb
188
196
  - spec/support/namespaced_plant.rb
189
197
  - spec/support/person.rb
198
+ - spec/support/profile.rb
199
+ - spec/support/profile_history.rb