activerecord 1.1.0 → 1.2.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 (68) hide show
  1. data/CHANGELOG +250 -0
  2. data/README +17 -9
  3. data/dev-utils/eval_debugger.rb +1 -1
  4. data/install.rb +3 -1
  5. data/lib/active_record.rb +9 -2
  6. data/lib/active_record/acts/list.rb +178 -0
  7. data/lib/active_record/acts/tree.rb +44 -0
  8. data/lib/active_record/associations.rb +45 -8
  9. data/lib/active_record/associations/association_collection.rb +18 -9
  10. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +14 -13
  11. data/lib/active_record/associations/has_many_association.rb +21 -12
  12. data/lib/active_record/base.rb +137 -37
  13. data/lib/active_record/callbacks.rb +30 -25
  14. data/lib/active_record/connection_adapters/abstract_adapter.rb +57 -33
  15. data/lib/active_record/connection_adapters/mysql_adapter.rb +4 -0
  16. data/lib/active_record/connection_adapters/sqlite_adapter.rb +3 -2
  17. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +298 -0
  18. data/lib/active_record/fixtures.rb +241 -147
  19. data/lib/active_record/support/class_inheritable_attributes.rb +5 -2
  20. data/lib/active_record/support/inflector.rb +13 -12
  21. data/lib/active_record/support/misc.rb +6 -0
  22. data/lib/active_record/timestamp.rb +33 -0
  23. data/lib/active_record/transactions.rb +1 -1
  24. data/lib/active_record/validations.rb +294 -16
  25. data/rakefile +3 -7
  26. data/test/abstract_unit.rb +1 -4
  27. data/test/associations_test.rb +17 -4
  28. data/test/base_test.rb +37 -5
  29. data/test/connections/native_sqlserver/connection.rb +15 -0
  30. data/test/deprecated_associations_test.rb +40 -38
  31. data/test/finder_test.rb +82 -4
  32. data/test/fixtures/accounts.yml +8 -0
  33. data/test/fixtures/company.rb +6 -0
  34. data/test/fixtures/company_in_module.rb +1 -1
  35. data/test/fixtures/db_definitions/mysql.sql +13 -0
  36. data/test/fixtures/db_definitions/postgresql.sql +13 -0
  37. data/test/fixtures/db_definitions/sqlite.sql +14 -0
  38. data/test/fixtures/db_definitions/sqlserver.sql +110 -0
  39. data/test/fixtures/db_definitions/sqlserver2.sql +4 -0
  40. data/test/fixtures/developer.rb +2 -2
  41. data/test/fixtures/developers.yml +13 -0
  42. data/test/fixtures/fixture_database.sqlite +0 -0
  43. data/test/fixtures/fixture_database_2.sqlite +0 -0
  44. data/test/fixtures/mixin.rb +17 -0
  45. data/test/fixtures/mixins.yml +14 -0
  46. data/test/fixtures/naked/csv/accounts.csv +1 -0
  47. data/test/fixtures/naked/yml/accounts.yml +1 -0
  48. data/test/fixtures/naked/yml/companies.yml +1 -0
  49. data/test/fixtures/naked/yml/courses.yml +1 -0
  50. data/test/fixtures/project.rb +6 -0
  51. data/test/fixtures/reply.rb +14 -1
  52. data/test/fixtures/topic.rb +2 -2
  53. data/test/fixtures/topics/first +1 -0
  54. data/test/fixtures_test.rb +42 -12
  55. data/test/inflector_test.rb +2 -1
  56. data/test/inheritance_test.rb +22 -12
  57. data/test/mixin_test.rb +138 -0
  58. data/test/pk_test.rb +4 -2
  59. data/test/reflection_test.rb +3 -3
  60. data/test/transactions_test.rb +15 -0
  61. data/test/validations_test.rb +229 -4
  62. metadata +24 -10
  63. data/lib/active_record/associations.rb.orig +0 -555
  64. data/test/deprecated_associations_test.rb.orig +0 -334
  65. data/test/fixtures/accounts/signals37 +0 -3
  66. data/test/fixtures/accounts/unknown +0 -2
  67. data/test/fixtures/developers/david +0 -2
  68. data/test/fixtures/developers/jamis +0 -2
@@ -1,334 +0,0 @@
1
- require 'abstract_unit'
2
- require 'fixtures/developer'
3
- require 'fixtures/project'
4
- require 'fixtures/company'
5
- require 'fixtures/topic'
6
- # require File.dirname(__FILE__) + '/../dev-utils/eval_debugger'
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 DeprecatedAssociationsTest < Test::Unit::TestCase
20
- def setup
21
- create_fixtures "accounts", "companies", "accounts", "developers", "projects", "developers_projects", "topics"
22
- @signals37 = Firm.find(1)
23
- end
24
-
25
- def test_has_many_find
26
- assert_equal 2, Firm.find_first.clients.length
27
- end
28
-
29
- def test_has_many_orders
30
- assert_equal "Summit", Firm.find_first.clients.first.name
31
- end
32
-
33
- def test_has_many_class_name
34
- assert_equal "Microsoft", Firm.find_first.clients_sorted_desc.first.name
35
- end
36
-
37
- def test_has_many_foreign_key
38
- assert_equal "Microsoft", Firm.find_first.clients_of_firm.first.name
39
- end
40
-
41
- def test_has_many_conditions
42
- assert_equal "Microsoft", Firm.find_first.clients_like_ms.first.name
43
- end
44
-
45
- def test_has_many_sql
46
- firm = Firm.find_first
47
- assert_equal "Microsoft", firm.clients_using_sql.first.name
48
- assert_equal 1, firm.clients_using_sql_count
49
- assert_equal 1, Firm.find_first.clients_using_sql_count
50
- end
51
-
52
- def test_has_many_queries
53
- assert Firm.find_first.has_clients?
54
- firm = Firm.find_first
55
- assert_equal 2, firm.clients_count # tests using class count
56
- firm.clients
57
- assert firm.has_clients?
58
- assert_equal 2, firm.clients_count # tests using collection length
59
- end
60
-
61
- def test_has_many_dependence
62
- assert_equal 2, Client.find_all.length
63
- Firm.find_first.destroy
64
- assert_equal 0, Client.find_all.length
65
- end
66
-
67
- def test_has_many_dependence_with_transaction_support_on_failure
68
- assert_equal 2, Client.find_all.length
69
-
70
- firm = Firm.find_first
71
- clients = firm.clients
72
- clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
73
-
74
- firm.destroy rescue "do nothing"
75
-
76
- assert_equal 2, Client.find_all.length
77
- end
78
-
79
- def test_has_one_dependence
80
- firm = Firm.find(1)
81
- assert firm.has_account?
82
- firm.destroy
83
- assert_equal 1, Account.find_all.length
84
- end
85
-
86
- def test_has_one_dependence_with_missing_association
87
- Account.destroy_all
88
- firm = Firm.find(1)
89
- assert !firm.has_account?
90
- firm.destroy
91
- end
92
-
93
- def test_belongs_to
94
- assert_equal @signals37.name, Client.find(3).firm.name
95
- assert Client.find(3).has_firm?, "Microsoft should have a firm"
96
- # assert !Company.find(1).has_firm?, "37signals shouldn't have a firm"
97
- end
98
-
99
- def test_belongs_to_with_different_class_name
100
- assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
101
- assert Company.find(3).has_firm_with_other_name?, "Microsoft should have a firm"
102
- end
103
-
104
- def test_belongs_to_with_condition
105
- assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
106
- assert Company.find(3).has_firm_with_condition?, "Microsoft should have a firm"
107
- end
108
-
109
-
110
- def test_belongs_to_equality
111
- assert Company.find(3).firm?(Company.find(1)), "Microsoft should have 37signals as firm"
112
- assert_raises(RuntimeError) { !Company.find(3).firm?(Company.find(3)) } # "Summit shouldn't have itself as firm"
113
- end
114
-
115
- def test_has_one
116
- assert @signals37.account?(Account.find(1))
117
- assert_equal Account.find(1).credit_limit, @signals37.account.credit_limit
118
- assert @signals37.has_account?, "37signals should have an account"
119
- assert Account.find(1).firm?(@signals37), "37signals account should be able to backtrack"
120
- assert Account.find(1).has_firm?, "37signals account should be able to backtrack"
121
-
122
- assert !Account.find(2).has_firm?, "Unknown isn't linked"
123
- assert !Account.find(2).firm?(@signals37), "Unknown isn't linked"
124
- end
125
-
126
- def test_has_many_dependence_on_account
127
- assert_equal 2, Account.find_all.length
128
- @signals37.destroy
129
- assert_equal 1, Account.find_all.length
130
- end
131
-
132
- def test_find_in
133
- assert_equal Client.find(2).name, @signals37.find_in_clients(2).name
134
- assert_raises(ActiveRecord::RecordNotFound) { @signals37.find_in_clients(6) }
135
- end
136
-
137
- def test_force_reload
138
- firm = Firm.new
139
- firm.save
140
- firm.clients.each {|c|} # forcing to load all clients
141
- assert firm.clients.empty?, "New firm shouldn't have client objects"
142
- assert !firm.has_clients?, "New firm shouldn't have clients"
143
- assert_equal 0, firm.clients_count, "New firm should have 0 clients"
144
-
145
- client = Client.new("firm_id" => firm.id)
146
- client.save
147
-
148
- assert firm.clients.empty?, "New firm should have cached no client objects"
149
- assert !firm.has_clients?, "New firm should have cached a no-clients response"
150
- assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
151
-
152
- assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
153
- assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
154
- assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
155
- end
156
-
157
- def test_included_in_collection
158
- assert @signals37.clients.include?(Client.find(2))
159
- end
160
-
161
- def test_build_to_collection
162
- assert_equal 1, @signals37.clients_of_firm_count
163
- new_client = @signals37.build_to_clients_of_firm("name" => "Another Client")
164
- assert_equal "Another Client", new_client.name
165
- assert new_client.save
166
-
167
- assert new_client.firm?(@signals37)
168
- assert_equal 2, @signals37.clients_of_firm_count(true)
169
- end
170
-
171
- def test_create_in_collection
172
- assert_equal @signals37.create_in_clients_of_firm("name" => "Another Client"), @signals37.clients_of_firm(true).last
173
- end
174
-
175
- def test_succesful_build_association
176
- firm = Firm.new("name" => "GlobalMegaCorp")
177
- firm.save
178
-
179
- account = firm.build_account("credit_limit" => 1000)
180
- assert account.save
181
- assert_equal account, firm.account
182
- end
183
-
184
- def test_failing_build_association
185
- firm = Firm.new("name" => "GlobalMegaCorp")
186
- firm.save
187
-
188
- account = firm.build_account
189
- assert !account.save
190
- assert_equal "can't be empty", account.errors.on("credit_limit")
191
- end
192
-
193
- def test_create_association
194
- firm = Firm.new("name" => "GlobalMegaCorp")
195
- firm.save
196
- assert_equal firm.create_account("credit_limit" => 1000), firm.account
197
- end
198
-
199
- def test_has_and_belongs_to_many
200
- david = Developer.find(1)
201
- assert david.has_projects?
202
- assert_equal 2, david.projects_count
203
-
204
- active_record = Project.find(1)
205
- assert active_record.has_developers?
206
- assert_equal 2, active_record.developers_count
207
- assert_equal david.name, active_record.developers.first.name
208
- end
209
-
210
- def test_has_and_belongs_to_many_removing
211
- david = Developer.find(1)
212
- active_record = Project.find(1)
213
-
214
- david.remove_projects(active_record)
215
-
216
- assert_equal 1, david.projects_count
217
- assert_equal 1, active_record.developers_count
218
- end
219
-
220
- def test_has_and_belongs_to_many_zero
221
- david = Developer.find(1)
222
- david.remove_projects(Project.find_all)
223
-
224
- assert_equal 0, david.projects_count
225
- assert !david.has_projects?
226
- end
227
-
228
- def test_has_and_belongs_to_many_adding
229
- jamis = Developer.find(2)
230
- action_controller = Project.find(2)
231
-
232
- jamis.add_projects(action_controller)
233
-
234
- assert_equal 2, jamis.projects_count
235
- assert_equal 2, action_controller.developers_count
236
- end
237
-
238
- def test_has_and_belongs_to_many_adding_from_the_project
239
- jamis = Developer.find(2)
240
- action_controller = Project.find(2)
241
-
242
- action_controller.add_developers(jamis)
243
-
244
- assert_equal 2, jamis.projects_count
245
- assert_equal 2, action_controller.developers_count
246
- end
247
-
248
- def test_has_and_belongs_to_many_adding_a_collection
249
- aridridel = Developer.new("name" => "Aridridel")
250
- aridridel.save
251
-
252
- aridridel.add_projects([ Project.find(1), Project.find(2) ])
253
- assert_equal 2, aridridel.projects_count
254
- end
255
-
256
- def test_belongs_to_counter
257
- topic = Topic.create("title" => "Apple", "content" => "hello world")
258
- assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
259
-
260
- reply = topic.create_in_replies("title" => "I'm saying no!", "content" => "over here")
261
- assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created"
262
-
263
- reply.destroy
264
- assert_equal 0, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply deleted"
265
- end
266
-
267
- def test_natural_assignment_of_has_one
268
- apple = Firm.create("name" => "Apple")
269
- citibank = Account.create("credit_limit" => 10)
270
- apple.account = citibank
271
- assert_equal apple.id, citibank.firm_id
272
- end
273
-
274
- def test_natural_assignment_of_belongs_to
275
- apple = Firm.create("name" => "Apple")
276
- citibank = Account.create("credit_limit" => 10)
277
- citibank.firm = apple
278
- assert_equal apple.id, citibank.firm_id
279
- end
280
-
281
- def test_natural_assignment_of_has_many
282
- apple = Firm.create("name" => "Apple")
283
- natural = Client.new("name" => "Natural Company")
284
- apple.clients << natural
285
- assert_equal apple.id, natural.firm_id
286
- assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find { |c| c.id == natural.id }
287
- apple.clients.delete natural
288
- assert_nil Firm.find(apple.id).clients.find { |c| c.id == natural.id }
289
- end
290
-
291
-
292
- def test_natural_adding_of_has_and_belongs_to_many
293
- rails = Project.create("name" => "Rails")
294
- ap = Project.create("name" => "Action Pack")
295
- john = Developer.create("name" => "John")
296
- mike = Developer.create("name" => "Mike")
297
- rails.developers << john
298
- rails.developers << mike
299
-
300
- assert_equal Developer.find(john.id), Project.find(rails.id).developers.find { |d| d.id == john.id }
301
- assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find { |d| d.id == mike.id }
302
- assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find { |p| p.id == rails.id }
303
- assert_equal Project.find(rails.id), Developer.find(john.id).projects.find { |p| p.id == rails.id }
304
- ap.developers << john
305
- assert_equal Developer.find(john.id), Project.find(ap.id).developers.find { |d| d.id == john.id }
306
- assert_equal Project.find(ap.id), Developer.find(john.id).projects.find { |p| p.id == ap.id }
307
-
308
- ap.developers.delete john
309
- assert_nil Project.find(ap.id).developers.find { |d| d.id == john.id }
310
- assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id }
311
- end
312
-
313
- def test_storing_in_pstore
314
- require "pstore"
315
- apple = Firm.create("name" => "Apple")
316
- natural = Client.new("name" => "Natural Company")
317
- apple.clients << natural
318
-
319
- db = PStore.new("/tmp/ar-pstore-association-test")
320
- db.transaction do
321
- db["apple"] = apple
322
- end
323
-
324
- db = PStore.new("/tmp/ar-pstore-association-test")
325
- db.transaction do
326
- assert_equal "Natural Company", db["apple"].clients.first.name
327
- end
328
- end
329
-
330
- def test_has_many_find_all
331
- assert_equal 2, Firm.find_first.find_all_in_clients("type = 'Client'").length
332
- assert_equal 1, Firm.find_first.find_all_in_clients("name = 'Summit'").length
333
- end
334
- end
@@ -1,3 +0,0 @@
1
- id => 1
2
- firm_id => 1
3
- credit_limit => 50
@@ -1,2 +0,0 @@
1
- id => 2
2
- credit_limit => 50
@@ -1,2 +0,0 @@
1
- id => 1
2
- name => David
@@ -1,2 +0,0 @@
1
- id => 2
2
- name => Jamis