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

Fix inline habtm fixtures for tables with composite primary keys #51462

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions activerecord/lib/active_record/fixture_set/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,25 @@ def add_join_records(association)

targets = targets.is_a?(Array) ? targets : targets.split(/\s*,\s*/)
joins = targets.map do |target|
join = { lhs_key => @row[model_metadata.primary_key_name],
rhs_key => ActiveRecord::FixtureSet.identify(target, column_type) }
join = {}

if rhs_key.is_a?(Array)
composite_key = ActiveRecord::FixtureSet.composite_identify(target, rhs_key)
composite_key.each do |column, value|
join[column] = value
end
else
join[rhs_key] = ActiveRecord::FixtureSet.identify(target, column_type)
end

if lhs_key.is_a?(Array)
lhs_key.zip(model_metadata.primary_key_name).each do |fkey, pkey|
join[fkey] = @row[pkey]
end
else
join[lhs_key] = @row[model_metadata.primary_key_name]
end

association.timestamp_column_names.each do |col|
join[col] = @now
end
Expand Down
10 changes: 9 additions & 1 deletion activerecord/test/cases/fixtures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,15 @@ def readonly_config
end

class CompositePkFixturesTest < ActiveRecord::TestCase
fixtures :cpk_orders, :cpk_books, :cpk_authors, :cpk_reviews, :cpk_order_agreements
fixtures :cpk_orders, :cpk_books, :cpk_posts, :cpk_tags, :cpk_authors, :cpk_reviews, :cpk_order_agreements

def test_supports_inline_habtm
assert(cpk_posts(:welcome).tags.include?(cpk_tags(:cpk_tag_ruby_on_rails)))
assert(cpk_posts(:welcome).tags.include?(cpk_tags(:cpk_tag_digital_product)))
assert_not(cpk_posts(:welcome).tags.include?(cpk_tags(:cpk_tag_loyal_customer)))

assert_equal(cpk_posts(:thinking).tags, [cpk_tags(:cpk_tag_digital_product)])
end

def test_generates_composite_primary_key_for_partially_filled_fixtures
alice = cpk_authors(:cpk_great_author)
Expand Down
12 changes: 12 additions & 0 deletions activerecord/test/fixtures/cpk_posts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
_fixture:
model_class: Cpk::Post

welcome:
title: Welcome to the weblog
author: David
tags: cpk_tag_ruby_on_rails, cpk_tag_digital_product

thinking:
title: So I was thinking
author: Bob
tags: cpk_tag_digital_product
1 change: 1 addition & 0 deletions activerecord/test/models/cpk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative "cpk/tag"
require_relative "cpk/chapter"
require_relative "cpk/post"
require_relative "cpk/posts_tag"
require_relative "cpk/comment"
require_relative "cpk/car"
require_relative "cpk/car_review"
2 changes: 2 additions & 0 deletions activerecord/test/models/cpk/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ module Cpk
class Post < ActiveRecord::Base
self.table_name = :cpk_posts
has_many :comments, class_name: "Cpk::Comment", query_constraints: %i[commentable_title commentable_author], as: :commentable
has_many :posts_tags, class_name: "Cpk::PostsTag", query_constraints: %i[post_title post_author]
has_many :tags, through: :posts_tags, class_name: "Cpk::Tag"
end
end
10 changes: 10 additions & 0 deletions activerecord/test/models/cpk/posts_tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Cpk
class PostsTag < ActiveRecord::Base
self.table_name = :cpk_posts_tags

belongs_to :post, class_name: "Cpk:Post", query_constraints: %i[post_title post_author]
belongs_to :tag, class_name: "Cpk::Tag"
end
end
6 changes: 6 additions & 0 deletions activerecord/test/schema/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@
t.string :author
end

create_table :cpk_posts_tags, force: true do |t|
t.string :post_title
t.string :post_author
t.integer :tag_id
end

create_table :cpk_comments, force: true do |t|
t.string :commentable_title
t.string :commentable_author
Expand Down