activerecord 1.14.4 → 1.15.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (159) hide show
  1. data/CHANGELOG +400 -1
  2. data/README +2 -2
  3. data/RUNNING_UNIT_TESTS +21 -3
  4. data/Rakefile +55 -10
  5. data/lib/active_record.rb +10 -4
  6. data/lib/active_record/acts/list.rb +15 -4
  7. data/lib/active_record/acts/nested_set.rb +11 -12
  8. data/lib/active_record/acts/tree.rb +13 -14
  9. data/lib/active_record/aggregations.rb +46 -22
  10. data/lib/active_record/associations.rb +213 -162
  11. data/lib/active_record/associations/association_collection.rb +45 -15
  12. data/lib/active_record/associations/association_proxy.rb +32 -13
  13. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +18 -18
  14. data/lib/active_record/associations/has_many_association.rb +37 -17
  15. data/lib/active_record/associations/has_many_through_association.rb +120 -30
  16. data/lib/active_record/associations/has_one_association.rb +1 -1
  17. data/lib/active_record/attribute_methods.rb +75 -0
  18. data/lib/active_record/base.rb +282 -203
  19. data/lib/active_record/calculations.rb +95 -54
  20. data/lib/active_record/callbacks.rb +13 -24
  21. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +12 -1
  22. data/lib/active_record/connection_adapters/abstract/connection_specification.rb.rej +21 -0
  23. data/lib/active_record/connection_adapters/abstract/database_statements.rb +30 -4
  24. data/lib/active_record/connection_adapters/abstract/quoting.rb +16 -9
  25. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +121 -37
  26. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +55 -23
  27. data/lib/active_record/connection_adapters/abstract_adapter.rb +8 -0
  28. data/lib/active_record/connection_adapters/db2_adapter.rb +1 -11
  29. data/lib/active_record/connection_adapters/firebird_adapter.rb +364 -50
  30. data/lib/active_record/connection_adapters/frontbase_adapter.rb +861 -0
  31. data/lib/active_record/connection_adapters/mysql_adapter.rb +86 -33
  32. data/lib/active_record/connection_adapters/openbase_adapter.rb +4 -3
  33. data/lib/active_record/connection_adapters/oracle_adapter.rb +151 -127
  34. data/lib/active_record/connection_adapters/postgresql_adapter.rb +125 -48
  35. data/lib/active_record/connection_adapters/sqlite_adapter.rb +38 -10
  36. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +183 -155
  37. data/lib/active_record/connection_adapters/sybase_adapter.rb +190 -212
  38. data/lib/active_record/deprecated_associations.rb +24 -10
  39. data/lib/active_record/deprecated_finders.rb +4 -1
  40. data/lib/active_record/fixtures.rb +37 -23
  41. data/lib/active_record/locking/optimistic.rb +106 -0
  42. data/lib/active_record/locking/pessimistic.rb +77 -0
  43. data/lib/active_record/migration.rb +8 -5
  44. data/lib/active_record/observer.rb +73 -34
  45. data/lib/active_record/reflection.rb +21 -7
  46. data/lib/active_record/schema_dumper.rb +33 -5
  47. data/lib/active_record/timestamp.rb +23 -34
  48. data/lib/active_record/transactions.rb +37 -30
  49. data/lib/active_record/validations.rb +46 -30
  50. data/lib/active_record/vendor/mysql.rb +20 -5
  51. data/lib/active_record/version.rb +2 -2
  52. data/lib/active_record/wrappings.rb +1 -2
  53. data/lib/active_record/xml_serialization.rb +308 -0
  54. data/test/aaa_create_tables_test.rb +5 -1
  55. data/test/abstract_unit.rb +18 -8
  56. data/test/{active_schema_mysql.rb → active_schema_test_mysql.rb} +2 -2
  57. data/test/adapter_test.rb +9 -7
  58. data/test/adapter_test_sqlserver.rb +81 -0
  59. data/test/aggregations_test.rb +29 -0
  60. data/test/{association_callbacks_test.rb → associations/callbacks_test.rb} +10 -8
  61. data/test/{associations_cascaded_eager_loading_test.rb → associations/cascaded_eager_loading_test.rb} +35 -3
  62. data/test/{associations_go_eager_test.rb → associations/eager_test.rb} +36 -2
  63. data/test/{associations_extensions_test.rb → associations/extension_test.rb} +5 -0
  64. data/test/{associations_join_model_test.rb → associations/join_model_test.rb} +118 -8
  65. data/test/associations_test.rb +339 -45
  66. data/test/attribute_methods_test.rb +49 -0
  67. data/test/base_test.rb +321 -67
  68. data/test/calculations_test.rb +48 -10
  69. data/test/callbacks_test.rb +13 -0
  70. data/test/connection_test_firebird.rb +8 -0
  71. data/test/connections/native_db2/connection.rb +18 -17
  72. data/test/connections/native_firebird/connection.rb +19 -17
  73. data/test/connections/native_frontbase/connection.rb +27 -0
  74. data/test/connections/native_mysql/connection.rb +18 -15
  75. data/test/connections/native_openbase/connection.rb +14 -15
  76. data/test/connections/native_oracle/connection.rb +16 -12
  77. data/test/connections/native_postgresql/connection.rb +16 -17
  78. data/test/connections/native_sqlite/connection.rb +3 -6
  79. data/test/connections/native_sqlite3/connection.rb +3 -6
  80. data/test/connections/native_sqlserver/connection.rb +16 -17
  81. data/test/connections/native_sqlserver_odbc/connection.rb +18 -19
  82. data/test/connections/native_sybase/connection.rb +16 -17
  83. data/test/datatype_test_postgresql.rb +52 -0
  84. data/test/defaults_test.rb +52 -10
  85. data/test/deprecated_associations_test.rb +151 -107
  86. data/test/deprecated_finder_test.rb +83 -66
  87. data/test/empty_date_time_test.rb +25 -0
  88. data/test/finder_test.rb +118 -11
  89. data/test/fixtures/accounts.yml +6 -1
  90. data/test/fixtures/author.rb +27 -4
  91. data/test/fixtures/categorizations.yml +8 -2
  92. data/test/fixtures/category.rb +1 -2
  93. data/test/fixtures/comments.yml +0 -6
  94. data/test/fixtures/companies.yml +6 -1
  95. data/test/fixtures/company.rb +23 -1
  96. data/test/fixtures/company_in_module.rb +8 -10
  97. data/test/fixtures/customer.rb +2 -2
  98. data/test/fixtures/customers.yml +9 -0
  99. data/test/fixtures/db_definitions/db2.drop.sql +1 -0
  100. data/test/fixtures/db_definitions/db2.sql +9 -0
  101. data/test/fixtures/db_definitions/firebird.drop.sql +3 -0
  102. data/test/fixtures/db_definitions/firebird.sql +13 -1
  103. data/test/fixtures/db_definitions/frontbase.drop.sql +31 -0
  104. data/test/fixtures/db_definitions/frontbase.sql +262 -0
  105. data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
  106. data/test/fixtures/db_definitions/frontbase2.sql +4 -0
  107. data/test/fixtures/db_definitions/mysql.drop.sql +1 -0
  108. data/test/fixtures/db_definitions/mysql.sql +23 -14
  109. data/test/fixtures/db_definitions/openbase.sql +13 -1
  110. data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
  111. data/test/fixtures/db_definitions/oracle.sql +29 -2
  112. data/test/fixtures/db_definitions/postgresql.drop.sql +3 -1
  113. data/test/fixtures/db_definitions/postgresql.sql +13 -3
  114. data/test/fixtures/db_definitions/schema.rb +29 -1
  115. data/test/fixtures/db_definitions/sqlite.drop.sql +1 -0
  116. data/test/fixtures/db_definitions/sqlite.sql +12 -3
  117. data/test/fixtures/db_definitions/sqlserver.drop.sql +3 -0
  118. data/test/fixtures/db_definitions/sqlserver.sql +35 -0
  119. data/test/fixtures/db_definitions/sybase.drop.sql +2 -0
  120. data/test/fixtures/db_definitions/sybase.sql +13 -4
  121. data/test/fixtures/developer.rb +12 -0
  122. data/test/fixtures/edge.rb +5 -0
  123. data/test/fixtures/edges.yml +6 -0
  124. data/test/fixtures/funny_jokes.yml +3 -7
  125. data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
  126. data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
  127. data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
  128. data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
  129. data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
  130. data/test/fixtures/mixin.rb +15 -0
  131. data/test/fixtures/mixins.yml +38 -0
  132. data/test/fixtures/post.rb +3 -2
  133. data/test/fixtures/project.rb +3 -1
  134. data/test/fixtures/topic.rb +6 -1
  135. data/test/fixtures/topics.yml +4 -4
  136. data/test/fixtures/vertex.rb +9 -0
  137. data/test/fixtures/vertices.yml +4 -0
  138. data/test/fixtures_test.rb +45 -0
  139. data/test/inheritance_test.rb +67 -6
  140. data/test/lifecycle_test.rb +40 -19
  141. data/test/locking_test.rb +170 -26
  142. data/test/method_scoping_test.rb +2 -2
  143. data/test/migration_test.rb +387 -110
  144. data/test/migration_test_firebird.rb +124 -0
  145. data/test/mixin_nested_set_test.rb +14 -2
  146. data/test/mixin_test.rb +56 -18
  147. data/test/modules_test.rb +8 -2
  148. data/test/multiple_db_test.rb +2 -2
  149. data/test/pk_test.rb +1 -0
  150. data/test/reflection_test.rb +8 -2
  151. data/test/schema_authorization_test_postgresql.rb +75 -0
  152. data/test/schema_dumper_test.rb +40 -4
  153. data/test/table_name_test_sqlserver.rb +23 -0
  154. data/test/threaded_connections_test.rb +19 -16
  155. data/test/transactions_test.rb +86 -72
  156. data/test/validations_test.rb +126 -56
  157. data/test/xml_serialization_test.rb +125 -0
  158. metadata +45 -11
  159. data/lib/active_record/locking.rb +0 -79
@@ -0,0 +1,5 @@
1
+ # This class models an edge in a directed graph.
2
+ class Edge < ActiveRecord::Base
3
+ belongs_to :source, :class_name => 'Vertex', :foreign_key => 'source_id'
4
+ belongs_to :sink, :class_name => 'Vertex', :foreign_key => 'sink_id'
5
+ end
@@ -0,0 +1,6 @@
1
+ <% (1..4).each do |id| %>
2
+ edge_<%= id %>:
3
+ id: <%= id %>
4
+ source_id: <%= id %>
5
+ sink_id: <%= id + 1 %>
6
+ <% end %>
@@ -4,11 +4,7 @@ a_joke:
4
4
 
5
5
  another_joke:
6
6
  id: 2
7
- name: The Aristocrats
8
- a_joke:
9
- id: 1
10
- name: Knock knock
7
+ name: |
8
+ The \n Aristocrats
9
+ Ate the candy
11
10
 
12
- another_joke:
13
- id: 2
14
- name: The Aristocrats
@@ -0,0 +1,15 @@
1
+ class GiveMeBigNumbers < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :big_numbers do |table|
4
+ table.column :bank_balance, :decimal, :precision => 10, :scale => 2
5
+ table.column :big_bank_balance, :decimal, :precision => 15, :scale => 2
6
+ table.column :world_population, :decimal, :precision => 10
7
+ table.column :my_house_population, :decimal, :precision => 2
8
+ table.column :value_of_e, :decimal
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :big_numbers
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ class PeopleHaveMiddleNames < ActiveRecord::Migration
2
+ def self.up
3
+ add_column "people", "middle_name", :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column "people", "middle_name"
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class PeopleHaveLastNames < ActiveRecord::Migration
2
+ def self.up
3
+ add_column "people", "last_name", :string
4
+ end
5
+
6
+ def self.down
7
+ remove_column "people", "last_name"
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class WeNeedReminders < ActiveRecord::Migration
2
+ def self.up
3
+ create_table("reminders") do |t|
4
+ t.column :content, :text
5
+ t.column :remind_at, :datetime
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table "reminders"
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ class InnocentJointable < ActiveRecord::Migration
2
+ def self.up
3
+ create_table("people_reminders", :id => false) do |t|
4
+ t.column :reminder_id, :integer
5
+ t.column :person_id, :integer
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table "people_reminders"
11
+ end
12
+ end
@@ -10,6 +10,11 @@ class TreeMixinWithoutOrder < Mixin
10
10
  acts_as_tree :foreign_key => "parent_id"
11
11
  end
12
12
 
13
+ class RecursivelyCascadedTreeMixin < Mixin
14
+ acts_as_tree :foreign_key => "parent_id"
15
+ has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id
16
+ end
17
+
13
18
  class ListMixin < Mixin
14
19
  acts_as_list :column => "pos", :scope => :parent
15
20
 
@@ -46,3 +51,13 @@ class NestedSetWithSymbolScope < Mixin
46
51
 
47
52
  def self.table_name() "mixins" end
48
53
  end
54
+
55
+ class NestedSetSuperclass < Mixin
56
+ acts_as_nested_set :scope => :root
57
+
58
+ def self.table_name() "mixins" end
59
+ end
60
+
61
+ class NestedSetSubclass < NestedSetSuperclass
62
+
63
+ end
@@ -39,6 +39,26 @@ tree_without_order_2:
39
39
  type: TreeMixinWithoutOrder
40
40
  parent_id:
41
41
 
42
+ recursively_cascaded_tree_1:
43
+ id: 5005
44
+ type: RecursivelyCascadedTreeMixin
45
+ parent_id:
46
+
47
+ recursively_cascaded_tree_2:
48
+ id: 5006
49
+ type: RecursivelyCascadedTreeMixin
50
+ parent_id: 5005
51
+
52
+ recursively_cascaded_tree_3:
53
+ id: 5007
54
+ type: RecursivelyCascadedTreeMixin
55
+ parent_id: 5006
56
+
57
+ recursively_cascaded_tree_4:
58
+ id: 5008
59
+ type: RecursivelyCascadedTreeMixin
60
+ parent_id: 5007
61
+
42
62
  # List mixins
43
63
 
44
64
  <% (1..4).each do |counter| %>
@@ -57,6 +77,24 @@ set_<%= counter %>:
57
77
  type: NestedSet
58
78
  <% end %>
59
79
 
80
+ # Nested set with STI
81
+ <%
82
+ [ [3100, 0, 1, 10, "NestedSetSuperclass"],
83
+ [3101, 3100, 2, 5, "NestedSetSubclass"],
84
+ [3102, 3101, 3, 4, "NestedSetSuperclass"],
85
+ [3103, 3100, 6, 9, "NestedSetSuperclass"],
86
+ [3104, 3103, 7, 8, "NestedSetSubclass"]
87
+ ].each do |sti| %>
88
+ sti_set_<%= sti[0] %>:
89
+ id: <%= sti[0] %>
90
+ parent_id: <%= sti[1] %>
91
+ lft: <%= sti[2] %>
92
+ rgt: <%= sti[3] %>
93
+ type: <%= sti[4] %>
94
+ root_id: 3100
95
+
96
+ <% end %>
97
+
60
98
  # Big old set
61
99
  <%
62
100
  [[4001, 0, 1, 20],
@@ -5,7 +5,7 @@ class Post < ActiveRecord::Base
5
5
  end
6
6
  end
7
7
 
8
- belongs_to :author_with_posts, :class_name => "Author", :include => :posts
8
+ belongs_to :author_with_posts, :class_name => "Author", :foreign_key => :author_id, :include => :posts
9
9
 
10
10
  has_many :comments, :order => "body" do
11
11
  def find_most_recent
@@ -46,7 +46,7 @@ class Post < ActiveRecord::Base
46
46
  end
47
47
  end
48
48
 
49
- class SpecialPost < Post; end;
49
+ class SpecialPost < Post; end
50
50
 
51
51
  class StiPost < Post
52
52
  self.abstract_class = true
@@ -54,4 +54,5 @@ class StiPost < Post
54
54
  end
55
55
 
56
56
  class SubStiPost < StiPost
57
+ self.table_name = Post.table_name
57
58
  end
@@ -1,7 +1,9 @@
1
1
  class Project < ActiveRecord::Base
2
- has_and_belongs_to_many :developers, :uniq => true
2
+ has_and_belongs_to_many :developers, :uniq => true, :order => 'developers.name desc, developers.id desc'
3
+ has_and_belongs_to_many :non_unique_developers, :order => 'developers.name desc, developers.id desc', :class_name => 'Developer'
3
4
  has_and_belongs_to_many :limited_developers, :class_name => "Developer", :limit => 1
4
5
  has_and_belongs_to_many :developers_named_david, :class_name => "Developer", :conditions => "name = 'David'", :uniq => true
6
+ has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true
5
7
  has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0"
6
8
  has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id}'
7
9
  has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}"
@@ -9,6 +9,11 @@ class Topic < ActiveRecord::Base
9
9
  Topic.find(parent_id)
10
10
  end
11
11
 
12
+ # trivial method for testing Array#to_xml with :methods
13
+ def topic_id
14
+ id
15
+ end
16
+
12
17
  protected
13
18
  def default_written_on
14
19
  self.written_on = Time.now unless attribute_present?("written_on")
@@ -17,4 +22,4 @@ class Topic < ActiveRecord::Base
17
22
  def destroy_children
18
23
  self.class.delete_all "parent_id = #{id}"
19
24
  end
20
- end
25
+ end
@@ -3,20 +3,20 @@ first:
3
3
  title: The First Topic
4
4
  author_name: David
5
5
  author_email_address: david@loudthinking.com
6
- written_on: 2003-07-16t15:28:00.00+01:00
6
+ written_on: 2003-07-16t15:28:11.2233+01:00
7
7
  last_read: 2004-04-15
8
8
  bonus_time: 2005-01-30t15:28:00.00+01:00
9
9
  content: Have a nice day
10
10
  approved: false
11
- replies_count: 0
11
+ replies_count: 1
12
12
 
13
13
  second:
14
14
  id: 2
15
15
  title: The Second Topic's of the day
16
16
  author_name: Mary
17
- written_on: 2003-07-15t15:28:00.00+01:00
17
+ written_on: 2003-07-15t15:28:00.0099+01:00
18
18
  content: Have a nice day
19
19
  approved: true
20
- replies_count: 2
20
+ replies_count: 0
21
21
  parent_id: 1
22
22
  type: Reply
@@ -0,0 +1,9 @@
1
+ # This class models a vertex in a directed graph.
2
+ class Vertex < ActiveRecord::Base
3
+ has_many :sink_edges, :class_name => 'Edge', :foreign_key => 'source_id'
4
+ has_many :sinks, :through => :sink_edges, :source => :sink
5
+
6
+ has_and_belongs_to_many :sources,
7
+ :class_name => 'Vertex', :join_table => 'edges',
8
+ :foreign_key => 'sink_id', :association_foreign_key => 'source_id'
9
+ end
@@ -0,0 +1,4 @@
1
+ <% (1..5).each do |id| %>
2
+ vertex_<%= id %>:
3
+ id: <%= id %>
4
+ <% end %>
@@ -343,3 +343,48 @@ class InvalidTableNameFixturesTest < Test::Unit::TestCase
343
343
  end
344
344
  end
345
345
  end
346
+
347
+ class CheckEscapedYamlFixturesTest < Test::Unit::TestCase
348
+ set_fixture_class :funny_jokes => 'Joke'
349
+ fixtures :funny_jokes
350
+
351
+ def test_proper_escaped_fixture
352
+ assert_equal "The \\n Aristocrats\nAte the candy\n", funny_jokes(:another_joke).name
353
+ end
354
+ end
355
+
356
+ class DevelopersProject; end;
357
+
358
+ class ManyToManyFixturesWithClassDefined < Test::Unit::TestCase
359
+ fixtures :developers_projects
360
+
361
+ def test_this_should_run_cleanly
362
+ assert true
363
+ end
364
+ end
365
+
366
+
367
+ class FixturesBrokenRollbackTest < Test::Unit::TestCase
368
+ def blank_setup; end
369
+ alias_method :ar_setup_with_fixtures, :setup_with_fixtures
370
+ alias_method :setup_with_fixtures, :blank_setup
371
+ alias_method :setup, :blank_setup
372
+
373
+ def blank_teardown; end
374
+ alias_method :ar_teardown_with_fixtures, :teardown_with_fixtures
375
+ alias_method :teardown_with_fixtures, :blank_teardown
376
+ alias_method :teardown, :blank_teardown
377
+
378
+ def test_no_rollback_in_teardown_unless_transaction_active
379
+ assert_equal 0, Thread.current['open_transactions']
380
+ assert_raise(RuntimeError) { ar_setup_with_fixtures }
381
+ assert_equal 0, Thread.current['open_transactions']
382
+ assert_nothing_raised { ar_teardown_with_fixtures }
383
+ assert_equal 0, Thread.current['open_transactions']
384
+ end
385
+
386
+ private
387
+ def load_fixtures
388
+ raise 'argh'
389
+ end
390
+ end
@@ -4,17 +4,17 @@ require 'fixtures/project'
4
4
  require 'fixtures/subscriber'
5
5
 
6
6
  class InheritanceTest < Test::Unit::TestCase
7
- fixtures :companies, :projects, :subscribers
7
+ fixtures :companies, :projects, :subscribers, :accounts
8
8
 
9
9
  def test_a_bad_type_column
10
10
  #SQLServer need to turn Identity Insert On before manually inserting into the Identity column
11
- if current_adapter?(:SQLServerAdapter) || current_adapter?(:SybaseAdapter)
11
+ if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
12
12
  Company.connection.execute "SET IDENTITY_INSERT companies ON"
13
13
  end
14
14
  Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')"
15
15
 
16
16
  #We then need to turn it back Off before continuing.
17
- if current_adapter?(:SQLServerAdapter) || current_adapter?(:SybaseAdapter)
17
+ if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
18
18
  Company.connection.execute "SET IDENTITY_INSERT companies OFF"
19
19
  end
20
20
  assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) }
@@ -30,6 +30,7 @@ class InheritanceTest < Test::Unit::TestCase
30
30
  def test_alt_inheritance_find
31
31
  switch_to_alt_inheritance_column
32
32
  test_inheritance_find
33
+ switch_to_default_inheritance_column
33
34
  end
34
35
 
35
36
  def test_inheritance_find_all
@@ -41,6 +42,7 @@ class InheritanceTest < Test::Unit::TestCase
41
42
  def test_alt_inheritance_find_all
42
43
  switch_to_alt_inheritance_column
43
44
  test_inheritance_find_all
45
+ switch_to_default_inheritance_column
44
46
  end
45
47
 
46
48
  def test_inheritance_save
@@ -55,10 +57,11 @@ class InheritanceTest < Test::Unit::TestCase
55
57
  def test_alt_inheritance_save
56
58
  switch_to_alt_inheritance_column
57
59
  test_inheritance_save
60
+ switch_to_default_inheritance_column
58
61
  end
59
62
 
60
63
  def test_inheritance_condition
61
- assert_equal 8, Company.count
64
+ assert_equal 9, Company.count
62
65
  assert_equal 2, Firm.count
63
66
  assert_equal 3, Client.count
64
67
  end
@@ -66,6 +69,7 @@ class InheritanceTest < Test::Unit::TestCase
66
69
  def test_alt_inheritance_condition
67
70
  switch_to_alt_inheritance_column
68
71
  test_inheritance_condition
72
+ switch_to_default_inheritance_column
69
73
  end
70
74
 
71
75
  def test_finding_incorrect_type_data
@@ -76,6 +80,7 @@ class InheritanceTest < Test::Unit::TestCase
76
80
  def test_alt_finding_incorrect_type_data
77
81
  switch_to_alt_inheritance_column
78
82
  test_finding_incorrect_type_data
83
+ switch_to_default_inheritance_column
79
84
  end
80
85
 
81
86
  def test_update_all_within_inheritance
@@ -87,6 +92,7 @@ class InheritanceTest < Test::Unit::TestCase
87
92
  def test_alt_update_all_within_inheritance
88
93
  switch_to_alt_inheritance_column
89
94
  test_update_all_within_inheritance
95
+ switch_to_default_inheritance_column
90
96
  end
91
97
 
92
98
  def test_destroy_all_within_inheritance
@@ -98,6 +104,7 @@ class InheritanceTest < Test::Unit::TestCase
98
104
  def test_alt_destroy_all_within_inheritance
99
105
  switch_to_alt_inheritance_column
100
106
  test_destroy_all_within_inheritance
107
+ switch_to_default_inheritance_column
101
108
  end
102
109
 
103
110
  def test_find_first_within_inheritance
@@ -109,6 +116,7 @@ class InheritanceTest < Test::Unit::TestCase
109
116
  def test_alt_find_first_within_inheritance
110
117
  switch_to_alt_inheritance_column
111
118
  test_find_first_within_inheritance
119
+ switch_to_default_inheritance_column
112
120
  end
113
121
 
114
122
  def test_complex_inheritance
@@ -124,6 +132,19 @@ class InheritanceTest < Test::Unit::TestCase
124
132
  def test_alt_complex_inheritance
125
133
  switch_to_alt_inheritance_column
126
134
  test_complex_inheritance
135
+ switch_to_default_inheritance_column
136
+ end
137
+
138
+ def test_eager_load_belongs_to_something_inherited
139
+ account = Account.find(1, :include => :firm)
140
+ assert_not_nil account.instance_variable_get("@firm"), "nil proves eager load failed"
141
+ end
142
+
143
+ def test_alt_eager_loading
144
+ switch_to_alt_inheritance_column
145
+ test_eager_load_belongs_to_something_inherited
146
+ switch_to_default_inheritance_column
147
+ ActiveRecord::Base.logger.debug "cocksucker"
127
148
  end
128
149
 
129
150
  def test_inheritance_without_mapping
@@ -138,7 +159,47 @@ class InheritanceTest < Test::Unit::TestCase
138
159
  c['type'] = nil
139
160
  c.save
140
161
  end
141
-
142
- def Company.inheritance_column() "ruby_type" end
162
+ [ Company, Firm, Client].each { |klass| klass.reset_column_information }
163
+ Company.set_inheritance_column('ruby_type')
164
+ end
165
+ def switch_to_default_inheritance_column
166
+ [ Company, Firm, Client].each { |klass| klass.reset_column_information }
167
+ Company.set_inheritance_column('type')
143
168
  end
144
169
  end
170
+
171
+
172
+ class InheritanceComputeTypeTest < Test::Unit::TestCase
173
+ fixtures :companies
174
+
175
+ def setup
176
+ Dependencies.log_activity = true
177
+ end
178
+
179
+ def teardown
180
+ Dependencies.log_activity = false
181
+ self.class.const_remove :FirmOnTheFly rescue nil
182
+ Firm.const_remove :FirmOnTheFly rescue nil
183
+ end
184
+
185
+ def test_instantiation_doesnt_try_to_require_corresponding_file
186
+ foo = Firm.find(:first).clone
187
+ foo.ruby_type = foo.type = 'FirmOnTheFly'
188
+ foo.save!
189
+
190
+ # Should fail without FirmOnTheFly in the type condition.
191
+ assert_raise(ActiveRecord::RecordNotFound) { Firm.find(foo.id) }
192
+
193
+ # Nest FirmOnTheFly in the test case where Dependencies won't see it.
194
+ self.class.const_set :FirmOnTheFly, Class.new(Firm)
195
+ assert_raise(ActiveRecord::SubclassNotFound) { Firm.find(foo.id) }
196
+
197
+ # Nest FirmOnTheFly in Firm where Dependencies will see it.
198
+ # This is analogous to nesting models in a migration.
199
+ Firm.const_set :FirmOnTheFly, Class.new(Firm)
200
+
201
+ # And instantiate will find the existing constant rather than trying
202
+ # to require firm_on_the_fly.
203
+ assert_nothing_raised { assert_kind_of Firm::FirmOnTheFly, Firm.find(foo.id) }
204
+ end
205
+ end