Skip to content

Commit 6025230

Browse files
authored
Merge pull request rails#51462 from fatkodima/fix-composite-fixtures
Fix inline habtm fixtures for tables with composite primary keys
2 parents 2eb6c45 + 5488eb0 commit 6025230

File tree

7 files changed

+59
-3
lines changed

7 files changed

+59
-3
lines changed

activerecord/lib/active_record/fixture_set/table_row.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,25 @@ def add_join_records(association)
193193

194194
targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
195195
joins = targets.map do |target|
196-
join = { lhs_key => @row[model_metadata.primary_key_name],
197-
rhs_key => ActiveRecord::FixtureSet.identify(target, column_type) }
196+
join = {}
197+
198+
if rhs_key.is_a?(Array)
199+
composite_key = ActiveRecord::FixtureSet.composite_identify(target, rhs_key)
200+
composite_key.each do |column, value|
201+
join[column] = value
202+
end
203+
else
204+
join[rhs_key] = ActiveRecord::FixtureSet.identify(target, column_type)
205+
end
206+
207+
if lhs_key.is_a?(Array)
208+
lhs_key.zip(model_metadata.primary_key_name).each do |fkey, pkey|
209+
join[fkey] = @row[pkey]
210+
end
211+
else
212+
join[lhs_key] = @row[model_metadata.primary_key_name]
213+
end
214+
198215
association.timestamp_column_names.each do |col|
199216
join[col] = @now
200217
end

activerecord/test/cases/fixtures_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1777,7 +1777,15 @@ def readonly_config
17771777
end
17781778

17791779
class CompositePkFixturesTest < ActiveRecord::TestCase
1780-
fixtures :cpk_orders, :cpk_books, :cpk_authors, :cpk_reviews, :cpk_order_agreements
1780+
fixtures :cpk_orders, :cpk_books, :cpk_posts, :cpk_tags, :cpk_authors, :cpk_reviews, :cpk_order_agreements
1781+
1782+
def test_supports_inline_habtm
1783+
assert_includes cpk_posts(:welcome).tags, cpk_tags(:cpk_tag_ruby_on_rails)
1784+
assert_includes cpk_posts(:welcome).tags, cpk_tags(:cpk_tag_digital_product)
1785+
assert_not_includes cpk_posts(:welcome).tags, cpk_tags(:cpk_tag_loyal_customer)
1786+
1787+
assert_equal [cpk_tags(:cpk_tag_digital_product)], cpk_posts(:thinking).tags
1788+
end
17811789

17821790
def test_generates_composite_primary_key_for_partially_filled_fixtures
17831791
alice = cpk_authors(:cpk_great_author)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
_fixture:
2+
model_class: Cpk::Post
3+
4+
welcome:
5+
title: Welcome to the weblog
6+
author: David
7+
tags: cpk_tag_ruby_on_rails, cpk_tag_digital_product
8+
9+
thinking:
10+
title: So I was thinking
11+
author: Bob
12+
tags: cpk_tag_digital_product

activerecord/test/models/cpk.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
require_relative "cpk/tag"
1010
require_relative "cpk/chapter"
1111
require_relative "cpk/post"
12+
require_relative "cpk/posts_tag"
1213
require_relative "cpk/comment"
1314
require_relative "cpk/car"
1415
require_relative "cpk/car_review"

activerecord/test/models/cpk/post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ module Cpk
44
class Post < ActiveRecord::Base
55
self.table_name = :cpk_posts
66
has_many :comments, class_name: "Cpk::Comment", foreign_key: %i[commentable_title commentable_author], as: :commentable
7+
has_many :posts_tags, class_name: "Cpk::PostsTag", foreign_key: %i[post_title post_author]
8+
has_many :tags, through: :posts_tags, class_name: "Cpk::Tag"
79
end
810
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
module Cpk
4+
class PostsTag < ActiveRecord::Base
5+
self.table_name = :cpk_posts_tags
6+
7+
belongs_to :post, class_name: "Cpk:Post", foreign_key: %i[post_title post_author]
8+
belongs_to :tag, class_name: "Cpk::Tag"
9+
end
10+
end

activerecord/test/schema/schema.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@
266266
t.string :author
267267
end
268268

269+
create_table :cpk_posts_tags, force: true do |t|
270+
t.string :post_title
271+
t.string :post_author
272+
t.integer :tag_id
273+
end
274+
269275
create_table :cpk_comments, force: true do |t|
270276
t.string :commentable_title
271277
t.string :commentable_author

0 commit comments

Comments
 (0)