store_base_sti_class 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/Appraisals +42 -17
  2. data/Gemfile +1 -2
  3. data/Gemfile.lock +62 -26
  4. data/README.rdoc +7 -3
  5. data/Rakefile +2 -2
  6. data/VERSION +1 -1
  7. data/db/storebasestiname_unittest.sql +0 -0
  8. data/gemfiles/rails_3.0.10.gemfile +3 -4
  9. data/gemfiles/rails_3.0.10.gemfile.lock +40 -11
  10. data/gemfiles/rails_3.0.11.gemfile +3 -4
  11. data/gemfiles/rails_3.0.11.gemfile.lock +40 -11
  12. data/gemfiles/rails_3.0.12.gemfile +3 -4
  13. data/gemfiles/rails_3.0.12.gemfile.lock +40 -11
  14. data/gemfiles/rails_3.0.13.gemfile +10 -0
  15. data/gemfiles/rails_3.0.13.gemfile.lock +72 -0
  16. data/gemfiles/rails_3.0.14.gemfile +10 -0
  17. data/gemfiles/rails_3.0.14.gemfile.lock +72 -0
  18. data/gemfiles/rails_3.0.15.gemfile +10 -0
  19. data/gemfiles/rails_3.0.15.gemfile.lock +72 -0
  20. data/gemfiles/rails_3.0.16.gemfile +10 -0
  21. data/gemfiles/rails_3.0.16.gemfile.lock +72 -0
  22. data/gemfiles/rails_3.0.17.gemfile +10 -0
  23. data/gemfiles/rails_3.0.17.gemfile.lock +72 -0
  24. data/gemfiles/rails_3.0.18.gemfile +10 -0
  25. data/gemfiles/rails_3.0.18.gemfile.lock +72 -0
  26. data/gemfiles/rails_3.0.19.gemfile +10 -0
  27. data/gemfiles/rails_3.0.19.gemfile.lock +72 -0
  28. data/gemfiles/rails_3.0.5.gemfile +3 -4
  29. data/gemfiles/rails_3.0.5.gemfile.lock +41 -12
  30. data/gemfiles/rails_3.0.6.gemfile +3 -4
  31. data/gemfiles/rails_3.0.6.gemfile.lock +40 -11
  32. data/gemfiles/rails_3.0.7.gemfile +3 -4
  33. data/gemfiles/rails_3.0.7.gemfile.lock +40 -11
  34. data/gemfiles/rails_3.0.8.gemfile +3 -4
  35. data/gemfiles/rails_3.0.8.gemfile.lock +40 -11
  36. data/gemfiles/rails_3.0.9.gemfile +3 -4
  37. data/gemfiles/rails_3.0.9.gemfile.lock +40 -11
  38. data/gemfiles/rails_3.1.0.gemfile +3 -4
  39. data/gemfiles/rails_3.1.0.gemfile.lock +42 -14
  40. data/gemfiles/rails_3.1.10.gemfile +10 -0
  41. data/gemfiles/rails_3.1.10.gemfile.lock +43 -0
  42. data/gemfiles/rails_3.1.2.gemfile +3 -4
  43. data/gemfiles/rails_3.1.2.gemfile.lock +42 -14
  44. data/gemfiles/rails_3.1.3.gemfile +3 -4
  45. data/gemfiles/rails_3.1.3.gemfile.lock +42 -14
  46. data/gemfiles/rails_3.1.4.gemfile +3 -4
  47. data/gemfiles/rails_3.1.4.gemfile.lock +42 -14
  48. data/gemfiles/rails_3.1.5.gemfile +10 -0
  49. data/gemfiles/rails_3.1.5.gemfile.lock +43 -0
  50. data/gemfiles/rails_3.1.6.gemfile +10 -0
  51. data/gemfiles/rails_3.1.6.gemfile.lock +43 -0
  52. data/gemfiles/rails_3.1.7.gemfile +10 -0
  53. data/gemfiles/rails_3.1.7.gemfile.lock +43 -0
  54. data/gemfiles/rails_3.1.8.gemfile +10 -0
  55. data/gemfiles/rails_3.1.8.gemfile.lock +43 -0
  56. data/gemfiles/rails_3.1.9.gemfile +10 -0
  57. data/gemfiles/rails_3.1.9.gemfile.lock +43 -0
  58. data/gemfiles/rails_3.2.0.gemfile +3 -4
  59. data/gemfiles/rails_3.2.0.gemfile.lock +42 -14
  60. data/gemfiles/rails_3.2.1.gemfile +3 -4
  61. data/gemfiles/rails_3.2.1.gemfile.lock +42 -14
  62. data/gemfiles/rails_3.2.10.gemfile +10 -0
  63. data/gemfiles/rails_3.2.10.gemfile.lock +73 -0
  64. data/gemfiles/rails_3.2.11.gemfile +10 -0
  65. data/gemfiles/rails_3.2.11.gemfile.lock +73 -0
  66. data/gemfiles/rails_3.2.12.gemfile +10 -0
  67. data/gemfiles/rails_3.2.12.gemfile.lock +73 -0
  68. data/gemfiles/rails_3.2.13.gemfile +10 -0
  69. data/gemfiles/rails_3.2.13.gemfile.lock +73 -0
  70. data/gemfiles/rails_3.2.14.gemfile +10 -0
  71. data/gemfiles/rails_3.2.14.gemfile.lock +73 -0
  72. data/gemfiles/rails_3.2.2.gemfile +3 -4
  73. data/gemfiles/rails_3.2.2.gemfile.lock +42 -14
  74. data/gemfiles/rails_3.2.3.gemfile +3 -4
  75. data/gemfiles/rails_3.2.3.gemfile.lock +42 -14
  76. data/gemfiles/rails_3.2.4.gemfile +10 -0
  77. data/gemfiles/rails_3.2.4.gemfile.lock +73 -0
  78. data/gemfiles/rails_3.2.5.gemfile +10 -0
  79. data/gemfiles/rails_3.2.5.gemfile.lock +73 -0
  80. data/gemfiles/rails_3.2.6.gemfile +10 -0
  81. data/gemfiles/rails_3.2.6.gemfile.lock +73 -0
  82. data/gemfiles/rails_3.2.7.gemfile +10 -0
  83. data/gemfiles/rails_3.2.7.gemfile.lock +73 -0
  84. data/gemfiles/rails_3.2.8.gemfile +10 -0
  85. data/gemfiles/rails_3.2.8.gemfile.lock +73 -0
  86. data/gemfiles/rails_3.2.9.gemfile +10 -0
  87. data/gemfiles/rails_3.2.9.gemfile.lock +73 -0
  88. data/gemfiles/rails_4.0.0.gemfile +10 -0
  89. data/gemfiles/rails_4.0.0.gemfile.lock +81 -0
  90. data/gemfiles/rails_4.0.1.gemfile +10 -0
  91. data/gemfiles/rails_4.0.1.gemfile.lock +81 -0
  92. data/lib/store_base_sti_class.rb +2 -0
  93. data/lib/store_base_sti_class_for_4_0.rb +337 -0
  94. data/store_base_sti_class.gemspec +57 -13
  95. metadata +148 -114
  96. data/gemfiles/rails_3.0.3.gemfile +0 -11
  97. data/gemfiles/rails_3.0.3.gemfile.lock +0 -43
  98. data/gemfiles/rails_3.0.4.gemfile +0 -11
  99. data/gemfiles/rails_3.0.4.gemfile.lock +0 -43
  100. data/gemfiles/rails_3.1.1.gemfile +0 -11
  101. data/gemfiles/rails_3.1.1.gemfile.lock +0 -45
@@ -0,0 +1,73 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.8)
5
+ activesupport (= 3.2.8)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.8)
8
+ activemodel (= 3.2.8)
9
+ activesupport (= 3.2.8)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.8)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ addressable (2.3.5)
16
+ appraisal (0.5.2)
17
+ bundler
18
+ rake
19
+ arel (3.0.2)
20
+ builder (3.0.4)
21
+ faraday (0.8.8)
22
+ multipart-post (~> 1.2.0)
23
+ git (1.2.6)
24
+ github_api (0.10.1)
25
+ addressable
26
+ faraday (~> 0.8.1)
27
+ hashie (>= 1.2)
28
+ multi_json (~> 1.4)
29
+ nokogiri (~> 1.5.2)
30
+ oauth2
31
+ hashie (2.0.5)
32
+ highline (1.6.20)
33
+ httpauth (0.2.0)
34
+ i18n (0.6.5)
35
+ jeweler (1.8.8)
36
+ builder
37
+ bundler (~> 1.0)
38
+ git (>= 1.2.5)
39
+ github_api (= 0.10.1)
40
+ highline (>= 1.6.15)
41
+ nokogiri (= 1.5.10)
42
+ rake
43
+ rdoc
44
+ json (1.8.1)
45
+ jwt (0.1.8)
46
+ multi_json (>= 1.5)
47
+ multi_json (1.8.2)
48
+ multi_xml (0.5.5)
49
+ multipart-post (1.2.0)
50
+ nokogiri (1.5.10)
51
+ oauth2 (0.9.2)
52
+ faraday (~> 0.8)
53
+ httpauth (~> 0.2)
54
+ jwt (~> 0.1.4)
55
+ multi_json (~> 1.0)
56
+ multi_xml (~> 0.5)
57
+ rack (~> 1.2)
58
+ rack (1.5.2)
59
+ rake (10.1.0)
60
+ rdoc (4.0.1)
61
+ json (~> 1.4)
62
+ sqlite3 (1.3.8)
63
+ tzinfo (0.3.38)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ activerecord (= 3.2.8)
70
+ appraisal
71
+ bundler
72
+ jeweler
73
+ sqlite3
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", :group=>:development
6
+ gem "appraisal", :group=>:development
7
+ gem "jeweler", :group=>:development
8
+ gem "bundler", :group=>:development
9
+ gem "activerecord", "3.2.9"
10
+
@@ -0,0 +1,73 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activemodel (3.2.9)
5
+ activesupport (= 3.2.9)
6
+ builder (~> 3.0.0)
7
+ activerecord (3.2.9)
8
+ activemodel (= 3.2.9)
9
+ activesupport (= 3.2.9)
10
+ arel (~> 3.0.2)
11
+ tzinfo (~> 0.3.29)
12
+ activesupport (3.2.9)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ addressable (2.3.5)
16
+ appraisal (0.5.2)
17
+ bundler
18
+ rake
19
+ arel (3.0.2)
20
+ builder (3.0.4)
21
+ faraday (0.8.8)
22
+ multipart-post (~> 1.2.0)
23
+ git (1.2.6)
24
+ github_api (0.10.1)
25
+ addressable
26
+ faraday (~> 0.8.1)
27
+ hashie (>= 1.2)
28
+ multi_json (~> 1.4)
29
+ nokogiri (~> 1.5.2)
30
+ oauth2
31
+ hashie (2.0.5)
32
+ highline (1.6.20)
33
+ httpauth (0.2.0)
34
+ i18n (0.6.5)
35
+ jeweler (1.8.8)
36
+ builder
37
+ bundler (~> 1.0)
38
+ git (>= 1.2.5)
39
+ github_api (= 0.10.1)
40
+ highline (>= 1.6.15)
41
+ nokogiri (= 1.5.10)
42
+ rake
43
+ rdoc
44
+ json (1.8.1)
45
+ jwt (0.1.8)
46
+ multi_json (>= 1.5)
47
+ multi_json (1.8.2)
48
+ multi_xml (0.5.5)
49
+ multipart-post (1.2.0)
50
+ nokogiri (1.5.10)
51
+ oauth2 (0.9.2)
52
+ faraday (~> 0.8)
53
+ httpauth (~> 0.2)
54
+ jwt (~> 0.1.4)
55
+ multi_json (~> 1.0)
56
+ multi_xml (~> 0.5)
57
+ rack (~> 1.2)
58
+ rack (1.5.2)
59
+ rake (10.1.0)
60
+ rdoc (4.0.1)
61
+ json (~> 1.4)
62
+ sqlite3 (1.3.8)
63
+ tzinfo (0.3.38)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ activerecord (= 3.2.9)
70
+ appraisal
71
+ bundler
72
+ jeweler
73
+ sqlite3
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", :group=>:development
6
+ gem "appraisal", :group=>:development
7
+ gem "jeweler", :group=>:development
8
+ gem "bundler", :group=>:development
9
+ gem "activerecord", "4.0.0"
10
+
@@ -0,0 +1,81 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activemodel (4.0.0)
5
+ activesupport (= 4.0.0)
6
+ builder (~> 3.1.0)
7
+ activerecord (4.0.0)
8
+ activemodel (= 4.0.0)
9
+ activerecord-deprecated_finders (~> 1.0.2)
10
+ activesupport (= 4.0.0)
11
+ arel (~> 4.0.0)
12
+ activerecord-deprecated_finders (1.0.3)
13
+ activesupport (4.0.0)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ minitest (~> 4.2)
16
+ multi_json (~> 1.3)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 0.3.37)
19
+ addressable (2.3.5)
20
+ appraisal (0.5.2)
21
+ bundler
22
+ rake
23
+ arel (4.0.1)
24
+ atomic (1.1.14)
25
+ builder (3.1.4)
26
+ faraday (0.8.8)
27
+ multipart-post (~> 1.2.0)
28
+ git (1.2.6)
29
+ github_api (0.10.1)
30
+ addressable
31
+ faraday (~> 0.8.1)
32
+ hashie (>= 1.2)
33
+ multi_json (~> 1.4)
34
+ nokogiri (~> 1.5.2)
35
+ oauth2
36
+ hashie (2.0.5)
37
+ highline (1.6.20)
38
+ httpauth (0.2.0)
39
+ i18n (0.6.5)
40
+ jeweler (1.8.8)
41
+ builder
42
+ bundler (~> 1.0)
43
+ git (>= 1.2.5)
44
+ github_api (= 0.10.1)
45
+ highline (>= 1.6.15)
46
+ nokogiri (= 1.5.10)
47
+ rake
48
+ rdoc
49
+ json (1.8.1)
50
+ jwt (0.1.8)
51
+ multi_json (>= 1.5)
52
+ minitest (4.7.5)
53
+ multi_json (1.8.2)
54
+ multi_xml (0.5.5)
55
+ multipart-post (1.2.0)
56
+ nokogiri (1.5.10)
57
+ oauth2 (0.9.2)
58
+ faraday (~> 0.8)
59
+ httpauth (~> 0.2)
60
+ jwt (~> 0.1.4)
61
+ multi_json (~> 1.0)
62
+ multi_xml (~> 0.5)
63
+ rack (~> 1.2)
64
+ rack (1.5.2)
65
+ rake (10.1.0)
66
+ rdoc (4.0.1)
67
+ json (~> 1.4)
68
+ sqlite3 (1.3.8)
69
+ thread_safe (0.1.3)
70
+ atomic
71
+ tzinfo (0.3.38)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ activerecord (= 4.0.0)
78
+ appraisal
79
+ bundler
80
+ jeweler
81
+ sqlite3
@@ -0,0 +1,10 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3", :group=>:development
6
+ gem "appraisal", :group=>:development
7
+ gem "jeweler", :group=>:development
8
+ gem "bundler", :group=>:development
9
+ gem "activerecord", "4.0.1"
10
+
@@ -0,0 +1,81 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ activemodel (4.0.1)
5
+ activesupport (= 4.0.1)
6
+ builder (~> 3.1.0)
7
+ activerecord (4.0.1)
8
+ activemodel (= 4.0.1)
9
+ activerecord-deprecated_finders (~> 1.0.2)
10
+ activesupport (= 4.0.1)
11
+ arel (~> 4.0.0)
12
+ activerecord-deprecated_finders (1.0.3)
13
+ activesupport (4.0.1)
14
+ i18n (~> 0.6, >= 0.6.4)
15
+ minitest (~> 4.2)
16
+ multi_json (~> 1.3)
17
+ thread_safe (~> 0.1)
18
+ tzinfo (~> 0.3.37)
19
+ addressable (2.3.5)
20
+ appraisal (0.5.2)
21
+ bundler
22
+ rake
23
+ arel (4.0.1)
24
+ atomic (1.1.14)
25
+ builder (3.1.4)
26
+ faraday (0.8.8)
27
+ multipart-post (~> 1.2.0)
28
+ git (1.2.6)
29
+ github_api (0.10.1)
30
+ addressable
31
+ faraday (~> 0.8.1)
32
+ hashie (>= 1.2)
33
+ multi_json (~> 1.4)
34
+ nokogiri (~> 1.5.2)
35
+ oauth2
36
+ hashie (2.0.5)
37
+ highline (1.6.20)
38
+ httpauth (0.2.0)
39
+ i18n (0.6.5)
40
+ jeweler (1.8.8)
41
+ builder
42
+ bundler (~> 1.0)
43
+ git (>= 1.2.5)
44
+ github_api (= 0.10.1)
45
+ highline (>= 1.6.15)
46
+ nokogiri (= 1.5.10)
47
+ rake
48
+ rdoc
49
+ json (1.8.1)
50
+ jwt (0.1.8)
51
+ multi_json (>= 1.5)
52
+ minitest (4.7.5)
53
+ multi_json (1.8.2)
54
+ multi_xml (0.5.5)
55
+ multipart-post (1.2.0)
56
+ nokogiri (1.5.10)
57
+ oauth2 (0.9.2)
58
+ faraday (~> 0.8)
59
+ httpauth (~> 0.2)
60
+ jwt (~> 0.1.4)
61
+ multi_json (~> 1.0)
62
+ multi_xml (~> 0.5)
63
+ rack (~> 1.2)
64
+ rack (1.5.2)
65
+ rake (10.1.0)
66
+ rdoc (4.0.1)
67
+ json (~> 1.4)
68
+ sqlite3 (1.3.8)
69
+ thread_safe (0.1.3)
70
+ atomic
71
+ tzinfo (0.3.38)
72
+
73
+ PLATFORMS
74
+ ruby
75
+
76
+ DEPENDENCIES
77
+ activerecord (= 4.0.1)
78
+ appraisal
79
+ bundler
80
+ jeweler
81
+ sqlite3
@@ -4,4 +4,6 @@ if ActiveRecord::VERSION::STRING =~ /^3\.0/
4
4
  require 'store_base_sti_class_for_3_0'
5
5
  elsif ActiveRecord::VERSION::STRING =~ /^3\.(1|2)/
6
6
  require 'store_base_sti_class_for_3_1_and_above'
7
+ elsif ActiveRecord::VERSION::STRING =~ /^4/
8
+ require 'store_base_sti_class_for_4_0'
7
9
  end
@@ -0,0 +1,337 @@
1
+ require 'active_record'
2
+
3
+ if ActiveRecord::VERSION::STRING =~ /^4/
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
+
14
+ def creation_attributes
15
+ attributes = {}
16
+
17
+ if (reflection.macro == :has_one || reflection.macro == :has_many) && !options[:through]
18
+ attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
19
+
20
+ if reflection.options[:as]
21
+ # START PATCH
22
+ # original:
23
+ # attributes[reflection.type] = owner.class.base_class.name
24
+
25
+ attributes[reflection.type] = ActiveRecord::Base.store_base_sti_class ? owner.class.base_class.name : owner.class.name
26
+
27
+ # END PATCH
28
+ end
29
+ end
30
+
31
+ attributes
32
+ end
33
+
34
+ end
35
+
36
+ class JoinDependency # :nodoc:
37
+ class JoinAssociation < JoinPart # :nodoc:
38
+
39
+ def join_to(manager)
40
+ tables = @tables.dup
41
+ foreign_table = parent_table
42
+ foreign_klass = parent.base_klass
43
+
44
+ # The chain starts with the target table, but we want to end with it here (makes
45
+ # more sense in this context), so we reverse
46
+ chain.reverse.each_with_index do |reflection, i|
47
+ table = tables.shift
48
+
49
+ case reflection.source_macro
50
+ when :belongs_to
51
+ key = reflection.association_primary_key
52
+ foreign_key = reflection.foreign_key
53
+ when :has_and_belongs_to_many
54
+ # Join the join table first...
55
+ manager.from(join(
56
+ table,
57
+ table[reflection.foreign_key].
58
+ eq(foreign_table[reflection.active_record_primary_key])
59
+ ))
60
+
61
+ foreign_table, table = table, tables.shift
62
+
63
+ key = reflection.association_primary_key
64
+ foreign_key = reflection.association_foreign_key
65
+ else
66
+ key = reflection.foreign_key
67
+ foreign_key = reflection.active_record_primary_key
68
+ end
69
+
70
+ constraint = build_constraint(reflection, table, key, foreign_table, foreign_key)
71
+
72
+ scope_chain_items = scope_chain[i]
73
+
74
+ if reflection.type
75
+ # START PATCH
76
+ # original:
77
+ # scope_chain_items += [
78
+ # ActiveRecord::Relation.new(reflection.klass, table)
79
+ # .where(reflection.type => foreign_klass.base_class.name)
80
+ # ]
81
+
82
+ if ActiveRecord::Base.store_base_sti_class
83
+ scope_chain_items += [
84
+ ActiveRecord::Relation.new(reflection.klass, table)
85
+ .where(reflection.type => foreign_klass.base_class.name)
86
+ ]
87
+ else
88
+ scope_chain_items += [
89
+ ActiveRecord::Relation.new(reflection.klass, table)
90
+ .where(reflection.type => ([foreign_klass] + foreign_klass.descendants).map(&:name))
91
+ ]
92
+ end
93
+
94
+ # END PATCH
95
+ end
96
+
97
+ scope_chain_items += [reflection.klass.send(:build_default_scope)].compact
98
+
99
+ scope_chain_items.each do |item|
100
+ unless item.is_a?(Relation)
101
+ item = ActiveRecord::Relation.new(reflection.klass, table).instance_exec(self, &item)
102
+ end
103
+
104
+ constraint = constraint.and(item.arel.constraints) unless item.arel.constraints.empty?
105
+ end
106
+
107
+ manager.from(join(table, constraint))
108
+
109
+ # The current table in this iteration becomes the foreign table in the next
110
+ foreign_table, foreign_klass = table, reflection.klass
111
+ end
112
+
113
+ manager
114
+ end
115
+ end
116
+ end
117
+
118
+
119
+ class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
120
+
121
+ private
122
+
123
+ def replace_keys(record)
124
+ super
125
+
126
+ # START PATCH
127
+ # original: owner[reflection.foreign_type] = record && record.class.base_class.name
128
+
129
+ unless ActiveRecord::Base.store_base_sti_class
130
+ owner[reflection.foreign_type] = record && record.class.sti_name
131
+ else
132
+ owner[reflection.foreign_type] = record && record.class.base_class.name
133
+ end
134
+
135
+ #END PATCH
136
+ end
137
+ end
138
+ end
139
+
140
+ module Associations
141
+ class Preloader
142
+ class Association
143
+ private
144
+
145
+ def build_scope
146
+ scope = klass.unscoped
147
+ scope.default_scoped = true
148
+
149
+ values = reflection_scope.values
150
+ preload_values = preload_scope.values
151
+
152
+ scope.where_values = Array(values[:where]) + Array(preload_values[:where])
153
+ scope.references_values = Array(values[:references]) + Array(preload_values[:references])
154
+
155
+ scope.select! preload_values[:select] || values[:select] || table[Arel.star]
156
+ scope.includes! preload_values[:includes] || values[:includes]
157
+
158
+ if options[:as]
159
+ scope.where!(klass.table_name => {
160
+
161
+ #START PATCH
162
+ #original: reflection.type => model.base_class.sti_name
163
+
164
+ reflection.type => ActiveRecord::Base.store_base_sti_class ? model.base_class.sti_name : model.sti_name
165
+
166
+ #END PATCH
167
+ })
168
+ end
169
+
170
+ scope
171
+ end
172
+ end
173
+
174
+ module ThroughAssociation
175
+ def through_scope
176
+ through_scope = through_reflection.klass.unscoped
177
+
178
+ if options[:source_type]
179
+ #START PATCH
180
+ #original: through_scope.where! reflection.foreign_type => options[:source_type]
181
+
182
+ through_scope.where! reflection.foreign_type => ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s)
183
+
184
+ #END PATCH
185
+ else
186
+ unless reflection_scope.where_values.empty?
187
+ through_scope.includes_values = Array(reflection_scope.values[:includes] || options[:source])
188
+ through_scope.where_values = reflection_scope.values[:where]
189
+ end
190
+
191
+ through_scope.references! reflection_scope.values[:references]
192
+ through_scope.order! reflection_scope.values[:order] if through_scope.eager_loading?
193
+ end
194
+
195
+ through_scope
196
+ end
197
+ end
198
+ end
199
+
200
+ class AssociationScope
201
+ def add_constraints(scope)
202
+ tables = construct_tables
203
+
204
+ chain.each_with_index do |reflection, i|
205
+ table, foreign_table = tables.shift, tables.first
206
+
207
+ if reflection.source_macro == :has_and_belongs_to_many
208
+ join_table = tables.shift
209
+
210
+ scope = scope.joins(join(
211
+ join_table,
212
+ table[reflection.association_primary_key].
213
+ eq(join_table[reflection.association_foreign_key])
214
+ ))
215
+
216
+ table, foreign_table = join_table, tables.first
217
+ end
218
+
219
+ if reflection.source_macro == :belongs_to
220
+ if reflection.options[:polymorphic]
221
+ key = reflection.association_primary_key(self.klass)
222
+ else
223
+ key = reflection.association_primary_key
224
+ end
225
+
226
+ foreign_key = reflection.foreign_key
227
+ else
228
+ key = reflection.foreign_key
229
+ foreign_key = reflection.active_record_primary_key
230
+ end
231
+
232
+ if reflection == chain.last
233
+ bind_val = bind scope, table.table_name, key.to_s, owner[foreign_key]
234
+ scope = scope.where(table[key].eq(bind_val))
235
+
236
+ if reflection.type
237
+ # START PATCH
238
+ # original: value = owner.class.base_class.name
239
+
240
+ unless ActiveRecord::Base.store_base_sti_class
241
+ value = owner.class.name
242
+ else
243
+ value = owner.class.base_class.name
244
+ end
245
+
246
+ # END PATCH
247
+ bind_val = bind scope, table.table_name, reflection.type.to_s, value
248
+ scope = scope.where(table[reflection.type].eq(bind_val))
249
+ end
250
+ else
251
+ constraint = table[key].eq(foreign_table[foreign_key])
252
+
253
+ if reflection.type
254
+ # START PATCH
255
+ # original: type = chain[i + 1].klass.base_class.name
256
+ # constraint = constraint.and(table[reflection.type].eq(type))
257
+
258
+ if ActiveRecord::Base.store_base_sti_class
259
+ type = chain[i + 1].klass.base_class.name
260
+ constraint = constraint.and(table[reflection.type].eq(type))
261
+ else
262
+ klass = chain[i + 1].klass
263
+ constraint = constraint.and(table[reflection.type].in(([klass] + klass.descendants).map(&:name)))
264
+ end
265
+
266
+ # END PATCH
267
+ end
268
+
269
+ scope = scope.joins(join(foreign_table, constraint))
270
+ end
271
+
272
+ is_first_chain = i == 0
273
+ klass = is_first_chain ? self.klass : reflection.klass
274
+
275
+ # Exclude the scope of the association itself, because that
276
+ # was already merged in the #scope method.
277
+ scope_chain[i].each do |scope_chain_item|
278
+ item = eval_scope(klass, scope_chain_item)
279
+
280
+ if scope_chain_item == self.reflection.scope
281
+ scope.merge! item.except(:where, :includes)
282
+ end
283
+
284
+ if is_first_chain
285
+ scope.includes! item.includes_values
286
+ end
287
+
288
+ scope.where_values += item.where_values
289
+ scope.order_values |= item.order_values
290
+ end
291
+ end
292
+
293
+ scope
294
+ end
295
+
296
+ end
297
+ end
298
+
299
+ module Reflection
300
+ class ThroughReflection < AssociationReflection
301
+
302
+ def scope_chain
303
+ @scope_chain ||= begin
304
+ scope_chain = source_reflection.scope_chain.map(&:dup)
305
+
306
+ # Add to it the scope from this reflection (if any)
307
+ scope_chain.first << scope if scope
308
+
309
+ through_scope_chain = through_reflection.scope_chain.map(&:dup)
310
+
311
+ if options[:source_type]
312
+ # START PATCH
313
+ # original:
314
+ # through_scope_chain.first <<
315
+ # through_reflection.klass.where(foreign_type => options[:source_type])
316
+
317
+ unless ActiveRecord::Base.store_base_sti_class
318
+ through_scope_chain.first <<
319
+ through_reflection.klass.where(foreign_type => ([options[:source_type].constantize] + options[:source_type].constantize.descendants).map(&:to_s))
320
+ else
321
+ through_scope_chain.first <<
322
+ through_reflection.klass.where(foreign_type => options[:source_type])
323
+ end
324
+
325
+ # END PATCH
326
+ end
327
+
328
+ # Recursively fill out the rest of the array from the through reflection
329
+ scope_chain + through_scope_chain
330
+ end
331
+ end
332
+ end
333
+ end
334
+
335
+ end
336
+
337
+ end