Skip to content

Commit

Permalink
AR 6.1: Support polymorphic_name
Browse files Browse the repository at this point in the history
`polymorphic_name` was introduced in 5.2.0 but wasn't properly ported to CPK.
  • Loading branch information
marshall-lee committed Sep 8, 2023
1 parent c0f9a9e commit a2e06cf
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [2.5, 2.6, 2.7, 3.0, 3.1]
ruby: [2.5, 2.6, 2.7, "3.0", 3.1]
runs-on: ${{matrix.os}}
services:
mariadb:
Expand All @@ -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 All @@ -30,7 +30,7 @@ jobs:
BUNDLE_JOBS: 4
BUNDLE_PATH: vendor/bundle
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{matrix.ruby}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [2.5, 2.6, 2.7, 3.0, 3.1]
ruby: [2.5, 2.6, 2.7, "3.0", 3.1]
runs-on: ${{matrix.os}}
services:
mysql:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/postgresql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [2.5, 2.6, 2.7, 3.0, 3.1]
ruby: [2.5, 2.6, 2.7, "3.0", 3.1]
runs-on: ${{matrix.os}}
services:
postgres:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
ruby: [2.5, 2.6, 2.7, 3.0, 3.1]
ruby: [2.5, 2.6, 2.7, "3.0", 3.1]
runs-on: ${{matrix.os}}
env:
BUNDLE_WITHOUT: "db2 oracle sqlserver postgresql mysql"
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

0 comments on commit a2e06cf

Please sign in to comment.