activerecord 2.3.2 → 2.3.3

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 (55) hide show
  1. data/CHANGELOG +11 -0
  2. data/Rakefile +10 -10
  3. data/lib/active_record/associations.rb +67 -30
  4. data/lib/active_record/associations/association_collection.rb +9 -5
  5. data/lib/active_record/associations/association_proxy.rb +2 -2
  6. data/lib/active_record/associations/belongs_to_association.rb +22 -4
  7. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +5 -1
  8. data/lib/active_record/associations/has_one_through_association.rb +8 -8
  9. data/lib/active_record/autosave_association.rb +11 -6
  10. data/lib/active_record/base.rb +16 -21
  11. data/lib/active_record/batches.rb +23 -15
  12. data/lib/active_record/calculations.rb +5 -13
  13. data/lib/active_record/connection_adapters/postgresql_adapter.rb +66 -22
  14. data/lib/active_record/connection_adapters/sqlite_adapter.rb +3 -0
  15. data/lib/active_record/fixtures.rb +5 -4
  16. data/lib/active_record/named_scope.rb +2 -2
  17. data/lib/active_record/schema_dumper.rb +5 -1
  18. data/lib/active_record/serialization.rb +3 -2
  19. data/lib/active_record/serializers/json_serializer.rb +8 -18
  20. data/lib/active_record/session_store.rb +9 -1
  21. data/lib/active_record/timestamp.rb +39 -9
  22. data/lib/active_record/validations.rb +1 -1
  23. data/lib/active_record/version.rb +1 -1
  24. data/test/cases/associations/belongs_to_associations_test.rb +102 -4
  25. data/test/cases/associations/eager_test.rb +12 -0
  26. data/test/cases/associations/has_many_associations_test.rb +6 -0
  27. data/test/cases/associations/has_one_through_associations_test.rb +8 -1
  28. data/test/cases/associations/inner_join_association_test.rb +5 -0
  29. data/test/cases/autosave_association_test.rb +22 -0
  30. data/test/cases/base_test.rb +2 -2
  31. data/test/cases/calculations_test.rb +8 -14
  32. data/test/cases/copy_table_test_sqlite.rb +5 -5
  33. data/test/cases/finder_test.rb +6 -0
  34. data/test/cases/fixtures_test.rb +5 -0
  35. data/test/cases/helper.rb +1 -2
  36. data/test/cases/json_serialization_test.rb +57 -57
  37. data/test/cases/method_scoping_test.rb +13 -3
  38. data/test/cases/reflection_test.rb +5 -5
  39. data/test/cases/schema_dumper_test.rb +17 -7
  40. data/test/cases/schema_test_postgresql.rb +76 -0
  41. data/test/cases/timestamp_test.rb +75 -0
  42. data/test/debug.log +415 -0
  43. data/test/fixtures/fixture_database.sqlite3 +0 -0
  44. data/test/fixtures/fixture_database_2.sqlite3 +0 -0
  45. data/test/models/author.rb +4 -0
  46. data/test/models/company.rb +7 -7
  47. data/test/models/developer.rb +10 -0
  48. data/test/models/essay.rb +3 -0
  49. data/test/models/pet.rb +1 -1
  50. data/test/models/project.rb +1 -1
  51. data/test/models/reply.rb +2 -1
  52. data/test/models/topic.rb +1 -0
  53. data/test/models/toy.rb +2 -0
  54. data/test/schema/schema.rb +15 -0
  55. metadata +6 -3
@@ -802,7 +802,7 @@ module ActiveRecord
802
802
  # Validates whether the value of the specified attribute is available in a particular enumerable object.
803
803
  #
804
804
  # class Person < ActiveRecord::Base
805
- # validates_inclusion_of :gender, :in => %w( m f ), :message => "woah! what are you then!??!!"
805
+ # validates_inclusion_of :gender, :in => %w( m f )
806
806
  # validates_inclusion_of :age, :in => 0..99
807
807
  # validates_inclusion_of :format, :in => %w( jpg gif png ), :message => "extension {{value}} is not included in the list"
808
808
  # end
@@ -2,7 +2,7 @@ module ActiveRecord
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 2
4
4
  MINOR = 3
5
- TINY = 2
5
+ TINY = 3
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
@@ -14,6 +14,7 @@ require 'models/tagging'
14
14
  require 'models/comment'
15
15
  require 'models/sponsor'
16
16
  require 'models/member'
17
+ require 'models/essay'
17
18
 
18
19
  class BelongsToAssociationsTest < ActiveRecord::TestCase
19
20
  fixtures :accounts, :companies, :developers, :projects, :topics,
@@ -25,6 +26,11 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
25
26
  assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
26
27
  end
27
28
 
29
+ def test_belongs_to_with_primary_key
30
+ client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
31
+ assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
32
+ end
33
+
28
34
  def test_proxy_assignment
29
35
  account = Account.find(1)
30
36
  assert_nothing_raised { account.firm = account.firm }
@@ -47,6 +53,13 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
47
53
  assert_equal apple.id, citibank.firm_id
48
54
  end
49
55
 
56
+ def test_natural_assignment_with_primary_key
57
+ apple = Firm.create("name" => "Apple")
58
+ citibank = Client.create("name" => "Primary key client")
59
+ citibank.firm_with_primary_key = apple
60
+ assert_equal apple.name, citibank.firm_name
61
+ end
62
+
50
63
  def test_no_unexpected_aliasing
51
64
  first_firm = companies(:first_firm)
52
65
  another_firm = companies(:another_firm)
@@ -69,6 +82,15 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
69
82
  assert_equal apple, citibank.firm
70
83
  end
71
84
 
85
+ def test_creating_the_belonging_object_with_primary_key
86
+ client = Client.create(:name => "Primary key client")
87
+ apple = client.create_firm_with_primary_key("name" => "Apple")
88
+ assert_equal apple, client.firm_with_primary_key
89
+ client.save
90
+ client.reload
91
+ assert_equal apple, client.firm_with_primary_key
92
+ end
93
+
72
94
  def test_building_the_belonging_object
73
95
  citibank = Account.create("credit_limit" => 10)
74
96
  apple = citibank.build_firm("name" => "Apple")
@@ -76,6 +98,13 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
76
98
  assert_equal apple.id, citibank.firm_id
77
99
  end
78
100
 
101
+ def test_building_the_belonging_object_with_primary_key
102
+ client = Client.create(:name => "Primary key client")
103
+ apple = client.build_firm_with_primary_key("name" => "Apple")
104
+ client.save
105
+ assert_equal apple.name, client.firm_name
106
+ end
107
+
79
108
  def test_natural_assignment_to_nil
80
109
  client = Client.find(3)
81
110
  client.firm = nil
@@ -84,6 +113,14 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
84
113
  assert_nil client.client_of
85
114
  end
86
115
 
116
+ def test_natural_assignment_to_nil_with_primary_key
117
+ client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
118
+ client.firm_with_primary_key = nil
119
+ client.save
120
+ assert_nil client.firm_with_primary_key(true)
121
+ assert_nil client.client_of
122
+ end
123
+
87
124
  def test_with_different_class_name
88
125
  assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
89
126
  assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
@@ -110,6 +147,17 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
110
147
  assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
111
148
  end
112
149
 
150
+ def test_belongs_to_with_primary_key_counter
151
+ debate = Topic.create("title" => "debate")
152
+ assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
153
+
154
+ trash = debate.replies_with_primary_key.create("title" => "blah!", "content" => "world around!")
155
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
156
+
157
+ trash.destroy
158
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
159
+ end
160
+
113
161
  def test_belongs_to_counter_with_assigning_nil
114
162
  p = Post.find(1)
115
163
  c = Comment.find(1)
@@ -122,6 +170,18 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
122
170
  assert_equal 1, Post.find(p.id).comments.size
123
171
  end
124
172
 
173
+ def test_belongs_to_with_primary_key_counter_with_assigning_nil
174
+ debate = Topic.create("title" => "debate")
175
+ reply = Reply.create("title" => "blah!", "content" => "world around!", "parent_title" => "debate")
176
+
177
+ assert_equal debate.title, reply.parent_title
178
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count")
179
+
180
+ reply.topic_with_primary_key = nil
181
+
182
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count")
183
+ end
184
+
125
185
  def test_belongs_to_counter_with_reassigning
126
186
  t1 = Topic.create("title" => "t1")
127
187
  t2 = Topic.create("title" => "t2")
@@ -219,6 +279,18 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
219
279
  assert_equal firm, final_cut.firm(true)
220
280
  end
221
281
 
282
+ def test_assignment_before_child_saved_with_primary_key
283
+ final_cut = Client.new("name" => "Final Cut")
284
+ firm = Firm.find(1)
285
+ final_cut.firm_with_primary_key = firm
286
+ assert final_cut.new_record?
287
+ assert final_cut.save
288
+ assert !final_cut.new_record?
289
+ assert !firm.new_record?
290
+ assert_equal firm, final_cut.firm_with_primary_key
291
+ assert_equal firm, final_cut.firm_with_primary_key(true)
292
+ end
293
+
222
294
  def test_new_record_with_foreign_key_but_no_object
223
295
  c = Client.new("firm_id" => 1)
224
296
  assert_equal Firm.find(:first), c.firm_with_basic_id
@@ -297,26 +369,52 @@ class BelongsToAssociationsTest < ActiveRecord::TestCase
297
369
  member = Member.create
298
370
  sponsor.sponsorable = member
299
371
  assert_equal "Member", sponsor.sponsorable_type
300
-
372
+
301
373
  # should update when assigning a new record
302
374
  sponsor = Sponsor.new
303
375
  member = Member.new
304
376
  sponsor.sponsorable = member
305
377
  assert_equal "Member", sponsor.sponsorable_type
306
378
  end
307
-
379
+
380
+ def test_polymorphic_assignment_with_primary_key_foreign_type_field_updating
381
+ # should update when assigning a saved record
382
+ essay = Essay.new
383
+ writer = Author.create(:name => "David")
384
+ essay.writer = writer
385
+ assert_equal "Author", essay.writer_type
386
+
387
+ # should update when assigning a new record
388
+ essay = Essay.new
389
+ writer = Author.new
390
+ essay.writer = writer
391
+ assert_equal "Author", essay.writer_type
392
+ end
393
+
308
394
  def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
309
395
  sponsor = Sponsor.new
310
396
  saved_member = Member.create
311
397
  new_member = Member.new
312
-
398
+
313
399
  sponsor.sponsorable = saved_member
314
400
  assert_equal saved_member.id, sponsor.sponsorable_id
315
-
401
+
316
402
  sponsor.sponsorable = new_member
317
403
  assert_equal nil, sponsor.sponsorable_id
318
404
  end
319
405
 
406
+ def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records
407
+ essay = Essay.new
408
+ saved_writer = Author.create(:name => "David")
409
+ new_writer = Author.new
410
+
411
+ essay.writer = saved_writer
412
+ assert_equal saved_writer.name, essay.writer_id
413
+
414
+ essay.writer = new_writer
415
+ assert_equal nil, essay.writer_id
416
+ end
417
+
320
418
  def test_belongs_to_proxy_should_not_respond_to_private_methods
321
419
  assert_raise(NoMethodError) { companies(:first_firm).private_method }
322
420
  assert_raise(NoMethodError) { companies(:second_client).firm.private_method }
@@ -223,6 +223,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
223
223
  end
224
224
  end
225
225
 
226
+ def test_eager_association_loading_with_belongs_to_and_conditions_hash
227
+ comments = []
228
+ assert_nothing_raised do
229
+ comments = Comment.find(:all, :include => :post, :conditions => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id')
230
+ end
231
+ assert_equal 3, comments.length
232
+ assert_equal [5,6,7], comments.collect { |c| c.id }
233
+ assert_no_queries do
234
+ comments.first.post
235
+ end
236
+ end
237
+
226
238
  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
227
239
  quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
228
240
  assert_nothing_raised do
@@ -719,6 +719,12 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
719
719
  assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty?
720
720
  end
721
721
 
722
+ def test_dependence_for_associations_with_hash_condition
723
+ david = authors(:david)
724
+ post = posts(:thinking).id
725
+ assert_difference('Post.count', -1) { assert david.destroy }
726
+ end
727
+
722
728
  def test_destroy_dependent_when_deleted_from_association
723
729
  firm = Firm.find(:first)
724
730
  assert_equal 2, firm.clients.size
@@ -43,7 +43,14 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase
43
43
  @member.reload
44
44
  end
45
45
  end
46
-
46
+
47
+ def test_set_record_to_nil_should_delete_association
48
+ @member.club = nil
49
+ @member.reload
50
+ assert_equal nil, @member.current_membership
51
+ assert_nil @member.club
52
+ end
53
+
47
54
  def test_has_one_through_polymorphic
48
55
  assert_equal clubs(:moustache_club), @member.sponsor_club
49
56
  end
@@ -29,6 +29,11 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase
29
29
  assert_match /INNER JOIN .?categories.? ON.*AND.*.?General.?.*TERMINATING_MARKER/, sql
30
30
  end
31
31
 
32
+ def test_construct_finder_sql_applies_aliases_tables_on_association_conditions
33
+ result = Author.find(:all, :joins => [:thinking_posts, :welcome_posts])
34
+ assert_equal authors(:david), result.first
35
+ end
36
+
32
37
  def test_construct_finder_sql_unpacks_nested_joins
33
38
  sql = Author.send(:construct_finder_sql, :joins => {:posts => [[:comments]]})
34
39
  assert_no_match /inner join.*inner join.*inner join/i, sql, "only two join clauses should be present"
@@ -38,6 +38,17 @@ class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase
38
38
  end
39
39
 
40
40
  class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
41
+ def test_should_save_parent_but_not_invalid_child
42
+ firm = Firm.new(:name => 'GlobalMegaCorp')
43
+ assert firm.valid?
44
+
45
+ firm.build_account_using_primary_key
46
+ assert !firm.build_account_using_primary_key.valid?
47
+
48
+ assert firm.save
49
+ assert firm.account_using_primary_key.new_record?
50
+ end
51
+
41
52
  def test_save_fails_for_invalid_has_one
42
53
  firm = Firm.find(:first)
43
54
  assert firm.valid?
@@ -126,6 +137,17 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas
126
137
  end
127
138
 
128
139
  class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
140
+ def test_should_save_parent_but_not_invalid_child
141
+ client = Client.new(:name => 'Joe (the Plumber)')
142
+ assert client.valid?
143
+
144
+ client.build_firm
145
+ assert !client.firm.valid?
146
+
147
+ assert client.save
148
+ assert client.firm.new_record?
149
+ end
150
+
129
151
  def test_save_fails_for_invalid_belongs_to
130
152
  assert log = AuditLog.create(:developer_id => 0, :message => "")
131
153
 
@@ -1756,7 +1756,7 @@ class BasicsTest < ActiveRecord::TestCase
1756
1756
  end
1757
1757
 
1758
1758
  def test_scoped_find_with_group_and_having
1759
- developers = Developer.with_scope(:find => { :group => 'salary', :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" }) do
1759
+ developers = Developer.with_scope(:find => { :group => 'developers.salary', :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" }) do
1760
1760
  Developer.find(:all)
1761
1761
  end
1762
1762
  assert_equal 3, developers.size
@@ -2014,7 +2014,7 @@ class BasicsTest < ActiveRecord::TestCase
2014
2014
 
2015
2015
  def test_inspect_instance
2016
2016
  topic = topics(:first)
2017
- assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", approved: false, replies_count: 1, parent_id: nil, type: nil>), topic.inspect
2017
+ assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", approved: false, replies_count: 1, parent_id: nil, parent_title: nil, type: nil>), topic.inspect
2018
2018
  end
2019
2019
 
2020
2020
  def test_inspect_new_instance
@@ -2,6 +2,9 @@ require "cases/helper"
2
2
  require 'models/company'
3
3
  require 'models/topic'
4
4
  require 'models/edge'
5
+ require 'models/owner'
6
+ require 'models/pet'
7
+ require 'models/toy'
5
8
 
6
9
  Company.has_many :accounts
7
10
 
@@ -10,7 +13,7 @@ class NumericData < ActiveRecord::Base
10
13
  end
11
14
 
12
15
  class CalculationsTest < ActiveRecord::TestCase
13
- fixtures :companies, :accounts, :topics
16
+ fixtures :companies, :accounts, :topics, :owners, :pets, :toys
14
17
 
15
18
  def test_should_sum_field
16
19
  assert_equal 318, Account.sum(:credit_limit)
@@ -264,19 +267,6 @@ class CalculationsTest < ActiveRecord::TestCase
264
267
  assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
265
268
  end
266
269
 
267
- def test_should_count_scoped_select
268
- Account.update_all("credit_limit = NULL")
269
- assert_equal 0, Account.scoped(:select => "credit_limit").count
270
- end
271
-
272
- def test_should_count_scoped_select_with_options
273
- Account.update_all("credit_limit = NULL")
274
- Account.last.update_attribute('credit_limit', 49)
275
- Account.first.update_attribute('credit_limit', 51)
276
-
277
- assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
278
- end
279
-
280
270
  def test_should_count_manual_select_with_include
281
271
  assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
282
272
  end
@@ -297,6 +287,10 @@ class CalculationsTest < ActiveRecord::TestCase
297
287
  assert_raise(ArgumentError) { Account.count(1, 2, 3) }
298
288
  end
299
289
 
290
+ def test_count_with_scoped_has_many_through_association
291
+ assert_equal 1, owners(:blackbeard).toys.with_name('Bone').count
292
+ end
293
+
300
294
  def test_should_sum_expression
301
295
  assert_equal '636', Account.sum("2 * credit_limit")
302
296
  end
@@ -10,7 +10,7 @@ class CopyTableTest < ActiveRecord::TestCase
10
10
  end
11
11
  end
12
12
 
13
- def test_copy_table(from = 'companies', to = 'companies2', options = {})
13
+ def test_copy_table(from = 'customers', to = 'customers2', options = {})
14
14
  assert_nothing_raised {copy_table(from, to, options)}
15
15
  assert_equal row_count(from), row_count(to)
16
16
 
@@ -24,11 +24,11 @@ class CopyTableTest < ActiveRecord::TestCase
24
24
  end
25
25
 
26
26
  def test_copy_table_renaming_column
27
- test_copy_table('companies', 'companies2',
28
- :rename => {'client_of' => 'fan_of'}) do |from, to, options|
29
- expected = column_values(from, 'client_of')
27
+ test_copy_table('customers', 'customers2',
28
+ :rename => {'name' => 'person_name'}) do |from, to, options|
29
+ expected = column_values(from, 'name')
30
30
  assert expected.any?, 'only nils in resultset; real values are needed'
31
- assert_equal expected, column_values(to, 'fan_of')
31
+ assert_equal expected, column_values(to, 'person_name')
32
32
  end
33
33
  end
34
34
 
@@ -119,6 +119,12 @@ class FinderTest < ActiveRecord::TestCase
119
119
  Address.new(existing_address.street + "1", existing_address.city, existing_address.country))
120
120
  end
121
121
 
122
+ def test_exists_with_scoped_include
123
+ Developer.with_scope(:find => { :include => :projects, :order => "projects.name" }) do
124
+ assert Developer.exists?
125
+ end
126
+ end
127
+
122
128
  def test_find_by_array_of_one_id
123
129
  assert_kind_of(Array, Topic.find([ 1 ]))
124
130
  assert_equal(1, Topic.find([ 1 ]).length)
@@ -518,6 +518,11 @@ class FoxyFixturesTest < ActiveRecord::TestCase
518
518
  assert_equal(Fixtures.identify(:foo), Fixtures.identify(:foo))
519
519
  end
520
520
 
521
+ def test_identifies_consistently
522
+ assert_equal 1281023246, Fixtures.identify(:ruby)
523
+ assert_equal 2140105598, Fixtures.identify(:sapphire_2)
524
+ end
525
+
521
526
  TIMESTAMP_COLUMNS = %w(created_at created_on updated_at updated_on)
522
527
 
523
528
  def test_populates_timestamp_columns
@@ -5,8 +5,7 @@ require 'config'
5
5
 
6
6
  require 'rubygems'
7
7
  require 'test/unit'
8
- gem 'mocha', '>= 0.9.5'
9
- require 'mocha'
8
+ require 'stringio'
10
9
 
11
10
  require 'active_record'
12
11
  require 'active_record/test_case'
@@ -26,19 +26,19 @@ class JsonSerializationTest < ActiveRecord::TestCase
26
26
  NamespacedContact.include_root_in_json = true
27
27
  @contact = NamespacedContact.new :name => 'whatever'
28
28
  json = @contact.to_json
29
- assert_match %r{^\{"namespaced_contact": \{}, json
29
+ assert_match %r{^\{"namespaced_contact":\{}, json
30
30
  end
31
31
 
32
32
  def test_should_include_root_in_json
33
33
  Contact.include_root_in_json = true
34
34
  json = @contact.to_json
35
35
 
36
- assert_match %r{^\{"contact": \{}, json
37
- assert_match %r{"name": "Konata Izumi"}, json
38
- assert_match %r{"age": 16}, json
39
- assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
40
- assert_match %r{"awesome": true}, json
41
- assert_match %r{"preferences": \{"shows": "anime"\}}, json
36
+ assert_match %r{^\{"contact":\{}, json
37
+ assert_match %r{"name":"Konata Izumi"}, json
38
+ assert_match %r{"age":16}, json
39
+ assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
40
+ assert_match %r{"awesome":true}, json
41
+ assert_match %r{"preferences":\{"shows":"anime"\}}, json
42
42
  ensure
43
43
  Contact.include_root_in_json = false
44
44
  end
@@ -46,31 +46,31 @@ class JsonSerializationTest < ActiveRecord::TestCase
46
46
  def test_should_encode_all_encodable_attributes
47
47
  json = @contact.to_json
48
48
 
49
- assert_match %r{"name": "Konata Izumi"}, json
50
- assert_match %r{"age": 16}, json
51
- assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
52
- assert_match %r{"awesome": true}, json
53
- assert_match %r{"preferences": \{"shows": "anime"\}}, json
49
+ assert_match %r{"name":"Konata Izumi"}, json
50
+ assert_match %r{"age":16}, json
51
+ assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
52
+ assert_match %r{"awesome":true}, json
53
+ assert_match %r{"preferences":\{"shows":"anime"\}}, json
54
54
  end
55
55
 
56
56
  def test_should_allow_attribute_filtering_with_only
57
57
  json = @contact.to_json(:only => [:name, :age])
58
58
 
59
- assert_match %r{"name": "Konata Izumi"}, json
60
- assert_match %r{"age": 16}, json
61
- assert_no_match %r{"awesome": true}, json
62
- assert !json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
63
- assert_no_match %r{"preferences": \{"shows": "anime"\}}, json
59
+ assert_match %r{"name":"Konata Izumi"}, json
60
+ assert_match %r{"age":16}, json
61
+ assert_no_match %r{"awesome":true}, json
62
+ assert !json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
63
+ assert_no_match %r{"preferences":\{"shows":"anime"\}}, json
64
64
  end
65
65
 
66
66
  def test_should_allow_attribute_filtering_with_except
67
67
  json = @contact.to_json(:except => [:name, :age])
68
68
 
69
- assert_no_match %r{"name": "Konata Izumi"}, json
70
- assert_no_match %r{"age": 16}, json
71
- assert_match %r{"awesome": true}, json
72
- assert json.include?(%("created_at": #{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
73
- assert_match %r{"preferences": \{"shows": "anime"\}}, json
69
+ assert_no_match %r{"name":"Konata Izumi"}, json
70
+ assert_no_match %r{"age":16}, json
71
+ assert_match %r{"awesome":true}, json
72
+ assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))}))
73
+ assert_match %r{"preferences":\{"shows":"anime"\}}, json
74
74
  end
75
75
 
76
76
  def test_methods_are_called_on_object
@@ -79,12 +79,12 @@ class JsonSerializationTest < ActiveRecord::TestCase
79
79
  def @contact.favorite_quote; "Constraints are liberating"; end
80
80
 
81
81
  # Single method.
82
- assert_match %r{"label": "Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
82
+ assert_match %r{"label":"Has cheezburger"}, @contact.to_json(:only => :name, :methods => :label)
83
83
 
84
84
  # Both methods.
85
85
  methods_json = @contact.to_json(:only => :name, :methods => [:label, :favorite_quote])
86
- assert_match %r{"label": "Has cheezburger"}, methods_json
87
- assert_match %r{"favorite_quote": "Constraints are liberating"}, methods_json
86
+ assert_match %r{"label":"Has cheezburger"}, methods_json
87
+ assert_match %r{"favorite_quote":"Constraints are liberating"}, methods_json
88
88
  end
89
89
  end
90
90
 
@@ -99,42 +99,42 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
99
99
  def test_includes_uses_association_name
100
100
  json = @david.to_json(:include => :posts)
101
101
 
102
- assert_match %r{"posts": \[}, json
102
+ assert_match %r{"posts":\[}, json
103
103
 
104
- assert_match %r{"id": 1}, json
105
- assert_match %r{"name": "David"}, json
104
+ assert_match %r{"id":1}, json
105
+ assert_match %r{"name":"David"}, json
106
106
 
107
- assert_match %r{"author_id": 1}, json
108
- assert_match %r{"title": "Welcome to the weblog"}, json
109
- assert_match %r{"body": "Such a lovely day"}, json
107
+ assert_match %r{"author_id":1}, json
108
+ assert_match %r{"title":"Welcome to the weblog"}, json
109
+ assert_match %r{"body":"Such a lovely day"}, json
110
110
 
111
- assert_match %r{"title": "So I was thinking"}, json
112
- assert_match %r{"body": "Like I hopefully always am"}, json
111
+ assert_match %r{"title":"So I was thinking"}, json
112
+ assert_match %r{"body":"Like I hopefully always am"}, json
113
113
  end
114
114
 
115
115
  def test_includes_uses_association_name_and_applies_attribute_filters
116
116
  json = @david.to_json(:include => { :posts => { :only => :title } })
117
117
 
118
- assert_match %r{"name": "David"}, json
119
- assert_match %r{"posts": \[}, json
118
+ assert_match %r{"name":"David"}, json
119
+ assert_match %r{"posts":\[}, json
120
120
 
121
- assert_match %r{"title": "Welcome to the weblog"}, json
122
- assert_no_match %r{"body": "Such a lovely day"}, json
121
+ assert_match %r{"title":"Welcome to the weblog"}, json
122
+ assert_no_match %r{"body":"Such a lovely day"}, json
123
123
 
124
- assert_match %r{"title": "So I was thinking"}, json
125
- assert_no_match %r{"body": "Like I hopefully always am"}, json
124
+ assert_match %r{"title":"So I was thinking"}, json
125
+ assert_no_match %r{"body":"Like I hopefully always am"}, json
126
126
  end
127
127
 
128
128
  def test_includes_fetches_second_level_associations
129
129
  json = @david.to_json(:include => { :posts => { :include => { :comments => { :only => :body } } } })
130
130
 
131
- assert_match %r{"name": "David"}, json
132
- assert_match %r{"posts": \[}, json
131
+ assert_match %r{"name":"David"}, json
132
+ assert_match %r{"posts":\[}, json
133
133
 
134
- assert_match %r{"comments": \[}, json
135
- assert_match %r{\{"body": "Thank you again for the welcome"\}}, json
136
- assert_match %r{\{"body": "Don't think too hard"\}}, json
137
- assert_no_match %r{"post_id": }, json
134
+ assert_match %r{"comments":\[}, json
135
+ assert_match %r{\{"body":"Thank you again for the welcome"\}}, json
136
+ assert_match %r{\{"body":"Don't think too hard"\}}, json
137
+ assert_no_match %r{"post_id":}, json
138
138
  end
139
139
 
140
140
  def test_includes_fetches_nth_level_associations
@@ -151,11 +151,11 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
151
151
  }
152
152
  })
153
153
 
154
- assert_match %r{"name": "David"}, json
155
- assert_match %r{"posts": \[}, json
154
+ assert_match %r{"name":"David"}, json
155
+ assert_match %r{"posts":\[}, json
156
156
 
157
- assert_match %r{"taggings": \[}, json
158
- assert_match %r{"tag": \{"name": "General"\}}, json
157
+ assert_match %r{"taggings":\[}, json
158
+ assert_match %r{"tag":\{"name":"General"\}}, json
159
159
  end
160
160
 
161
161
  def test_should_not_call_methods_on_associations_that_dont_respond
@@ -163,33 +163,33 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
163
163
  json = @david.to_json(:include => :posts, :methods => :favorite_quote)
164
164
 
165
165
  assert !@david.posts.first.respond_to?(:favorite_quote)
166
- assert_match %r{"favorite_quote": "Constraints are liberating"}, json
167
- assert_equal %r{"favorite_quote": }.match(json).size, 1
166
+ assert_match %r{"favorite_quote":"Constraints are liberating"}, json
167
+ assert_equal %r{"favorite_quote":}.match(json).size, 1
168
168
  end
169
169
 
170
170
  def test_should_allow_only_option_for_list_of_authors
171
171
  authors = [@david, @mary]
172
172
 
173
- assert_equal %([{"name": "David"}, {"name": "Mary"}]), authors.to_json(:only => :name)
173
+ assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name)
174
174
  end
175
175
 
176
176
  def test_should_allow_except_option_for_list_of_authors
177
177
  authors = [@david, @mary]
178
178
 
179
- assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id, :author_address_extra_id])
179
+ assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id])
180
180
  end
181
181
 
182
182
  def test_should_allow_includes_for_list_of_authors
183
183
  authors = [@david, @mary]
184
- json = authors.to_json(
184
+ json = ActiveSupport::JSON.encode(authors,
185
185
  :only => :name,
186
186
  :include => {
187
187
  :posts => { :only => :id }
188
188
  }
189
189
  )
190
190
 
191
- ['"name": "David"', '"posts": [', '{"id": 1}', '{"id": 2}', '{"id": 4}',
192
- '{"id": 5}', '{"id": 6}', '"name": "Mary"', '"posts": [{"id": 7}]'].each do |fragment|
191
+ ['"name":"David"', '"posts":[', '{"id":1}', '{"id":2}', '{"id":4}',
192
+ '{"id":5}', '{"id":6}', '"name":"Mary"', '"posts":[{"id":7}]'].each do |fragment|
193
193
  assert json.include?(fragment), json
194
194
  end
195
195
  end
@@ -200,6 +200,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
200
200
  2 => @mary
201
201
  }
202
202
 
203
- assert_equal %({"1": {"name": "David"}}), authors_hash.to_json(:only => [1, :name])
203
+ assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name])
204
204
  end
205
205
  end