mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Using existing models for building multiple has_one through tests
Follow up of #32514.
This commit is contained in:
parent
aa941c9ce1
commit
f2151c3b3c
7 changed files with 15 additions and 54 deletions
|
@ -22,10 +22,6 @@ require "models/customer"
|
|||
require "models/carrier"
|
||||
require "models/shop_account"
|
||||
require "models/customer_carrier"
|
||||
require "models/game"
|
||||
require "models/game_board"
|
||||
require "models/game_collection"
|
||||
require "models/game_owner"
|
||||
|
||||
class HasOneThroughAssociationsTest < ActiveRecord::TestCase
|
||||
fixtures :member_types, :members, :clubs, :memberships, :sponsors, :organizations, :minivans,
|
||||
|
@ -69,21 +65,21 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_building_multiple_associations_builds_through_record
|
||||
game_owner = GameOwner.create
|
||||
game_collection = GameCollection.create
|
||||
game_board_with_one_association = GameBoard.new(game_owner: game_owner)
|
||||
assert_nil game_board_with_one_association.game.id
|
||||
game_board_with_two_associations = GameBoard.new(game_owner: game_owner, game_collection: game_collection)
|
||||
assert_nil game_board_with_two_associations.game.id
|
||||
member_type = MemberType.create!
|
||||
member = Member.create!
|
||||
member_detail_with_one_association = MemberDetail.new(member_type: member_type)
|
||||
assert_predicate member_detail_with_one_association.member, :new_record?
|
||||
member_detail_with_two_associations = MemberDetail.new(member_type: member_type, admittable: member)
|
||||
assert_predicate member_detail_with_two_associations.member, :new_record?
|
||||
end
|
||||
|
||||
def test_creating_multiple_associations_creates_through_record
|
||||
game_owner = GameOwner.create
|
||||
game_collection = GameCollection.create
|
||||
game_board_with_one_association = GameBoard.create(game_owner: game_owner)
|
||||
assert_not_nil game_board_with_one_association.game.id
|
||||
game_board_with_two_associations = GameBoard.create(game_owner: game_owner, game_collection: game_collection)
|
||||
assert_not_nil game_board_with_two_associations.game.id
|
||||
member_type = MemberType.create!
|
||||
member = Member.create!
|
||||
member_detail_with_one_association = MemberDetail.create!(member_type: member_type)
|
||||
assert_not_predicate member_detail_with_one_association.member, :new_record?
|
||||
member_detail_with_two_associations = MemberDetail.create!(member_type: member_type, admittable: member)
|
||||
assert_not_predicate member_detail_with_two_associations.member, :new_record?
|
||||
end
|
||||
|
||||
def test_creating_association_sets_both_parent_ids_for_new
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Game < ActiveRecord::Base
|
||||
has_one :game_board
|
||||
belongs_to :game_owner
|
||||
belongs_to :game_collection
|
||||
end
|
|
@ -1,7 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GameBoard < ActiveRecord::Base
|
||||
belongs_to :game
|
||||
has_one :game_owner, through: :game
|
||||
has_one :game_collection, through: :game
|
||||
end
|
|
@ -1,5 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GameCollection < ActiveRecord::Base
|
||||
has_many :games
|
||||
end
|
|
@ -1,5 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GameOwner < ActiveRecord::Base
|
||||
has_many :games
|
||||
end
|
|
@ -5,6 +5,7 @@ class MemberDetail < ActiveRecord::Base
|
|||
belongs_to :organization
|
||||
has_one :member_type, through: :member
|
||||
has_one :membership, through: :member
|
||||
has_one :admittable, through: :member, source_type: "Member"
|
||||
|
||||
has_many :organization_member_details, through: :organization, source: :member_details
|
||||
end
|
||||
|
|
|
@ -364,19 +364,6 @@ ActiveRecord::Schema.define do
|
|||
t.integer :follower_id
|
||||
end
|
||||
|
||||
create_table :games, force: true do |t|
|
||||
t.integer :game_owner_id
|
||||
t.integer :game_collection_id
|
||||
end
|
||||
|
||||
create_table :game_boards, force: true do |t|
|
||||
t.integer :game_id
|
||||
end
|
||||
|
||||
create_table :game_collections, force: true
|
||||
|
||||
create_table :game_owners, force: true
|
||||
|
||||
create_table :goofy_string_id, force: true, id: false do |t|
|
||||
t.string :id, null: false
|
||||
t.string :info
|
||||
|
@ -499,7 +486,8 @@ ActiveRecord::Schema.define do
|
|||
|
||||
create_table :members, force: true do |t|
|
||||
t.string :name
|
||||
t.integer :member_type_id
|
||||
t.references :member_type, index: false
|
||||
t.references :admittable, polymorphic: true, index: false
|
||||
end
|
||||
|
||||
create_table :member_details, force: true do |t|
|
||||
|
|
Loading…
Reference in a new issue