aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
blob: 673d5f1dcf03d21ba5b64d06208f0ccad29393d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require "cases/helper"
require "models/post"
require "models/tagging"

module Namespaced
  class Post < ActiveRecord::Base
    self.table_name = "posts"
    has_one :tagging, as: :taggable, class_name: "Tagging"

    def self.polymorphic_name
      sti_name
    end
  end
end

module PolymorphicFullStiClassNamesSharedTest
  def setup
    @old_store_full_sti_class = ActiveRecord::Base.store_full_sti_class
    ActiveRecord::Base.store_full_sti_class = store_full_sti_class

    post = Namespaced::Post.create(title: "Great stuff", body: "This is not", author_id: 1)
    @tagging = post.create_tagging!
  end

  def teardown
    ActiveRecord::Base.store_full_sti_class = @old_store_full_sti_class
  end

  def test_class_names
    ActiveRecord::Base.store_full_sti_class = !store_full_sti_class
    post = Namespaced::Post.find_by_title("Great stuff")
    assert_nil post.tagging

    ActiveRecord::Base.store_full_sti_class = store_full_sti_class
    post = Namespaced::Post.find_by_title("Great stuff")
    assert_equal @tagging, post.tagging
  end

  def test_class_names_with_includes
    ActiveRecord::Base.store_full_sti_class = !store_full_sti_class
    post = Namespaced::Post.includes(:tagging).find_by_title("Great stuff")
    assert_nil post.tagging

    ActiveRecord::Base.store_full_sti_class = store_full_sti_class
    post = Namespaced::Post.includes(:tagging).find_by_title("Great stuff")
    assert_equal @tagging, post.tagging
  end

  def test_class_names_with_eager_load
    ActiveRecord::Base.store_full_sti_class = !store_full_sti_class
    post = Namespaced::Post.eager_load(:tagging).find_by_title("Great stuff")
    assert_nil post.tagging

    ActiveRecord::Base.store_full_sti_class = store_full_sti_class
    post = Namespaced::Post.eager_load(:tagging).find_by_title("Great stuff")
    assert_equal @tagging, post.tagging
  end

  def test_class_names_with_find_by
    post = Namespaced::Post.find_by_title("Great stuff")

    ActiveRecord::Base.store_full_sti_class = !store_full_sti_class
    assert_nil Tagging.find_by(taggable: post)

    ActiveRecord::Base.store_full_sti_class = store_full_sti_class
    assert_equal @tagging, Tagging.find_by(taggable: post)
  end
end

class PolymorphicFullStiClassNamesTest < ActiveRecord::TestCase
  include PolymorphicFullStiClassNamesSharedTest

  private
    def store_full_sti_class
      true
    end
end

class PolymorphicNonFullStiClassNamesTest < ActiveRecord::TestCase
  include PolymorphicFullStiClassNamesSharedTest

  private
    def store_full_sti_class
      false
    end
end