store_base_sti_class 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/Appraisals +25 -0
  2. data/CHANGELOG +2 -0
  3. data/Gemfile +9 -0
  4. data/Gemfile.lock +45 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.rdoc +63 -0
  7. data/Rakefile +47 -0
  8. data/VERSION +1 -0
  9. data/db/storebasestiname_unittest.sql +0 -0
  10. data/gemfiles/rails_3.0.10.gemfile +11 -0
  11. data/gemfiles/rails_3.0.10.gemfile.lock +43 -0
  12. data/gemfiles/rails_3.0.11.gemfile +11 -0
  13. data/gemfiles/rails_3.0.11.gemfile.lock +43 -0
  14. data/gemfiles/rails_3.0.12.gemfile +11 -0
  15. data/gemfiles/rails_3.0.12.gemfile.lock +43 -0
  16. data/gemfiles/rails_3.0.3.gemfile +11 -0
  17. data/gemfiles/rails_3.0.3.gemfile.lock +43 -0
  18. data/gemfiles/rails_3.0.4.gemfile +11 -0
  19. data/gemfiles/rails_3.0.4.gemfile.lock +43 -0
  20. data/gemfiles/rails_3.0.5.gemfile +11 -0
  21. data/gemfiles/rails_3.0.5.gemfile.lock +43 -0
  22. data/gemfiles/rails_3.0.6.gemfile +11 -0
  23. data/gemfiles/rails_3.0.6.gemfile.lock +43 -0
  24. data/gemfiles/rails_3.0.7.gemfile +11 -0
  25. data/gemfiles/rails_3.0.7.gemfile.lock +43 -0
  26. data/gemfiles/rails_3.0.8.gemfile +11 -0
  27. data/gemfiles/rails_3.0.8.gemfile.lock +43 -0
  28. data/gemfiles/rails_3.0.9.gemfile +11 -0
  29. data/gemfiles/rails_3.0.9.gemfile.lock +43 -0
  30. data/gemfiles/rails_3.1.0.gemfile +11 -0
  31. data/gemfiles/rails_3.1.0.gemfile.lock +47 -0
  32. data/gemfiles/rails_3.1.1.gemfile +11 -0
  33. data/gemfiles/rails_3.1.1.gemfile.lock +45 -0
  34. data/gemfiles/rails_3.1.2.gemfile +11 -0
  35. data/gemfiles/rails_3.1.2.gemfile.lock +45 -0
  36. data/gemfiles/rails_3.1.3.gemfile +11 -0
  37. data/gemfiles/rails_3.1.3.gemfile.lock +45 -0
  38. data/gemfiles/rails_3.1.4.gemfile +11 -0
  39. data/gemfiles/rails_3.1.4.gemfile.lock +45 -0
  40. data/gemfiles/rails_3.2.0.gemfile +11 -0
  41. data/gemfiles/rails_3.2.0.gemfile.lock +45 -0
  42. data/gemfiles/rails_3.2.1.gemfile +11 -0
  43. data/gemfiles/rails_3.2.1.gemfile.lock +45 -0
  44. data/gemfiles/rails_3.2.2.gemfile +11 -0
  45. data/gemfiles/rails_3.2.2.gemfile.lock +45 -0
  46. data/gemfiles/rails_3.2.3.gemfile +11 -0
  47. data/gemfiles/rails_3.2.3.gemfile.lock +45 -0
  48. data/lib/store_base_sti_class.rb +7 -0
  49. data/lib/store_base_sti_class_for_3_0.rb +470 -0
  50. data/lib/store_base_sti_class_for_3_1_and_above.rb +304 -0
  51. data/store_base_sti_class.gemspec +111 -0
  52. data/storebasestiname_unittest.sql +0 -0
  53. data/test/connection.rb +22 -0
  54. data/test/helper.rb +63 -0
  55. data/test/models.rb +48 -0
  56. data/test/schema.rb +35 -0
  57. data/test/test_store_base_sti_class.rb +161 -0
  58. metadata +205 -0
@@ -0,0 +1,161 @@
1
+ require 'helper'
2
+ require 'active_record/test_case'
3
+
4
+ class TestStoreBaseStiClass < ActiveRecord::TestCase
5
+
6
+ def setup
7
+ @old_store_base_sti_class = ActiveRecord::Base.store_base_sti_class
8
+ ActiveRecord::Base.store_base_sti_class = false
9
+
10
+ @thinking_post = SpecialPost.create(:title => 'Thinking', :body => "the body")
11
+ @misc_tag = Tag.create(:name => 'Misc')
12
+ end
13
+
14
+ def teardown
15
+ ActiveRecord::Base.store_base_sti_class = @old_store_base_sti_class
16
+ end
17
+
18
+ def test_polymorphic_belongs_to_assignment_with_inheritance
19
+ # should update when assigning a saved record
20
+ tagging = Tagging.new
21
+ post = SpecialPost.create(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
22
+ tagging.taggable = post
23
+ assert_equal post.id, tagging.taggable_id
24
+ assert_equal "SpecialPost", tagging.taggable_type
25
+
26
+ # should update when assigning a new record
27
+ tagging = Tagging.new
28
+ post = SpecialPost.new(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
29
+ tagging.taggable = post
30
+ assert_nil tagging.taggable_id
31
+ assert_equal "SpecialPost", tagging.taggable_type
32
+ end
33
+
34
+ def test_polymorphic_has_many_create_model_with_inheritance
35
+ post = SpecialPost.new(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
36
+
37
+ tagging = @misc_tag.taggings.create(:taggable => post)
38
+ assert_equal "SpecialPost", tagging.taggable_type
39
+
40
+ post.reload
41
+ assert_equal [tagging], post.taggings
42
+ end
43
+
44
+ def test_polymorphic_has_one_create_model_with_inheritance
45
+ post = SpecialPost.new(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
46
+
47
+ tagging = @misc_tag.create_tagging(:taggable => post)
48
+ assert_equal "SpecialPost", tagging.taggable_type
49
+
50
+ post.reload
51
+ assert_equal tagging, post.tagging
52
+ end
53
+
54
+ def test_polymorphic_has_many_create_via_association
55
+ tag = SpecialTag.create!(:name => 'Special')
56
+ tagging = tag.polytaggings.create!
57
+
58
+ assert_equal "SpecialTag", tagging.polytag_type
59
+ end
60
+
61
+ def test_polymorphic_has_many_through_create_via_association
62
+ tag = SpecialTag.create!(:name => 'Special')
63
+ post = tag.polytagged_posts.create!(:title => 'To Be or Not To Be?', :body => "the body")
64
+
65
+ assert_equal "SpecialTag", tag.polytaggings.first.polytag_type
66
+ end
67
+
68
+ def test_include_polymorphic_has_one
69
+ post = SpecialPost.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
70
+ tagging = post.create_tagging(:tag => @misc_tag)
71
+
72
+ post = Post.find(post.id, :include => :tagging)
73
+ assert_equal tagging, assert_no_queries { post.tagging }
74
+ end
75
+
76
+ def test_include_polymorphic_has_many
77
+ tag = SpecialTag.create!(:name => 'Special')
78
+ tag.polytagged_posts << SpecialPost.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
79
+ tag.polytagged_posts << @thinking_post
80
+
81
+ tag = Tag.find(tag.id, :include => :polytaggings)
82
+ assert_equal 2, assert_no_queries { tag.polytaggings.length }
83
+ end
84
+
85
+ def test_include_polymorphic_has_many_through
86
+ tag = SpecialTag.create!(:name => 'Special')
87
+ tag.polytagged_posts << SpecialPost.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
88
+ tag.polytagged_posts << @thinking_post
89
+
90
+ tag = Tag.find(tag.id, :include => :polytagged_posts)
91
+ assert_equal 2, assert_no_queries { tag.polytagged_posts.length }
92
+ end
93
+
94
+ def test_join_polymorhic_has_many
95
+ tag = SpecialTag.create!(:name => 'Special')
96
+ tag.polytagged_posts << SpecialPost.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
97
+ tag.polytagged_posts << @thinking_post
98
+
99
+ assert Tag.find_by_id(tag.id, :joins => :polytaggings, :conditions => [ 'taggings.id = ?', tag.polytaggings.first.id ])
100
+ end
101
+
102
+ def test_join_polymorhic_has_many_through
103
+ tag = SpecialTag.create!(:name => 'Special')
104
+ tag.polytagged_posts << SpecialPost.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
105
+ tag.polytagged_posts << @thinking_post
106
+
107
+ assert Tag.find_by_id(tag.id, :joins => :polytagged_posts, :conditions => [ 'posts.id = ?', tag.polytaggings.first.taggable_id ])
108
+ end
109
+
110
+ def test_has_many_through_polymorphic_has_one
111
+ author = Author.create!(:name => 'Bob')
112
+ post = Post.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :author => author, :body => "the body")
113
+ special_post = SpecialPost.create!(:title => 'IBM Watson''s Jeopardy play', :author => author, :body => "the body")
114
+ special_tag = SpecialTag.create!(:name => 'SpecialGeneral')
115
+
116
+ taggings = [ post.taggings.create(:tag => special_tag), special_post.taggings.create(:tag => special_tag) ]
117
+ assert_equal taggings.sort_by(&:id), author.tagging.sort_by(&:id)
118
+ end
119
+
120
+ def test_has_many_polymorphic_with_source_type
121
+ tag = SpecialTag.create!(:name => 'Special')
122
+ tag.polytagged_posts << SpecialPost.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :body => "the body")
123
+ tag.polytagged_posts << @thinking_post
124
+
125
+ tag.save!
126
+ tag.reload
127
+
128
+ tag = Tag.find(tag.id)
129
+ assert_equal 2, tag.polytagged_posts.length
130
+ end
131
+
132
+ def test_polymorphic_has_many_through_with_double_sti_on_join_model
133
+ tag = SpecialTag.create!(:name => 'Special')
134
+ post = @thinking_post
135
+
136
+ tag.polytagged_posts << post
137
+
138
+
139
+ tag.reload
140
+
141
+ assert_equal 1, tag.polytaggings.length
142
+
143
+ tagging = tag.polytaggings.first
144
+
145
+ assert_equal 'SpecialTag', tagging.polytag_type
146
+ assert_equal 'SpecialPost', tagging.taggable_type
147
+
148
+ assert_equal tag, tagging.polytag
149
+ assert_equal post, tagging.taggable
150
+ end
151
+
152
+ def test_finder_sql_is_supported
153
+ author = Author.create!(:name => 'Bob')
154
+ post = Post.create!(:title => 'Budget Forecasts Bigger 2011 Deficit', :author => author, :body => "the body")
155
+ special_tag = Tag.create!(:name => 'SpecialGeneral')
156
+ post.taggings.create(:tag => special_tag)
157
+
158
+ assert_equal [author], special_tag.authors
159
+ end
160
+
161
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: store_base_sti_class
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Andrew Mutz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-09 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ type: :runtime
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ version_requirements: *id001
32
+ name: appraisal
33
+ prerelease: false
34
+ - !ruby/object:Gem::Dependency
35
+ type: :runtime
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
45
+ version_requirements: *id002
46
+ name: activerecord
47
+ prerelease: false
48
+ - !ruby/object:Gem::Dependency
49
+ type: :runtime
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ version_requirements: *id003
60
+ name: sqlite3
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ type: :development
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: 3
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ version_requirements: *id004
74
+ name: jeweler
75
+ prerelease: false
76
+ - !ruby/object:Gem::Dependency
77
+ type: :development
78
+ requirement: &id005 !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ version_requirements: *id005
88
+ name: rcov
89
+ prerelease: false
90
+ - !ruby/object:Gem::Dependency
91
+ type: :development
92
+ requirement: &id006 !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ version_requirements: *id006
102
+ name: bundler
103
+ prerelease: false
104
+ description: "\n ActiveRecord has always stored the base class in polymorphic _type columns when using STI. This can have non-trivial\n performance implications in certain cases. This gem adds 'store_base_sti_class' configuration options which controls\n whether ActiveRecord will store the base class or the actual class. Default to true for backwards compatibility.\n "
105
+ email: andrew.mutz@appfolio.com
106
+ executables: []
107
+
108
+ extensions: []
109
+
110
+ extra_rdoc_files:
111
+ - LICENSE.txt
112
+ - README.rdoc
113
+ files:
114
+ - Appraisals
115
+ - CHANGELOG
116
+ - Gemfile
117
+ - Gemfile.lock
118
+ - LICENSE.txt
119
+ - README.rdoc
120
+ - Rakefile
121
+ - VERSION
122
+ - db/storebasestiname_unittest.sql
123
+ - gemfiles/rails_3.0.10.gemfile
124
+ - gemfiles/rails_3.0.10.gemfile.lock
125
+ - gemfiles/rails_3.0.11.gemfile
126
+ - gemfiles/rails_3.0.11.gemfile.lock
127
+ - gemfiles/rails_3.0.12.gemfile
128
+ - gemfiles/rails_3.0.12.gemfile.lock
129
+ - gemfiles/rails_3.0.3.gemfile
130
+ - gemfiles/rails_3.0.3.gemfile.lock
131
+ - gemfiles/rails_3.0.4.gemfile
132
+ - gemfiles/rails_3.0.4.gemfile.lock
133
+ - gemfiles/rails_3.0.5.gemfile
134
+ - gemfiles/rails_3.0.5.gemfile.lock
135
+ - gemfiles/rails_3.0.6.gemfile
136
+ - gemfiles/rails_3.0.6.gemfile.lock
137
+ - gemfiles/rails_3.0.7.gemfile
138
+ - gemfiles/rails_3.0.7.gemfile.lock
139
+ - gemfiles/rails_3.0.8.gemfile
140
+ - gemfiles/rails_3.0.8.gemfile.lock
141
+ - gemfiles/rails_3.0.9.gemfile
142
+ - gemfiles/rails_3.0.9.gemfile.lock
143
+ - gemfiles/rails_3.1.0.gemfile
144
+ - gemfiles/rails_3.1.0.gemfile.lock
145
+ - gemfiles/rails_3.1.1.gemfile
146
+ - gemfiles/rails_3.1.1.gemfile.lock
147
+ - gemfiles/rails_3.1.2.gemfile
148
+ - gemfiles/rails_3.1.2.gemfile.lock
149
+ - gemfiles/rails_3.1.3.gemfile
150
+ - gemfiles/rails_3.1.3.gemfile.lock
151
+ - gemfiles/rails_3.1.4.gemfile
152
+ - gemfiles/rails_3.1.4.gemfile.lock
153
+ - gemfiles/rails_3.2.0.gemfile
154
+ - gemfiles/rails_3.2.0.gemfile.lock
155
+ - gemfiles/rails_3.2.1.gemfile
156
+ - gemfiles/rails_3.2.1.gemfile.lock
157
+ - gemfiles/rails_3.2.2.gemfile
158
+ - gemfiles/rails_3.2.2.gemfile.lock
159
+ - gemfiles/rails_3.2.3.gemfile
160
+ - gemfiles/rails_3.2.3.gemfile.lock
161
+ - lib/store_base_sti_class.rb
162
+ - lib/store_base_sti_class_for_3_0.rb
163
+ - lib/store_base_sti_class_for_3_1_and_above.rb
164
+ - store_base_sti_class.gemspec
165
+ - storebasestiname_unittest.sql
166
+ - test/connection.rb
167
+ - test/helper.rb
168
+ - test/models.rb
169
+ - test/schema.rb
170
+ - test/test_store_base_sti_class.rb
171
+ homepage: http://github.com/appfolio/store_base_sti_class
172
+ licenses:
173
+ - MIT
174
+ post_install_message:
175
+ rdoc_options: []
176
+
177
+ require_paths:
178
+ - lib
179
+ required_ruby_version: !ruby/object:Gem::Requirement
180
+ none: false
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ hash: 3
185
+ segments:
186
+ - 0
187
+ version: "0"
188
+ required_rubygems_version: !ruby/object:Gem::Requirement
189
+ none: false
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ hash: 3
194
+ segments:
195
+ - 0
196
+ version: "0"
197
+ requirements: []
198
+
199
+ rubyforge_project:
200
+ rubygems_version: 1.8.21
201
+ signing_key:
202
+ specification_version: 3
203
+ summary: Modifies ActiveRecord 3.0.5+ with the ability to store the actual class (instead of the base class) in polymorhic _type columns when using STI
204
+ test_files: []
205
+