Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support polymorphic_name #606

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .github/workflows/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
MYSQL_PASSWORD: github
MYSQL_DATABASE: composite_primary_keys_unittest
options: >-
--health-cmd "mysqladmin ping -h localhost"
--health-cmd "healthcheck.sh --connect --innodb_initialized"
--health-interval 10s
--health-timeout 5s
--health-retries 5
Expand Down
4 changes: 2 additions & 2 deletions lib/composite_primary_keys/associations/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def creation_attributes
attributes[key1] = owner[key2]
end

if reflection.options[:as]
attributes[reflection.type] = owner.class.base_class.name
if reflection.type
attributes[reflection.type] = owner.class.polymorphic_name
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def next_chain_scope(scope, reflection, next_reflection)
end
end
end
end
end
6 changes: 6 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ user_comment_2:
article: second
person_id: 2
person_type: User

user_comment_3:
id: 4
article: first
person_id: 1
person_type: User1
9 changes: 9 additions & 0 deletions test/fixtures/user_with_polymorphic_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class UserWithPolymorphicName < ActiveRecord::Base
self.table_name = "users"

has_many :comments, :as => :person

def self.polymorphic_name
"User1"
end
end
6 changes: 6 additions & 0 deletions test/test_polymorphic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ def test_clear_has_many_through
article.user_commentators = []
assert_equal(0, article.comments.size)
end

def test_polymorphic_has_many_with_polymorphic_name
comments = UserWithPolymorphicName.find(1).comments
assert_equal 1, comments[0].person_id
assert_equal "User1", comments[0].person_type
end
end