activerecord 1.0.0 → 4.0.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 (255) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2102 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +213 -0
  5. data/examples/performance.rb +172 -0
  6. data/examples/simple.rb +14 -0
  7. data/lib/active_record/aggregations.rb +180 -84
  8. data/lib/active_record/associations/alias_tracker.rb +76 -0
  9. data/lib/active_record/associations/association.rb +248 -0
  10. data/lib/active_record/associations/association_scope.rb +135 -0
  11. data/lib/active_record/associations/belongs_to_association.rb +92 -0
  12. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +35 -0
  13. data/lib/active_record/associations/builder/association.rb +108 -0
  14. data/lib/active_record/associations/builder/belongs_to.rb +98 -0
  15. data/lib/active_record/associations/builder/collection_association.rb +89 -0
  16. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
  17. data/lib/active_record/associations/builder/has_many.rb +15 -0
  18. data/lib/active_record/associations/builder/has_one.rb +25 -0
  19. data/lib/active_record/associations/builder/singular_association.rb +32 -0
  20. data/lib/active_record/associations/collection_association.rb +608 -0
  21. data/lib/active_record/associations/collection_proxy.rb +986 -0
  22. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +58 -39
  23. data/lib/active_record/associations/has_many_association.rb +116 -85
  24. data/lib/active_record/associations/has_many_through_association.rb +197 -0
  25. data/lib/active_record/associations/has_one_association.rb +102 -0
  26. data/lib/active_record/associations/has_one_through_association.rb +36 -0
  27. data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
  28. data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
  29. data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
  30. data/lib/active_record/associations/join_dependency.rb +235 -0
  31. data/lib/active_record/associations/join_helper.rb +45 -0
  32. data/lib/active_record/associations/preloader/association.rb +121 -0
  33. data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
  34. data/lib/active_record/associations/preloader/collection_association.rb +24 -0
  35. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
  36. data/lib/active_record/associations/preloader/has_many.rb +17 -0
  37. data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
  38. data/lib/active_record/associations/preloader/has_one.rb +23 -0
  39. data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
  40. data/lib/active_record/associations/preloader/singular_association.rb +21 -0
  41. data/lib/active_record/associations/preloader/through_association.rb +63 -0
  42. data/lib/active_record/associations/preloader.rb +178 -0
  43. data/lib/active_record/associations/singular_association.rb +64 -0
  44. data/lib/active_record/associations/through_association.rb +87 -0
  45. data/lib/active_record/associations.rb +1437 -431
  46. data/lib/active_record/attribute_assignment.rb +201 -0
  47. data/lib/active_record/attribute_methods/before_type_cast.rb +70 -0
  48. data/lib/active_record/attribute_methods/dirty.rb +118 -0
  49. data/lib/active_record/attribute_methods/primary_key.rb +122 -0
  50. data/lib/active_record/attribute_methods/query.rb +40 -0
  51. data/lib/active_record/attribute_methods/read.rb +107 -0
  52. data/lib/active_record/attribute_methods/serialization.rb +162 -0
  53. data/lib/active_record/attribute_methods/time_zone_conversion.rb +59 -0
  54. data/lib/active_record/attribute_methods/write.rb +63 -0
  55. data/lib/active_record/attribute_methods.rb +393 -0
  56. data/lib/active_record/autosave_association.rb +426 -0
  57. data/lib/active_record/base.rb +268 -930
  58. data/lib/active_record/callbacks.rb +203 -230
  59. data/lib/active_record/coders/yaml_column.rb +38 -0
  60. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +638 -0
  61. data/lib/active_record/connection_adapters/abstract/database_limits.rb +67 -0
  62. data/lib/active_record/connection_adapters/abstract/database_statements.rb +390 -0
  63. data/lib/active_record/connection_adapters/abstract/query_cache.rb +95 -0
  64. data/lib/active_record/connection_adapters/abstract/quoting.rb +129 -0
  65. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +501 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
  67. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +873 -0
  68. data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
  69. data/lib/active_record/connection_adapters/abstract_adapter.rb +389 -275
  70. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
  71. data/lib/active_record/connection_adapters/column.rb +318 -0
  72. data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
  73. data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
  74. data/lib/active_record/connection_adapters/mysql_adapter.rb +517 -90
  75. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
  76. data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
  77. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
  78. data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
  79. data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
  80. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  81. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
  82. data/lib/active_record/connection_adapters/postgresql_adapter.rb +911 -138
  83. data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
  84. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +624 -0
  85. data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
  86. data/lib/active_record/connection_handling.rb +98 -0
  87. data/lib/active_record/core.rb +463 -0
  88. data/lib/active_record/counter_cache.rb +122 -0
  89. data/lib/active_record/dynamic_matchers.rb +131 -0
  90. data/lib/active_record/errors.rb +213 -0
  91. data/lib/active_record/explain.rb +38 -0
  92. data/lib/active_record/explain_registry.rb +30 -0
  93. data/lib/active_record/explain_subscriber.rb +29 -0
  94. data/lib/active_record/fixture_set/file.rb +55 -0
  95. data/lib/active_record/fixtures.rb +892 -138
  96. data/lib/active_record/inheritance.rb +200 -0
  97. data/lib/active_record/integration.rb +60 -0
  98. data/lib/active_record/locale/en.yml +47 -0
  99. data/lib/active_record/locking/optimistic.rb +181 -0
  100. data/lib/active_record/locking/pessimistic.rb +77 -0
  101. data/lib/active_record/log_subscriber.rb +82 -0
  102. data/lib/active_record/migration/command_recorder.rb +164 -0
  103. data/lib/active_record/migration/join_table.rb +15 -0
  104. data/lib/active_record/migration.rb +1015 -0
  105. data/lib/active_record/model_schema.rb +345 -0
  106. data/lib/active_record/nested_attributes.rb +546 -0
  107. data/lib/active_record/null_relation.rb +65 -0
  108. data/lib/active_record/persistence.rb +509 -0
  109. data/lib/active_record/query_cache.rb +56 -0
  110. data/lib/active_record/querying.rb +62 -0
  111. data/lib/active_record/railtie.rb +205 -0
  112. data/lib/active_record/railties/console_sandbox.rb +5 -0
  113. data/lib/active_record/railties/controller_runtime.rb +50 -0
  114. data/lib/active_record/railties/databases.rake +402 -0
  115. data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
  116. data/lib/active_record/readonly_attributes.rb +30 -0
  117. data/lib/active_record/reflection.rb +544 -87
  118. data/lib/active_record/relation/batches.rb +93 -0
  119. data/lib/active_record/relation/calculations.rb +399 -0
  120. data/lib/active_record/relation/delegation.rb +125 -0
  121. data/lib/active_record/relation/finder_methods.rb +349 -0
  122. data/lib/active_record/relation/merger.rb +161 -0
  123. data/lib/active_record/relation/predicate_builder.rb +106 -0
  124. data/lib/active_record/relation/query_methods.rb +1044 -0
  125. data/lib/active_record/relation/spawn_methods.rb +73 -0
  126. data/lib/active_record/relation.rb +655 -0
  127. data/lib/active_record/result.rb +67 -0
  128. data/lib/active_record/runtime_registry.rb +17 -0
  129. data/lib/active_record/sanitization.rb +168 -0
  130. data/lib/active_record/schema.rb +65 -0
  131. data/lib/active_record/schema_dumper.rb +204 -0
  132. data/lib/active_record/schema_migration.rb +39 -0
  133. data/lib/active_record/scoping/default.rb +146 -0
  134. data/lib/active_record/scoping/named.rb +175 -0
  135. data/lib/active_record/scoping.rb +82 -0
  136. data/lib/active_record/serialization.rb +22 -0
  137. data/lib/active_record/serializers/xml_serializer.rb +197 -0
  138. data/lib/active_record/statement_cache.rb +26 -0
  139. data/lib/active_record/store.rb +156 -0
  140. data/lib/active_record/tasks/database_tasks.rb +203 -0
  141. data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
  142. data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
  143. data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
  144. data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
  145. data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
  146. data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
  147. data/lib/active_record/test_case.rb +96 -0
  148. data/lib/active_record/timestamp.rb +119 -0
  149. data/lib/active_record/transactions.rb +366 -69
  150. data/lib/active_record/translation.rb +22 -0
  151. data/lib/active_record/validations/associated.rb +49 -0
  152. data/lib/active_record/validations/presence.rb +65 -0
  153. data/lib/active_record/validations/uniqueness.rb +225 -0
  154. data/lib/active_record/validations.rb +64 -185
  155. data/lib/active_record/version.rb +11 -0
  156. data/lib/active_record.rb +149 -24
  157. data/lib/rails/generators/active_record/migration/migration_generator.rb +62 -0
  158. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
  159. data/lib/rails/generators/active_record/migration/templates/migration.rb +39 -0
  160. data/lib/rails/generators/active_record/model/model_generator.rb +48 -0
  161. data/lib/rails/generators/active_record/model/templates/model.rb +10 -0
  162. data/lib/rails/generators/active_record/model/templates/module.rb +7 -0
  163. data/lib/rails/generators/active_record.rb +23 -0
  164. metadata +261 -161
  165. data/CHANGELOG +0 -581
  166. data/README +0 -361
  167. data/RUNNING_UNIT_TESTS +0 -36
  168. data/dev-utils/eval_debugger.rb +0 -9
  169. data/examples/associations.png +0 -0
  170. data/examples/associations.rb +0 -87
  171. data/examples/shared_setup.rb +0 -15
  172. data/examples/validation.rb +0 -88
  173. data/install.rb +0 -60
  174. data/lib/active_record/associations/association_collection.rb +0 -70
  175. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -107
  176. data/lib/active_record/deprecated_associations.rb +0 -70
  177. data/lib/active_record/observer.rb +0 -71
  178. data/lib/active_record/support/class_attribute_accessors.rb +0 -43
  179. data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
  180. data/lib/active_record/support/clean_logger.rb +0 -10
  181. data/lib/active_record/support/inflector.rb +0 -70
  182. data/lib/active_record/vendor/mysql.rb +0 -1117
  183. data/lib/active_record/vendor/simple.rb +0 -702
  184. data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
  185. data/lib/active_record/wrappings.rb +0 -59
  186. data/rakefile +0 -122
  187. data/test/abstract_unit.rb +0 -16
  188. data/test/aggregations_test.rb +0 -34
  189. data/test/all.sh +0 -8
  190. data/test/associations_test.rb +0 -477
  191. data/test/base_test.rb +0 -513
  192. data/test/class_inheritable_attributes_test.rb +0 -33
  193. data/test/connections/native_mysql/connection.rb +0 -24
  194. data/test/connections/native_postgresql/connection.rb +0 -24
  195. data/test/connections/native_sqlite/connection.rb +0 -24
  196. data/test/deprecated_associations_test.rb +0 -336
  197. data/test/finder_test.rb +0 -67
  198. data/test/fixtures/accounts/signals37 +0 -3
  199. data/test/fixtures/accounts/unknown +0 -2
  200. data/test/fixtures/auto_id.rb +0 -4
  201. data/test/fixtures/column_name.rb +0 -3
  202. data/test/fixtures/companies/first_client +0 -6
  203. data/test/fixtures/companies/first_firm +0 -4
  204. data/test/fixtures/companies/second_client +0 -6
  205. data/test/fixtures/company.rb +0 -37
  206. data/test/fixtures/company_in_module.rb +0 -33
  207. data/test/fixtures/course.rb +0 -3
  208. data/test/fixtures/courses/java +0 -2
  209. data/test/fixtures/courses/ruby +0 -2
  210. data/test/fixtures/customer.rb +0 -30
  211. data/test/fixtures/customers/david +0 -6
  212. data/test/fixtures/db_definitions/mysql.sql +0 -96
  213. data/test/fixtures/db_definitions/mysql2.sql +0 -4
  214. data/test/fixtures/db_definitions/postgresql.sql +0 -113
  215. data/test/fixtures/db_definitions/postgresql2.sql +0 -4
  216. data/test/fixtures/db_definitions/sqlite.sql +0 -85
  217. data/test/fixtures/db_definitions/sqlite2.sql +0 -4
  218. data/test/fixtures/default.rb +0 -2
  219. data/test/fixtures/developer.rb +0 -8
  220. data/test/fixtures/developers/david +0 -2
  221. data/test/fixtures/developers/jamis +0 -2
  222. data/test/fixtures/developers_projects/david_action_controller +0 -2
  223. data/test/fixtures/developers_projects/david_active_record +0 -2
  224. data/test/fixtures/developers_projects/jamis_active_record +0 -2
  225. data/test/fixtures/entrant.rb +0 -3
  226. data/test/fixtures/entrants/first +0 -3
  227. data/test/fixtures/entrants/second +0 -3
  228. data/test/fixtures/entrants/third +0 -3
  229. data/test/fixtures/fixture_database.sqlite +0 -0
  230. data/test/fixtures/fixture_database_2.sqlite +0 -0
  231. data/test/fixtures/movie.rb +0 -5
  232. data/test/fixtures/movies/first +0 -2
  233. data/test/fixtures/movies/second +0 -2
  234. data/test/fixtures/project.rb +0 -3
  235. data/test/fixtures/projects/action_controller +0 -2
  236. data/test/fixtures/projects/active_record +0 -2
  237. data/test/fixtures/reply.rb +0 -21
  238. data/test/fixtures/subscriber.rb +0 -5
  239. data/test/fixtures/subscribers/first +0 -2
  240. data/test/fixtures/subscribers/second +0 -2
  241. data/test/fixtures/topic.rb +0 -20
  242. data/test/fixtures/topics/first +0 -9
  243. data/test/fixtures/topics/second +0 -8
  244. data/test/fixtures_test.rb +0 -20
  245. data/test/inflector_test.rb +0 -104
  246. data/test/inheritance_test.rb +0 -125
  247. data/test/lifecycle_test.rb +0 -110
  248. data/test/modules_test.rb +0 -21
  249. data/test/multiple_db_test.rb +0 -46
  250. data/test/pk_test.rb +0 -57
  251. data/test/reflection_test.rb +0 -78
  252. data/test/thread_safety_test.rb +0 -33
  253. data/test/transactions_test.rb +0 -83
  254. data/test/unconnected_test.rb +0 -24
  255. data/test/validations_test.rb +0 -126
@@ -1,477 +0,0 @@
1
- require 'abstract_unit'
2
- require 'fixtures/developer'
3
- require 'fixtures/project'
4
- # require File.dirname(__FILE__) + '/../dev-utils/eval_debugger'
5
- require 'fixtures/company'
6
- require 'fixtures/topic'
7
- require 'fixtures/reply'
8
-
9
- # Can't declare new classes in test case methods, so tests before that
10
- bad_collection_keys = false
11
- begin
12
- class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
13
- rescue ActiveRecord::ActiveRecordError
14
- bad_collection_keys = true
15
- end
16
- raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
17
-
18
-
19
- class AssociationsTest < Test::Unit::TestCase
20
- def setup
21
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects"
22
- @signals37 = Firm.find(1)
23
- end
24
-
25
- def test_force_reload
26
- firm = Firm.new
27
- firm.save
28
- firm.clients.each {|c|} # forcing to load all clients
29
- assert firm.clients.empty?, "New firm shouldn't have client objects"
30
- assert !firm.has_clients?, "New firm shouldn't have clients"
31
- assert_equal 0, firm.clients.size, "New firm should have 0 clients"
32
-
33
- client = Client.new("firm_id" => firm.id)
34
- client.save
35
-
36
- assert firm.clients.empty?, "New firm should have cached no client objects"
37
- assert !firm.has_clients?, "New firm should have cached a no-clients response"
38
- assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"
39
-
40
- assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
41
- assert_equal 1, firm.clients(true).size, "New firm should have reloaded clients count"
42
- end
43
-
44
- def test_storing_in_pstore
45
- store_filename = "/tmp/ar-pstore-association-test"
46
- File.delete(store_filename) if File.exists?(store_filename)
47
- require "pstore"
48
- apple = Firm.create("name" => "Apple")
49
- natural = Client.new("name" => "Natural Company")
50
- apple.clients << natural
51
-
52
- db = PStore.new(store_filename)
53
- db.transaction do
54
- db["apple"] = apple
55
- end
56
-
57
- db = PStore.new(store_filename)
58
- db.transaction do
59
- assert_equal "Natural Company", db["apple"].clients.first.name
60
- end
61
- end
62
- end
63
-
64
- class HasOneAssociationsTest < Test::Unit::TestCase
65
- def setup
66
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects"
67
- @signals37 = Firm.find(1)
68
- end
69
-
70
- def test_has_one
71
- assert_equal @signals37.account, Account.find(1)
72
- assert_equal Account.find(1).credit_limit, @signals37.account.credit_limit
73
- assert @signals37.has_account?, "37signals should have an account"
74
- assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
75
- assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
76
-
77
- assert !Account.find(2).has_firm?, "Unknown isn't linked"
78
- assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
79
- end
80
-
81
- def test_type_mismatch
82
- assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.account = 1 }
83
- assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.account = Project.find(1) }
84
- end
85
-
86
- def test_natural_assignment
87
- apple = Firm.create("name" => "Apple")
88
- citibank = Account.create("credit_limit" => 10)
89
- apple.account = citibank
90
- assert_equal apple.id, citibank.firm_id
91
- end
92
-
93
- def test_natural_assignment_to_nil
94
- old_account_id = @signals37.account.id
95
- @signals37.account = nil
96
- @signals37.save
97
- assert_nil @signals37.account
98
- assert_nil Account.find(old_account_id).firm_id
99
- end
100
-
101
- def test_build
102
- firm = Firm.new("name" => "GlobalMegaCorp")
103
- firm.save
104
-
105
- account = firm.build_account("credit_limit" => 1000)
106
- assert account.save
107
- assert_equal account, firm.account
108
- end
109
-
110
- def test_failing_build_association
111
- firm = Firm.new("name" => "GlobalMegaCorp")
112
- firm.save
113
-
114
- account = firm.build_account
115
- assert !account.save
116
- assert_equal "can't be empty", account.errors.on("credit_limit")
117
- end
118
-
119
- def test_create
120
- firm = Firm.new("name" => "GlobalMegaCorp")
121
- firm.save
122
- assert_equal firm.create_account("credit_limit" => 1000), firm.account
123
- end
124
-
125
- def test_dependence
126
- firm = Firm.find(1)
127
- assert !firm.account.nil?
128
- firm.destroy
129
- assert_equal 1, Account.find_all.length
130
- end
131
-
132
- def test_dependence_with_missing_association
133
- Account.destroy_all
134
- firm = Firm.find(1)
135
- assert !firm.has_account?
136
- firm.destroy
137
- end
138
- end
139
-
140
-
141
- class HasManyAssociationsTest < Test::Unit::TestCase
142
- def setup
143
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
144
- @signals37 = Firm.find(1)
145
- end
146
-
147
- def force_signal37_to_load_all_clients_of_firm
148
- @signals37.clients_of_firm.each {|f| }
149
- end
150
-
151
- def test_finding
152
- assert_equal 2, Firm.find_first.clients.length
153
- end
154
-
155
- def test_finding_default_orders
156
- assert_equal "Summit", Firm.find_first.clients.first.name
157
- end
158
-
159
- def test_finding_with_different_class_name_and_order
160
- assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
161
- end
162
-
163
- def test_finding_with_foreign_key
164
- assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
165
- end
166
-
167
- def test_finding_with_condition
168
- assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
169
- end
170
-
171
- def test_finding_using_sql
172
- firm = Firm.find_first
173
- firm.clients_using_sql.first
174
- assert_equal "Microsoft", firm.clients_using_sql.first.name
175
- assert_equal 1, firm.clients_using_sql.size
176
- assert_equal 1, Firm.find_first.clients_using_sql.size
177
- end
178
-
179
- def test_find_all
180
- assert_equal 2, Firm.find_first.clients.find_all("type = 'Client'").length
181
- assert_equal 1, Firm.find_first.clients.find_all("name = 'Summit'").length
182
- end
183
-
184
- def test_find_in_collection
185
- assert_equal Client.find(2).name, @signals37.clients.find(2).name
186
- assert_equal Client.find(2).name, @signals37.clients.find {|c| c.name == @signals37.clients.find(2).name }.name
187
- assert_raises(ActiveRecord::RecordNotFound) { @signals37.clients.find(6) }
188
- end
189
-
190
- def test_adding
191
- force_signal37_to_load_all_clients_of_firm
192
- natural = Client.new("name" => "Natural Company")
193
- @signals37.clients_of_firm << natural
194
- assert_equal 2, @signals37.clients_of_firm.size # checking via the collection
195
- assert_equal 2, @signals37.clients_of_firm(true).size # checking using the db
196
- assert_equal natural, @signals37.clients_of_firm.last
197
- end
198
-
199
- def test_adding_a_mismatch_class
200
- assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.clients_of_firm << nil }
201
- assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.clients_of_firm << 1 }
202
- assert_raises(ActiveRecord::AssociationTypeMismatch) { @signals37.clients_of_firm << Topic.find(1) }
203
- end
204
-
205
- def test_adding_a_collection
206
- force_signal37_to_load_all_clients_of_firm
207
- @signals37.clients_of_firm.concat(Client.new("name" => "Natural Company"), Client.new("name" => "Apple"))
208
- assert_equal 3, @signals37.clients_of_firm.size
209
- assert_equal 3, @signals37.clients_of_firm(true).size
210
- end
211
-
212
- def test_build
213
- new_client = @signals37.clients_of_firm.build("name" => "Another Client")
214
- assert_equal "Another Client", new_client.name
215
- assert new_client.save
216
- assert_equal 2, @signals37.clients_of_firm(true).size
217
- end
218
-
219
- def test_create
220
- force_signal37_to_load_all_clients_of_firm
221
- new_client = @signals37.clients_of_firm.create("name" => "Another Client")
222
- assert_equal new_client, @signals37.clients_of_firm.last
223
- assert_equal new_client, @signals37.clients_of_firm(true).last
224
- end
225
-
226
- def test_deleting
227
- force_signal37_to_load_all_clients_of_firm
228
- @signals37.clients_of_firm.delete(@signals37.clients_of_firm.first)
229
- assert_equal 0, @signals37.clients_of_firm.size
230
- assert_equal 0, @signals37.clients_of_firm(true).size
231
- end
232
-
233
- def test_deleting_a_collection
234
- force_signal37_to_load_all_clients_of_firm
235
- @signals37.clients_of_firm.create("name" => "Another Client")
236
- assert_equal 2, @signals37.clients_of_firm.size
237
- @signals37.clients_of_firm.delete([@signals37.clients_of_firm[0], @signals37.clients_of_firm[1]])
238
- assert_equal 0, @signals37.clients_of_firm.size
239
- assert_equal 0, @signals37.clients_of_firm(true).size
240
- end
241
-
242
- def test_deleting_a_association_collection
243
- force_signal37_to_load_all_clients_of_firm
244
- @signals37.clients_of_firm.create("name" => "Another Client")
245
- assert_equal 2, @signals37.clients_of_firm.size
246
- @signals37.clients_of_firm.delete(@signals37.clients_of_firm)
247
- assert_equal 0, @signals37.clients_of_firm.size
248
- assert_equal 0, @signals37.clients_of_firm(true).size
249
- end
250
-
251
- def test_deleting_a_item_which_is_not_in_the_collection
252
- force_signal37_to_load_all_clients_of_firm
253
- summit = Client.find_first("name = 'Summit'")
254
- @signals37.clients_of_firm.delete(summit)
255
- assert_equal 1, @signals37.clients_of_firm.size
256
- assert_equal 1, @signals37.clients_of_firm(true).size
257
- assert_equal 2, summit.client_of
258
- end
259
-
260
- def test_destroy_all
261
- force_signal37_to_load_all_clients_of_firm
262
- assert !@signals37.clients_of_firm.empty?
263
- @signals37.clients_of_firm.destroy_all
264
- assert @signals37.clients_of_firm.empty?
265
- assert @signals37.clients_of_firm(true).empty?
266
- end
267
-
268
- def test_dependence
269
- assert_equal 2, Client.find_all.length
270
- Firm.find_first.destroy
271
- assert_equal 0, Client.find_all.length
272
- end
273
-
274
- def test_dependence_with_transaction_support_on_failure
275
- assert_equal 2, Client.find_all.length
276
- firm = Firm.find_first
277
- clients = firm.clients
278
- clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
279
-
280
- firm.destroy rescue "do nothing"
281
-
282
- assert_equal 2, Client.find_all.length
283
- end
284
-
285
- def test_dependence_on_account
286
- assert_equal 2, Account.find_all.length
287
- @signals37.destroy
288
- assert_equal 1, Account.find_all.length
289
- end
290
-
291
- def test_included_in_collection
292
- assert @signals37.clients.include?(Client.find(2))
293
- end
294
- end
295
-
296
- class BelongsToAssociationsTest < Test::Unit::TestCase
297
- def setup
298
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
299
- @signals37 = Firm.find(1)
300
- end
301
-
302
- def test_belongs_to
303
- Client.find(3).firm.name
304
- assert_equal @signals37.name, Client.find(3).firm.name
305
- assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
306
- end
307
-
308
- def test_type_mismatch
309
- assert_raises(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
310
- assert_raises(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
311
- end
312
-
313
- def test_natural_assignment
314
- apple = Firm.create("name" => "Apple")
315
- citibank = Account.create("credit_limit" => 10)
316
- citibank.firm = apple
317
- assert_equal apple.id, citibank.firm_id
318
- end
319
-
320
- def test_natural_assignment_to_nil
321
- client = Client.find(3)
322
- client.firm = nil
323
- client.save
324
- assert_nil client.firm(true)
325
- assert_nil client.client_of
326
- end
327
-
328
- def test_with_different_class_name
329
- assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
330
- assert !Company.find(3).firm_with_other_name.empty?, "Microsoft should have a firm"
331
- end
332
-
333
- def test_with_condition
334
- assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
335
- assert !Company.find(3).firm_with_condition.empty?, "Microsoft should have a firm"
336
- end
337
-
338
- def test_belongs_to_counter
339
- debate = Topic.create("title" => "debate")
340
- assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
341
-
342
- trash = debate.replies.create("title" => "blah!", "content" => "world around!")
343
- assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
344
-
345
- trash.destroy
346
- assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
347
- end
348
-
349
- def xtest_counter_cache
350
- apple = Firm.create("name" => "Apple")
351
- final_cut = apple.clients.create("name" => "Final Cut")
352
-
353
- apple.clients.to_s
354
- assert_equal 1, apple.clients.size, "Created one client"
355
-
356
- apple.companies_count = 2
357
- apple.save
358
-
359
- apple = Firm.find_first("name = 'Apple'")
360
- assert_equal 2, apple.clients.size, "Should use the new cached number"
361
-
362
- apple.clients.to_s
363
- assert_equal 1, apple.clients.size, "Should not use the cached number, but go to the database"
364
- end
365
- end
366
-
367
-
368
- class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
369
- def setup
370
- create_fixtures "accounts"
371
- create_fixtures "companies"
372
- create_fixtures "accounts"
373
- create_fixtures "developers"
374
- create_fixtures "projects"
375
- create_fixtures "developers_projects"
376
- @signals37 = Firm.find(1)
377
- end
378
-
379
- def test_has_and_belongs_to_many
380
- david = Developer.find(1)
381
-
382
- assert !david.projects.empty?
383
- assert_equal 2, david.projects.size
384
-
385
- active_record = Project.find(1)
386
- assert !active_record.developers.empty?
387
- assert_equal 2, active_record.developers.size
388
- assert_equal david.name, active_record.developers.first.name
389
- end
390
-
391
- def test_addings
392
- jamis = Developer.find(2)
393
- jamis.projects.id # causing the collection to load
394
- action_controller = Project.find(2)
395
- assert_equal 1, jamis.projects.size
396
- assert_equal 1, action_controller.developers.size
397
-
398
- jamis.projects << action_controller
399
-
400
- assert_equal 2, jamis.projects.size
401
- assert_equal 2, jamis.projects(true).size
402
- assert_equal 2, action_controller.developers(true).size
403
- end
404
-
405
- def test_adding_type_mismatch
406
- jamis = Developer.find(2)
407
- assert_raises(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil }
408
- assert_raises(ActiveRecord::AssociationTypeMismatch) { jamis.projects << 1 }
409
- end
410
-
411
- def test_adding_from_the_project
412
- jamis = Developer.find(2)
413
- action_controller = Project.find(2)
414
- action_controller.developers.id
415
- assert_equal 1, jamis.projects.size
416
- assert_equal 1, action_controller.developers.size
417
-
418
- action_controller.developers << jamis
419
-
420
- assert_equal 2, jamis.projects(true).size
421
- assert_equal 2, action_controller.developers.size
422
- assert_equal 2, action_controller.developers(true).size
423
- end
424
-
425
- def test_adding_a_collection
426
- aridridel = Developer.new("name" => "Aridridel")
427
- aridridel.save
428
- aridridel.projects.id
429
- aridridel.projects.concat([ Project.find(1), Project.find(2) ])
430
- assert_equal 2, aridridel.projects.size
431
- assert_equal 2, aridridel.projects(true).size
432
- end
433
-
434
- def test_deleting
435
- david = Developer.find(1)
436
- active_record = Project.find(1)
437
- david.projects.id
438
- assert_equal 2, david.projects.size
439
- assert_equal 2, active_record.developers.size
440
-
441
- david.projects.delete(active_record)
442
-
443
- assert_equal 1, david.projects.size
444
- assert_equal 1, david.projects(true).size
445
- assert_equal 1, active_record.developers(true).size
446
- end
447
-
448
- def test_deleting_a_collection
449
- david = Developer.find(1)
450
- david.projects.id
451
- david.projects.delete(Project.find_all)
452
- assert_equal 0, david.projects.size
453
- assert_equal 0, david.projects(true).size
454
- end
455
-
456
- def test_deleting_a_association_collection
457
- david = Developer.find(1)
458
- david.projects.id
459
- david.projects.delete(david.projects)
460
- assert_equal 0, david.projects.size
461
- assert_equal 0, david.projects(true).size
462
- end
463
-
464
- def test_removing_associations_on_destroy
465
- Developer.find(1).destroy
466
- assert Developer.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = '1'").empty?
467
- end
468
-
469
- def test_destroy_all
470
- david = Developer.find(1)
471
- david.projects.id
472
- assert !david.projects.empty?
473
- david.projects.destroy_all
474
- assert david.projects.empty?
475
- assert david.projects(true).empty?
476
- end
477
- end