Skip to content

Commit 246e6c9

Browse files
authored
Merge pull request #62 from clio/fix_set_own_attributes_extension
Should still able to set other reflection successfully
2 parents 148a2cc + c9ad377 commit 246e6c9

File tree

8 files changed

+51
-1
lines changed

8 files changed

+51
-1
lines changed

lib/polymorphic_integer_type/polymorphic_foreign_association_extension.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module PolymorphicForeignAssociationExtension
33

44
def set_owner_attributes(record)
55
super
6-
if reflection.foreign_integer_type && reflection.integer_type
6+
if reflection.try(:foreign_integer_type) && reflection.try(:integer_type)
77
record._write_attribute(reflection.foreign_integer_type, reflection.integer_type)
88
end
99
end

spec/polymorphic_integer_type_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,13 @@ class InlineDrink2 < ActiveRecord::Base
371371
expect(link.normal_target_type).to eq("InlineDrink2")
372372
end
373373
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
374383
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
require 'support/person'
1111
require 'support/food'
1212
require 'support/drink'
13+
require 'support/profile'
14+
require 'support/profile_history'
1315
require 'support/namespaced_activity'
1416
require 'byebug'
1517
require 'pry'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+

spec/support/person.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ class Person < ActiveRecord::Base
55
has_many :source_links, as: :source, integer_type: true, class_name: "Link"
66

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

spec/support/profile.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Profile < ActiveRecord::Base
2+
belongs_to :person
3+
belongs_to :profile_history
4+
end

spec/support/profile_history.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ProfileHistory < ActiveRecord::Base
2+
has_many :profiles
3+
end

0 commit comments

Comments
 (0)