store_base_sti_class 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/Appraisals +5 -57
  3. data/Gemfile.lock +6 -9
  4. data/README.md +8 -11
  5. data/debug.log +12652 -0
  6. data/gemfiles/rails_3.0.5.gemfile.lock +3 -3
  7. data/gemfiles/rails_3.0.6.gemfile.lock +3 -3
  8. data/gemfiles/rails_3.0.7.gemfile.lock +3 -3
  9. data/gemfiles/rails_3.2.14.gemfile.lock +1 -4
  10. data/gemfiles/rails_4.0.0.gemfile.lock +2 -5
  11. data/gemfiles/rails_4.0.1.gemfile.lock +2 -5
  12. data/gemfiles/rails_4.0.10.gemfile.lock +2 -5
  13. data/gemfiles/rails_4.0.11.1.gemfile.lock +2 -5
  14. data/gemfiles/rails_4.0.11.gemfile.lock +2 -5
  15. data/gemfiles/rails_4.0.12.gemfile.lock +2 -5
  16. data/gemfiles/rails_4.0.13.gemfile.lock +2 -5
  17. data/gemfiles/rails_4.0.2.gemfile.lock +2 -5
  18. data/gemfiles/rails_4.0.3.gemfile.lock +2 -5
  19. data/gemfiles/rails_4.0.4.gemfile.lock +2 -5
  20. data/gemfiles/rails_4.0.5.gemfile.lock +2 -5
  21. data/gemfiles/rails_4.0.6.gemfile.lock +2 -5
  22. data/gemfiles/rails_4.0.7.gemfile.lock +2 -5
  23. data/gemfiles/rails_4.0.8.gemfile.lock +2 -5
  24. data/gemfiles/rails_4.0.9.gemfile.lock +2 -5
  25. data/gemfiles/rails_4.1.0.gemfile.lock +2 -5
  26. data/gemfiles/rails_4.1.1.gemfile.lock +2 -5
  27. data/gemfiles/rails_4.1.10.gemfile.lock +2 -5
  28. data/gemfiles/rails_4.1.11.gemfile.lock +2 -5
  29. data/gemfiles/rails_4.1.12.gemfile.lock +2 -5
  30. data/gemfiles/rails_4.1.13.gemfile.lock +48 -0
  31. data/gemfiles/rails_4.1.14.1.gemfile.lock +48 -0
  32. data/gemfiles/rails_4.1.14.2.gemfile.lock +48 -0
  33. data/gemfiles/rails_4.1.14.gemfile.lock +48 -0
  34. data/gemfiles/rails_4.1.15.gemfile.lock +48 -0
  35. data/gemfiles/rails_4.1.2.gemfile.lock +2 -5
  36. data/gemfiles/rails_4.1.3.gemfile.lock +2 -5
  37. data/gemfiles/rails_4.1.4.gemfile.lock +2 -5
  38. data/gemfiles/rails_4.1.5.gemfile.lock +2 -5
  39. data/gemfiles/rails_4.1.6.gemfile.lock +2 -5
  40. data/gemfiles/rails_4.1.7.1.gemfile.lock +2 -5
  41. data/gemfiles/rails_4.1.7.gemfile.lock +2 -5
  42. data/gemfiles/rails_4.1.8.gemfile.lock +2 -5
  43. data/gemfiles/rails_4.1.9.gemfile.lock +2 -5
  44. data/gemfiles/rails_4.2.0.gemfile.lock +2 -5
  45. data/gemfiles/rails_4.2.1.gemfile.lock +2 -5
  46. data/gemfiles/rails_4.2.2.gemfile.lock +2 -5
  47. data/gemfiles/rails_4.2.3.gemfile.lock +2 -5
  48. data/gemfiles/rails_4.2.4.gemfile.lock +48 -0
  49. data/gemfiles/rails_4.2.5.1.gemfile.lock +48 -0
  50. data/gemfiles/rails_4.2.5.2.gemfile.lock +48 -0
  51. data/gemfiles/rails_4.2.5.gemfile.lock +48 -0
  52. data/gemfiles/rails_4.2.6.gemfile.lock +48 -0
  53. data/gemfiles/{rails_3.0.5.gemfile → rails_4.2.7.1.gemfile} +1 -2
  54. data/gemfiles/{rails_3.0.6.gemfile → rails_4.2.7.gemfile} +1 -2
  55. data/gemfiles/{rails_3.0.7.gemfile → rails_5.0.0.1.gemfile} +1 -2
  56. data/gemfiles/{rails_3.0.8.gemfile → rails_5.0.0.gemfile} +1 -2
  57. data/gemfiles/rails_5.0.1.gemfile +7 -0
  58. data/gemfiles/rails_5.0.1.gemfile.lock +48 -0
  59. data/lib/store_base_sti_class.rb +3 -5
  60. data/lib/store_base_sti_class/version.rb +1 -1
  61. data/lib/store_base_sti_class_for_5_0.rb +404 -0
  62. data/store_base_sti_class.gemspec +3 -3
  63. data/storebasestiname_unittest.sql +0 -0
  64. metadata +26 -64
  65. data/gemfiles/rails_3.0.10.gemfile +0 -8
  66. data/gemfiles/rails_3.0.11.gemfile +0 -8
  67. data/gemfiles/rails_3.0.12.gemfile +0 -8
  68. data/gemfiles/rails_3.0.13.gemfile +0 -8
  69. data/gemfiles/rails_3.0.14.gemfile +0 -8
  70. data/gemfiles/rails_3.0.15.gemfile +0 -8
  71. data/gemfiles/rails_3.0.16.gemfile +0 -8
  72. data/gemfiles/rails_3.0.17.gemfile +0 -8
  73. data/gemfiles/rails_3.0.18.gemfile +0 -8
  74. data/gemfiles/rails_3.0.19.gemfile +0 -8
  75. data/gemfiles/rails_3.0.20.gemfile +0 -8
  76. data/gemfiles/rails_3.0.9.gemfile +0 -8
  77. data/gemfiles/rails_3.1.0.gemfile +0 -8
  78. data/gemfiles/rails_3.1.10.gemfile +0 -8
  79. data/gemfiles/rails_3.1.11.gemfile +0 -8
  80. data/gemfiles/rails_3.1.12.gemfile +0 -8
  81. data/gemfiles/rails_3.1.2.gemfile +0 -8
  82. data/gemfiles/rails_3.1.3.gemfile +0 -8
  83. data/gemfiles/rails_3.1.4.gemfile +0 -8
  84. data/gemfiles/rails_3.1.5.gemfile +0 -8
  85. data/gemfiles/rails_3.1.6.gemfile +0 -8
  86. data/gemfiles/rails_3.1.7.gemfile +0 -8
  87. data/gemfiles/rails_3.1.8.gemfile +0 -8
  88. data/gemfiles/rails_3.1.9.gemfile +0 -8
  89. data/gemfiles/rails_3.2.0.gemfile +0 -8
  90. data/gemfiles/rails_3.2.1.gemfile +0 -8
  91. data/gemfiles/rails_3.2.10.gemfile +0 -8
  92. data/gemfiles/rails_3.2.11.gemfile +0 -8
  93. data/gemfiles/rails_3.2.12.gemfile +0 -8
  94. data/gemfiles/rails_3.2.13.gemfile +0 -8
  95. data/gemfiles/rails_3.2.14.gemfile +0 -8
  96. data/gemfiles/rails_3.2.15.gemfile +0 -8
  97. data/gemfiles/rails_3.2.16.gemfile +0 -8
  98. data/gemfiles/rails_3.2.17.gemfile +0 -8
  99. data/gemfiles/rails_3.2.18.gemfile +0 -8
  100. data/gemfiles/rails_3.2.19.gemfile +0 -8
  101. data/gemfiles/rails_3.2.2.gemfile +0 -8
  102. data/gemfiles/rails_3.2.20.gemfile +0 -8
  103. data/gemfiles/rails_3.2.21.gemfile +0 -8
  104. data/gemfiles/rails_3.2.22.1.gemfile +0 -8
  105. data/gemfiles/rails_3.2.22.2.gemfile +0 -8
  106. data/gemfiles/rails_3.2.22.gemfile +0 -8
  107. data/gemfiles/rails_3.2.3.gemfile +0 -8
  108. data/gemfiles/rails_3.2.4.gemfile +0 -8
  109. data/gemfiles/rails_3.2.5.gemfile +0 -8
  110. data/gemfiles/rails_3.2.6.gemfile +0 -8
  111. data/gemfiles/rails_3.2.7.gemfile +0 -8
  112. data/gemfiles/rails_3.2.8.gemfile +0 -8
  113. data/gemfiles/rails_3.2.9.gemfile +0 -8
  114. data/lib/store_base_sti_class_for_3_0.rb +0 -470
  115. data/lib/store_base_sti_class_for_3_1_and_above.rb +0 -304
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ store_base_sti_class (0.5.0)
5
+ activerecord (>= 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.5.1)
11
+ activesupport (= 4.2.5.1)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.5.1)
14
+ activemodel (= 4.2.5.1)
15
+ activesupport (= 4.2.5.1)
16
+ arel (~> 6.0)
17
+ activesupport (4.2.5.1)
18
+ i18n (~> 0.7)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.3, >= 0.3.4)
22
+ tzinfo (~> 1.1)
23
+ appraisal (2.1.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ arel (6.0.4)
28
+ builder (3.2.3)
29
+ i18n (0.8.0)
30
+ json (1.8.6)
31
+ minitest (5.10.1)
32
+ rake (12.0.0)
33
+ sqlite3 (1.3.13)
34
+ thor (0.19.4)
35
+ thread_safe (0.3.5)
36
+ tzinfo (1.2.2)
37
+ thread_safe (~> 0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ activerecord (= 4.2.5.1)
44
+ appraisal
45
+ bundler
46
+ minitest (>= 4.0)
47
+ sqlite3
48
+ store_base_sti_class!
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ store_base_sti_class (0.5.0)
5
+ activerecord (>= 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.5.2)
11
+ activesupport (= 4.2.5.2)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.5.2)
14
+ activemodel (= 4.2.5.2)
15
+ activesupport (= 4.2.5.2)
16
+ arel (~> 6.0)
17
+ activesupport (4.2.5.2)
18
+ i18n (~> 0.7)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.3, >= 0.3.4)
22
+ tzinfo (~> 1.1)
23
+ appraisal (2.1.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ arel (6.0.4)
28
+ builder (3.2.3)
29
+ i18n (0.8.0)
30
+ json (1.8.6)
31
+ minitest (5.10.1)
32
+ rake (12.0.0)
33
+ sqlite3 (1.3.13)
34
+ thor (0.19.4)
35
+ thread_safe (0.3.5)
36
+ tzinfo (1.2.2)
37
+ thread_safe (~> 0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ activerecord (= 4.2.5.2)
44
+ appraisal
45
+ bundler
46
+ minitest (>= 4.0)
47
+ sqlite3
48
+ store_base_sti_class!
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ store_base_sti_class (0.5.0)
5
+ activerecord (>= 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.5)
11
+ activesupport (= 4.2.5)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.5)
14
+ activemodel (= 4.2.5)
15
+ activesupport (= 4.2.5)
16
+ arel (~> 6.0)
17
+ activesupport (4.2.5)
18
+ i18n (~> 0.7)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.3, >= 0.3.4)
22
+ tzinfo (~> 1.1)
23
+ appraisal (2.1.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ arel (6.0.4)
28
+ builder (3.2.3)
29
+ i18n (0.8.0)
30
+ json (1.8.6)
31
+ minitest (5.10.1)
32
+ rake (12.0.0)
33
+ sqlite3 (1.3.13)
34
+ thor (0.19.4)
35
+ thread_safe (0.3.5)
36
+ tzinfo (1.2.2)
37
+ thread_safe (~> 0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ activerecord (= 4.2.5)
44
+ appraisal
45
+ bundler
46
+ minitest (>= 4.0)
47
+ sqlite3
48
+ store_base_sti_class!
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: ../
3
+ specs:
4
+ store_base_sti_class (0.5.0)
5
+ activerecord (>= 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.6)
11
+ activesupport (= 4.2.6)
12
+ builder (~> 3.1)
13
+ activerecord (4.2.6)
14
+ activemodel (= 4.2.6)
15
+ activesupport (= 4.2.6)
16
+ arel (~> 6.0)
17
+ activesupport (4.2.6)
18
+ i18n (~> 0.7)
19
+ json (~> 1.7, >= 1.7.7)
20
+ minitest (~> 5.1)
21
+ thread_safe (~> 0.3, >= 0.3.4)
22
+ tzinfo (~> 1.1)
23
+ appraisal (2.1.0)
24
+ bundler
25
+ rake
26
+ thor (>= 0.14.0)
27
+ arel (6.0.4)
28
+ builder (3.2.3)
29
+ i18n (0.8.0)
30
+ json (1.8.6)
31
+ minitest (5.10.1)
32
+ rake (12.0.0)
33
+ sqlite3 (1.3.13)
34
+ thor (0.19.4)
35
+ thread_safe (0.3.5)
36
+ tzinfo (1.2.2)
37
+ thread_safe (~> 0.1)
38
+
39
+ PLATFORMS
40
+ ruby
41
+
42
+ DEPENDENCIES
43
+ activerecord (= 4.2.6)
44
+ appraisal
45
+ bundler
46
+ minitest (>= 4.0)
47
+ sqlite3
48
+ store_base_sti_class!
@@ -2,7 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "3.0.5"
6
- gem "minitest", "~> 4.0"
5
+ gem "activerecord", "4.2.7.1"
7
6
 
8
7
  gemspec :path => "../"
@@ -2,7 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "3.0.6"
6
- gem "minitest", "~> 4.0"
5
+ gem "activerecord", "4.2.7"
7
6
 
8
7
  gemspec :path => "../"
@@ -2,7 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "3.0.7"
6
- gem "minitest", "~> 4.0"
5
+ gem "activerecord", "5.0.0.1"
7
6
 
8
7
  gemspec :path => "../"
@@ -2,7 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "3.0.8"
6
- gem "minitest", "~> 4.0"
5
+ gem "activerecord", "5.0.0"
7
6
 
8
7
  gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "5.0.1"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ store_base_sti_class (0.5.0)
5
+ activerecord (>= 3.0.5)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.0.1)
11
+ activesupport (= 5.0.1)
12
+ activerecord (5.0.1)
13
+ activemodel (= 5.0.1)
14
+ activesupport (= 5.0.1)
15
+ arel (~> 7.0)
16
+ activesupport (5.0.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (~> 0.7)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ appraisal (2.1.0)
22
+ bundler
23
+ rake
24
+ thor (>= 0.14.0)
25
+ arel (7.1.4)
26
+ concurrent-ruby (1.0.4)
27
+ i18n (0.8.0)
28
+ minitest (5.10.1)
29
+ rake (12.0.0)
30
+ sqlite3 (1.3.13)
31
+ thor (0.19.4)
32
+ thread_safe (0.3.5)
33
+ tzinfo (1.2.2)
34
+ thread_safe (~> 0.1)
35
+
36
+ PLATFORMS
37
+ ruby
38
+
39
+ DEPENDENCIES
40
+ activerecord (= 5.0.1)
41
+ appraisal
42
+ bundler
43
+ minitest (>= 4.0)
44
+ sqlite3
45
+ store_base_sti_class!
46
+
47
+ BUNDLED WITH
48
+ 1.14.3
@@ -1,15 +1,13 @@
1
1
  require 'active_record'
2
2
 
3
- if ActiveRecord::VERSION::STRING =~ /^3\.0/
4
- require 'store_base_sti_class_for_3_0'
5
- elsif ActiveRecord::VERSION::STRING =~ /^3\.(1|2)/
6
- require 'store_base_sti_class_for_3_1_and_above'
7
- elsif ActiveRecord::VERSION::STRING =~ /^4\.0/
3
+ if ActiveRecord::VERSION::STRING =~ /^4\.0/
8
4
  require 'store_base_sti_class_for_4_0'
9
5
  elsif ActiveRecord::VERSION::STRING =~ /^4\.1/
10
6
  require 'store_base_sti_class_for_4_1'
11
7
  elsif ActiveRecord::VERSION::STRING =~ /^4\.2/
12
8
  require 'store_base_sti_class_for_4_2'
9
+ elsif ActiveRecord::VERSION::STRING =~ /^5\.0/
10
+ require 'store_base_sti_class_for_5_0'
13
11
  end
14
12
 
15
13
  module StoreBaseSTIClass
@@ -1,3 +1,3 @@
1
1
  module StoreBaseSTIClass
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -0,0 +1,404 @@
1
+ require 'active_record/associations/join_dependency/join_part'
2
+
3
+ if ActiveRecord::VERSION::STRING =~ /^5\.0/
4
+ module ActiveRecord
5
+
6
+ class Base
7
+ class_attribute :store_base_sti_class
8
+ self.store_base_sti_class = true
9
+ end
10
+
11
+ module Associations
12
+ class Association
13
+ private
14
+
15
+ def creation_attributes
16
+ attributes = {}
17
+
18
+ if (reflection.has_one? || reflection.collection?) && !options[:through]
19
+ attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
20
+
21
+ if reflection.options[:as]
22
+ # START PATCH
23
+ # original:
24
+ # attributes[reflection.type] = owner.class.base_class.name
25
+
26
+ attributes[reflection.type] = ActiveRecord::Base.store_base_sti_class ? owner.class.base_class.name : owner.class.name
27
+ # END PATCH
28
+ end
29
+ end
30
+
31
+ attributes
32
+ end
33
+ end
34
+
35
+ class JoinDependency # :nodoc:
36
+ class JoinAssociation < JoinPart # :nodoc:
37
+ def join_constraints(foreign_table, foreign_klass, node, join_type, tables, scope_chain, chain)
38
+ joins = []
39
+ binds = []
40
+ tables = tables.reverse
41
+
42
+ scope_chain_index = 0
43
+ scope_chain = scope_chain.reverse
44
+
45
+ # The chain starts with the target table, but we want to end with it here (makes
46
+ # more sense in this context), so we reverse
47
+ chain.reverse_each do |reflection|
48
+ table = tables.shift
49
+ klass = reflection.klass
50
+
51
+ join_keys = reflection.join_keys(klass)
52
+ key = join_keys.key
53
+ foreign_key = join_keys.foreign_key
54
+
55
+ constraint = build_constraint(klass, table, key, foreign_table, foreign_key)
56
+
57
+ predicate_builder = PredicateBuilder.new(TableMetadata.new(klass, table))
58
+ scope_chain_items = scope_chain[scope_chain_index].map do |item|
59
+ if item.is_a?(Relation)
60
+ item
61
+ else
62
+ ActiveRecord::Relation.create(klass, table, predicate_builder)
63
+ .instance_exec(node, &item)
64
+ end
65
+ end
66
+ scope_chain_index += 1
67
+
68
+ klass_scope =
69
+ if klass.current_scope
70
+ klass.current_scope.clone
71
+ else
72
+ relation = ActiveRecord::Relation.create(
73
+ klass,
74
+ table,
75
+ predicate_builder,
76
+ )
77
+ klass.send(:build_default_scope, relation)
78
+ end
79
+ scope_chain_items.concat [klass_scope].compact
80
+
81
+ rel = scope_chain_items.inject(scope_chain_items.shift) do |left, right|
82
+ left.merge right
83
+ end
84
+
85
+ if rel && !rel.arel.constraints.empty?
86
+ binds += rel.bound_attributes
87
+ constraint = constraint.and rel.arel.constraints
88
+ end
89
+
90
+ if reflection.type
91
+ # START PATCH
92
+ # original:
93
+ # value = foreign_klass.base_class.name
94
+ value = ActiveRecord::Base.store_base_sti_class ? foreign_klass.base_class.name : foreign_klass.name
95
+ # END PATCH
96
+ column = klass.columns_hash[reflection.type.to_s]
97
+
98
+ binds << Relation::QueryAttribute.new(column.name, value, klass.type_for_attribute(column.name))
99
+ constraint = constraint.and klass.arel_attribute(reflection.type, table).eq(Arel::Nodes::BindParam.new)
100
+ end
101
+
102
+ joins << table.create_join(table, table.create_on(constraint), join_type)
103
+
104
+ # The current table in this iteration becomes the foreign table in the next
105
+ foreign_table, foreign_klass = table, klass
106
+ end
107
+
108
+ JoinInformation.new joins, binds
109
+ end
110
+ end
111
+ end
112
+
113
+ class BelongsToPolymorphicAssociation
114
+ private
115
+
116
+ def replace_keys(record)
117
+ super
118
+
119
+ # START PATCH
120
+ # original:
121
+ # owner[reflection.foreign_type] = record.class.base_class.name
122
+
123
+ owner[reflection.foreign_type] = ActiveRecord::Base.store_base_sti_class ? record.class.base_class.name : record.class.name
124
+
125
+ # END PATCH
126
+ end
127
+ end
128
+
129
+ class Preloader
130
+ class Association
131
+ private
132
+
133
+ def build_scope
134
+ scope = klass.unscoped
135
+
136
+ values = reflection_scope.values
137
+ preload_values = preload_scope.values
138
+
139
+ scope.where_clause = reflection_scope.where_clause + preload_scope.where_clause
140
+ scope.references_values = Array(values[:references]) + Array(preload_values[:references])
141
+
142
+ if preload_values[:select] || values[:select]
143
+ scope._select!(preload_values[:select] || values[:select])
144
+ end
145
+ scope.includes! preload_values[:includes] || values[:includes]
146
+ if preload_scope.joins_values.any?
147
+ scope.joins!(preload_scope.joins_values)
148
+ else
149
+ scope.joins!(reflection_scope.joins_values)
150
+ end
151
+
152
+ if order_values = preload_values[:order] || values[:order]
153
+ scope.order!(order_values)
154
+ end
155
+
156
+ if preload_values[:reordering] || values[:reordering]
157
+ scope.reordering_value = true
158
+ end
159
+
160
+ if preload_values[:readonly] || values[:readonly]
161
+ scope.readonly!
162
+ end
163
+
164
+ if options[:as]
165
+ # START PATCH
166
+ # original:
167
+ # scope.where!(klass.table_name => { reflection.type => model.base_class.sti_name })
168
+
169
+ scope.where!(klass.table_name => { reflection.type => ActiveRecord::Base.store_base_sti_class ? model.base_class.sti_name : model.sti_name })
170
+
171
+ # END PATCH
172
+ end
173
+
174
+ scope.unscope_values = Array(values[:unscope]) + Array(preload_values[:unscope])
175
+ klass.default_scoped.merge(scope)
176
+ end
177
+ end
178
+
179
+ module ThroughAssociation
180
+ private
181
+
182
+ def through_scope
183
+ scope = through_reflection.klass.unscoped
184
+
185
+ if options[:source_type]
186
+ # BEGIN PATCH
187
+ # original: scope.where! reflection.foreign_type => options[:source_type]
188
+
189
+ adjusted_foreign_type =
190
+ if ActiveRecord::Base.store_base_sti_class
191
+ options[:source_type]
192
+ else
193
+ ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
194
+ end
195
+
196
+ scope.where! reflection.foreign_type => adjusted_foreign_type
197
+
198
+ # END PATCH
199
+ else
200
+ unless reflection_scope.where_clause.empty?
201
+ scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
202
+ scope.where_clause = reflection_scope.where_clause
203
+ end
204
+
205
+ scope.references! reflection_scope.values[:references]
206
+ if scope.eager_loading? && order_values = reflection_scope.values[:order]
207
+ scope = scope.order(order_values)
208
+ end
209
+ end
210
+
211
+ scope
212
+ end
213
+ end
214
+ end
215
+
216
+ class AssociationScope
217
+
218
+ def self.get_bind_values(owner, chain)
219
+ binds = []
220
+ last_reflection = chain.last
221
+
222
+ binds << last_reflection.join_id_for(owner)
223
+ if last_reflection.type
224
+ # START PATCH
225
+ # original: binds << owner.class.base_class.name
226
+ binds << (ActiveRecord::Base.store_base_sti_class ? owner.class.base_class.name : owner.class.name)
227
+ # END PATCH
228
+ end
229
+
230
+ chain.each_cons(2).each do |reflection, next_reflection|
231
+ if reflection.type
232
+ # START PATCH
233
+ # original: binds << next_reflection.klass.base_class.name
234
+ binds << (ActiveRecord::Base.store_base_sti_class ? next_reflection.klass.base_class.name : next_reflection.klass.name)
235
+ # END PATCH
236
+ end
237
+ end
238
+ binds
239
+ end
240
+
241
+ private
242
+
243
+ def next_chain_scope(scope, table, reflection, association_klass, foreign_table, next_reflection)
244
+ join_keys = reflection.join_keys(association_klass)
245
+ key = join_keys.key
246
+ foreign_key = join_keys.foreign_key
247
+
248
+ constraint = table[key].eq(foreign_table[foreign_key])
249
+
250
+ if reflection.type
251
+ # BEGIN PATCH
252
+ # original:
253
+ # value = transform_value(next_reflection.klass.base_class.name)
254
+ # scope = scope.where(table.name => { reflection.type => value })
255
+ if ActiveRecord::Base.store_base_sti_class
256
+ value = transform_value(next_reflection.klass.base_class.name)
257
+ else
258
+ klass = next_reflection.klass
259
+ value = ([klass] + klass.descendants).map(&:name)
260
+ end
261
+ scope = scope.where(table.name => { reflection.type => value })
262
+ # END PATCH
263
+ end
264
+
265
+ scope = scope.joins(join(foreign_table, constraint))
266
+ end
267
+
268
+ def last_chain_scope(scope, table, reflection, owner, association_klass)
269
+ join_keys = reflection.join_keys(association_klass)
270
+ key = join_keys.key
271
+ foreign_key = join_keys.foreign_key
272
+
273
+ value = transform_value(owner[foreign_key])
274
+ scope = scope.where(table.name => { key => value })
275
+
276
+ if reflection.type
277
+ # BEGIN PATCH
278
+ # polymorphic_type = transform_value(owner.class.base_class.name)
279
+ polymorphic_type = transform_value(ActiveRecord::Base.store_base_sti_class ? owner.class.base_class.name : owner.class.name)
280
+ # END PATCH
281
+ scope = scope.where(table.name => { reflection.type => polymorphic_type })
282
+ end
283
+
284
+ scope
285
+ end
286
+
287
+ end
288
+
289
+ module ThroughAssociation
290
+ private
291
+
292
+ def construct_join_attributes(*records)
293
+ ensure_mutable
294
+
295
+ if source_reflection.association_primary_key(reflection.klass) == reflection.klass.primary_key
296
+ join_attributes = { source_reflection.name => records }
297
+ else
298
+ join_attributes = {
299
+ source_reflection.foreign_key =>
300
+ records.map { |record|
301
+ record.send(source_reflection.association_primary_key(reflection.klass))
302
+ }
303
+ }
304
+ end
305
+
306
+ if options[:source_type]
307
+
308
+ # START PATCH
309
+ # original:
310
+ # join_attributes[source_reflection.foreign_type] =
311
+ # records.map { |record| record.class.base_class.name }
312
+
313
+ join_attributes[source_reflection.foreign_type] =
314
+ records.map { |record| ActiveRecord::Base.store_base_sti_class ? record.class.base_class.name : record.class.name }
315
+
316
+ # END PATCH
317
+ end
318
+
319
+ if records.count == 1
320
+ Hash[join_attributes.map { |k, v| [k, v.first] }]
321
+ else
322
+ join_attributes
323
+ end
324
+ end
325
+ end
326
+
327
+ class HasManyThroughAssociation
328
+ private
329
+
330
+ def build_through_record(record)
331
+ @through_records[record.object_id] ||= begin
332
+ ensure_mutable
333
+
334
+ through_record = through_association.build(*options_for_through_record)
335
+ through_record.send("#{source_reflection.name}=", record)
336
+
337
+ # START PATCH
338
+ if ActiveRecord::Base.store_base_sti_class
339
+ if options[:source_type]
340
+ through_record.send("#{source_reflection.foreign_type}=", options[:source_type])
341
+ end
342
+ end
343
+ # END PATCH
344
+
345
+ through_record
346
+ end
347
+ end
348
+ end
349
+ end
350
+
351
+ module Reflection
352
+ class PolymorphicReflection
353
+ def source_type_info
354
+ type = @previous_reflection.foreign_type
355
+ source_type = @previous_reflection.options[:source_type]
356
+
357
+ # START PATCH
358
+ adjusted_source_type =
359
+ if ActiveRecord::Base.store_base_sti_class
360
+ source_type
361
+ else
362
+ ([source_type.constantize] + source_type.constantize.descendants).map(&:to_s)
363
+ end
364
+ # END PATCH
365
+
366
+ lambda { |object| where(type => adjusted_source_type) }
367
+ end
368
+ end
369
+
370
+ class ThroughReflection
371
+ def scope_chain
372
+ @scope_chain ||= begin
373
+ scope_chain = source_reflection.scope_chain.map(&:dup)
374
+
375
+ # Add to it the scope from this reflection (if any)
376
+ scope_chain.first << scope if scope
377
+
378
+ through_scope_chain = through_reflection.scope_chain.map(&:dup)
379
+
380
+ if options[:source_type]
381
+ type = foreign_type
382
+ # START PATCH
383
+ # original: source_type = options[:source_type]
384
+ source_type =
385
+ if ActiveRecord::Base.store_base_sti_class
386
+ options[:source_type]
387
+ else
388
+ ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
389
+ end
390
+ # END PATCH
391
+ through_scope_chain.first << lambda { |object|
392
+ where(type => source_type)
393
+ }
394
+ end
395
+
396
+ # Recursively fill out the rest of the array from the through reflection
397
+ scope_chain + through_scope_chain
398
+ end
399
+ end
400
+ end
401
+ end
402
+ end
403
+
404
+ end