activerecord 1.15.6 → 2.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 (185) hide show
  1. data/CHANGELOG +2454 -34
  2. data/README +1 -1
  3. data/RUNNING_UNIT_TESTS +3 -34
  4. data/Rakefile +98 -77
  5. data/install.rb +1 -1
  6. data/lib/active_record.rb +13 -22
  7. data/lib/active_record/aggregations.rb +38 -49
  8. data/lib/active_record/associations.rb +452 -333
  9. data/lib/active_record/associations/association_collection.rb +66 -20
  10. data/lib/active_record/associations/association_proxy.rb +9 -8
  11. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +46 -51
  12. data/lib/active_record/associations/has_many_association.rb +21 -57
  13. data/lib/active_record/associations/has_many_through_association.rb +38 -18
  14. data/lib/active_record/associations/has_one_association.rb +30 -14
  15. data/lib/active_record/attribute_methods.rb +253 -0
  16. data/lib/active_record/base.rb +719 -494
  17. data/lib/active_record/calculations.rb +62 -63
  18. data/lib/active_record/callbacks.rb +57 -83
  19. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +38 -9
  20. data/lib/active_record/connection_adapters/abstract/database_statements.rb +56 -15
  21. data/lib/active_record/connection_adapters/abstract/query_cache.rb +87 -0
  22. data/lib/active_record/connection_adapters/abstract/quoting.rb +23 -12
  23. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +191 -62
  24. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +37 -34
  25. data/lib/active_record/connection_adapters/abstract_adapter.rb +28 -17
  26. data/lib/active_record/connection_adapters/mysql_adapter.rb +119 -37
  27. data/lib/active_record/connection_adapters/postgresql_adapter.rb +473 -210
  28. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +34 -0
  29. data/lib/active_record/connection_adapters/sqlite_adapter.rb +91 -107
  30. data/lib/active_record/fixtures.rb +503 -113
  31. data/lib/active_record/locking/optimistic.rb +72 -34
  32. data/lib/active_record/migration.rb +80 -57
  33. data/lib/active_record/observer.rb +13 -10
  34. data/lib/active_record/query_cache.rb +16 -57
  35. data/lib/active_record/reflection.rb +35 -38
  36. data/lib/active_record/schema.rb +5 -5
  37. data/lib/active_record/schema_dumper.rb +35 -13
  38. data/lib/active_record/serialization.rb +98 -0
  39. data/lib/active_record/serializers/json_serializer.rb +71 -0
  40. data/lib/active_record/{xml_serialization.rb → serializers/xml_serializer.rb} +90 -83
  41. data/lib/active_record/timestamp.rb +20 -21
  42. data/lib/active_record/transactions.rb +39 -43
  43. data/lib/active_record/validations.rb +256 -107
  44. data/lib/active_record/version.rb +3 -3
  45. data/lib/activerecord.rb +1 -0
  46. data/test/aaa_create_tables_test.rb +15 -2
  47. data/test/abstract_unit.rb +24 -17
  48. data/test/active_schema_test_mysql.rb +20 -8
  49. data/test/adapter_test.rb +23 -5
  50. data/test/adapter_test_sqlserver.rb +15 -1
  51. data/test/aggregations_test.rb +16 -1
  52. data/test/all.sh +2 -2
  53. data/test/associations/ar_joins_test.rb +0 -0
  54. data/test/associations/callbacks_test.rb +51 -30
  55. data/test/associations/cascaded_eager_loading_test.rb +1 -29
  56. data/test/associations/eager_singularization_test.rb +145 -0
  57. data/test/associations/eager_test.rb +42 -6
  58. data/test/associations/extension_test.rb +6 -1
  59. data/test/associations/inner_join_association_test.rb +88 -0
  60. data/test/associations/join_model_test.rb +47 -16
  61. data/test/associations_test.rb +449 -226
  62. data/test/attribute_methods_test.rb +97 -0
  63. data/test/base_test.rb +251 -105
  64. data/test/binary_test.rb +22 -27
  65. data/test/calculations_test.rb +37 -5
  66. data/test/callbacks_test.rb +23 -0
  67. data/test/connection_test_firebird.rb +2 -2
  68. data/test/connection_test_mysql.rb +30 -0
  69. data/test/connections/native_mysql/connection.rb +3 -0
  70. data/test/connections/native_sqlite/connection.rb +5 -14
  71. data/test/connections/native_sqlite3/connection.rb +5 -14
  72. data/test/connections/native_sqlite3/in_memory_connection.rb +1 -1
  73. data/test/{copy_table_sqlite.rb → copy_table_test_sqlite.rb} +8 -3
  74. data/test/datatype_test_postgresql.rb +178 -27
  75. data/test/{empty_date_time_test.rb → date_time_test.rb} +13 -1
  76. data/test/defaults_test.rb +8 -1
  77. data/test/deprecated_finder_test.rb +7 -128
  78. data/test/finder_test.rb +192 -54
  79. data/test/fixtures/all/developers.yml +0 -0
  80. data/test/fixtures/all/people.csv +0 -0
  81. data/test/fixtures/all/tasks.yml +0 -0
  82. data/test/fixtures/author.rb +12 -5
  83. data/test/fixtures/binaries.yml +130 -435
  84. data/test/fixtures/category.rb +6 -0
  85. data/test/fixtures/company.rb +8 -1
  86. data/test/fixtures/computer.rb +1 -0
  87. data/test/fixtures/contact.rb +16 -0
  88. data/test/fixtures/customer.rb +2 -2
  89. data/test/fixtures/db_definitions/db2.drop.sql +1 -0
  90. data/test/fixtures/db_definitions/db2.sql +4 -0
  91. data/test/fixtures/db_definitions/firebird.drop.sql +3 -1
  92. data/test/fixtures/db_definitions/firebird.sql +6 -0
  93. data/test/fixtures/db_definitions/frontbase.drop.sql +1 -0
  94. data/test/fixtures/db_definitions/frontbase.sql +5 -0
  95. data/test/fixtures/db_definitions/openbase.sql +41 -25
  96. data/test/fixtures/db_definitions/oracle.drop.sql +2 -0
  97. data/test/fixtures/db_definitions/oracle.sql +5 -0
  98. data/test/fixtures/db_definitions/postgresql.drop.sql +7 -0
  99. data/test/fixtures/db_definitions/postgresql.sql +87 -58
  100. data/test/fixtures/db_definitions/postgresql2.sql +1 -2
  101. data/test/fixtures/db_definitions/schema.rb +280 -0
  102. data/test/fixtures/db_definitions/schema2.rb +11 -0
  103. data/test/fixtures/db_definitions/sqlite.drop.sql +1 -0
  104. data/test/fixtures/db_definitions/sqlite.sql +4 -0
  105. data/test/fixtures/db_definitions/sybase.drop.sql +1 -0
  106. data/test/fixtures/db_definitions/sybase.sql +4 -0
  107. data/test/fixtures/developer.rb +10 -0
  108. data/test/fixtures/example.log +1 -0
  109. data/test/fixtures/flowers.jpg +0 -0
  110. data/test/fixtures/item.rb +7 -0
  111. data/test/fixtures/items.yml +4 -0
  112. data/test/fixtures/joke.rb +0 -3
  113. data/test/fixtures/matey.rb +4 -0
  114. data/test/fixtures/mateys.yml +4 -0
  115. data/test/fixtures/minimalistic.rb +2 -0
  116. data/test/fixtures/minimalistics.yml +2 -0
  117. data/test/fixtures/mixins.yml +2 -100
  118. data/test/fixtures/parrot.rb +13 -0
  119. data/test/fixtures/parrots.yml +27 -0
  120. data/test/fixtures/parrots_pirates.yml +7 -0
  121. data/test/fixtures/pirate.rb +5 -0
  122. data/test/fixtures/pirates.yml +9 -0
  123. data/test/fixtures/post.rb +1 -0
  124. data/test/fixtures/project.rb +3 -2
  125. data/test/fixtures/reserved_words/distinct.yml +5 -0
  126. data/test/fixtures/reserved_words/distincts_selects.yml +11 -0
  127. data/test/fixtures/reserved_words/group.yml +14 -0
  128. data/test/fixtures/reserved_words/select.yml +8 -0
  129. data/test/fixtures/reserved_words/values.yml +7 -0
  130. data/test/fixtures/ship.rb +3 -0
  131. data/test/fixtures/ships.yml +5 -0
  132. data/test/fixtures/tagging.rb +4 -0
  133. data/test/fixtures/taggings.yml +8 -1
  134. data/test/fixtures/topic.rb +13 -1
  135. data/test/fixtures/treasure.rb +4 -0
  136. data/test/fixtures/treasures.yml +10 -0
  137. data/test/fixtures_test.rb +205 -24
  138. data/test/inheritance_test.rb +7 -1
  139. data/test/json_serialization_test.rb +180 -0
  140. data/test/lifecycle_test.rb +1 -1
  141. data/test/locking_test.rb +85 -2
  142. data/test/migration_test.rb +206 -40
  143. data/test/mixin_test.rb +13 -515
  144. data/test/pk_test.rb +3 -6
  145. data/test/query_cache_test.rb +104 -0
  146. data/test/reflection_test.rb +16 -0
  147. data/test/reserved_word_test_mysql.rb +177 -0
  148. data/test/schema_dumper_test.rb +38 -3
  149. data/test/serialization_test.rb +47 -0
  150. data/test/transactions_test.rb +74 -23
  151. data/test/unconnected_test.rb +1 -1
  152. data/test/validations_test.rb +322 -32
  153. data/test/xml_serialization_test.rb +121 -44
  154. metadata +48 -41
  155. data/examples/associations.rb +0 -87
  156. data/examples/shared_setup.rb +0 -15
  157. data/examples/validation.rb +0 -85
  158. data/lib/active_record/acts/list.rb +0 -256
  159. data/lib/active_record/acts/nested_set.rb +0 -211
  160. data/lib/active_record/acts/tree.rb +0 -96
  161. data/lib/active_record/connection_adapters/db2_adapter.rb +0 -228
  162. data/lib/active_record/connection_adapters/firebird_adapter.rb +0 -728
  163. data/lib/active_record/connection_adapters/frontbase_adapter.rb +0 -861
  164. data/lib/active_record/connection_adapters/openbase_adapter.rb +0 -350
  165. data/lib/active_record/connection_adapters/oracle_adapter.rb +0 -690
  166. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +0 -591
  167. data/lib/active_record/connection_adapters/sybase_adapter.rb +0 -662
  168. data/lib/active_record/deprecated_associations.rb +0 -104
  169. data/lib/active_record/deprecated_finders.rb +0 -44
  170. data/lib/active_record/vendor/simple.rb +0 -693
  171. data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
  172. data/lib/active_record/wrappings.rb +0 -58
  173. data/test/connections/native_sqlserver/connection.rb +0 -23
  174. data/test/connections/native_sqlserver_odbc/connection.rb +0 -25
  175. data/test/deprecated_associations_test.rb +0 -396
  176. data/test/fixtures/db_definitions/mysql.drop.sql +0 -32
  177. data/test/fixtures/db_definitions/mysql.sql +0 -234
  178. data/test/fixtures/db_definitions/mysql2.drop.sql +0 -2
  179. data/test/fixtures/db_definitions/mysql2.sql +0 -5
  180. data/test/fixtures/db_definitions/sqlserver.drop.sql +0 -34
  181. data/test/fixtures/db_definitions/sqlserver.sql +0 -243
  182. data/test/fixtures/db_definitions/sqlserver2.drop.sql +0 -2
  183. data/test/fixtures/db_definitions/sqlserver2.sql +0 -5
  184. data/test/fixtures/mixin.rb +0 -63
  185. data/test/mixin_nested_set_test.rb +0 -196
@@ -1,15 +0,0 @@
1
- require 'yaml'
2
-
3
- module ActiveRecord
4
- module Wrappings #:nodoc:
5
- class YamlWrapper < AbstractWrapper #:nodoc:
6
- def wrap(attribute) attribute.to_yaml end
7
- def unwrap(attribute) YAML::load(attribute) end
8
- end
9
-
10
- module ClassMethods #:nodoc:
11
- # Wraps the attribute in Yaml encoding
12
- def wrap_in_yaml(*attributes) wrap_with(YamlWrapper, attributes) end
13
- end
14
- end
15
- end
@@ -1,58 +0,0 @@
1
- module ActiveRecord
2
- # A plugin framework for wrapping attribute values before they go in and unwrapping them after they go out of the database.
3
- # This was intended primarily for YAML wrapping of arrays and hashes, but this behavior is now native in the Base class.
4
- # So for now this framework is laying dormant until a need pops up.
5
- module Wrappings #:nodoc:
6
- module ClassMethods #:nodoc:
7
- def wrap_with(wrapper, *attributes)
8
- [ attributes ].flat.each { |attribute| wrapper.wrap(attribute) }
9
- end
10
- end
11
-
12
- def self.included(base)
13
- base.extend(ClassMethods)
14
- end
15
-
16
- class AbstractWrapper #:nodoc:
17
- def self.wrap(attribute, record_binding) #:nodoc:
18
- %w( before_save after_save after_initialize ).each do |callback|
19
- eval "#{callback} #{name}.new('#{attribute}')", record_binding
20
- end
21
- end
22
-
23
- def initialize(attribute) #:nodoc:
24
- @attribute = attribute
25
- end
26
-
27
- def save_wrapped_attribute(record) #:nodoc:
28
- if record.attribute_present?(@attribute)
29
- record.send(
30
- "write_attribute",
31
- @attribute,
32
- wrap(record.send("read_attribute", @attribute))
33
- )
34
- end
35
- end
36
-
37
- def load_wrapped_attribute(record) #:nodoc:
38
- if record.attribute_present?(@attribute)
39
- record.send(
40
- "write_attribute",
41
- @attribute,
42
- unwrap(record.send("read_attribute", @attribute))
43
- )
44
- end
45
- end
46
-
47
- alias_method :before_save, :save_wrapped_attribute #:nodoc:
48
- alias_method :after_save, :load_wrapped_attribute #:nodoc:
49
- alias_method :after_initialize, :after_save #:nodoc:
50
-
51
- # Overwrite to implement the logic that'll take the regular attribute and wrap it.
52
- def wrap(attribute) end
53
-
54
- # Overwrite to implement the logic that'll take the wrapped attribute and unwrap it.
55
- def unwrap(attribute) end
56
- end
57
- end
58
- end
@@ -1,23 +0,0 @@
1
- print "Using native SQLServer\n"
2
- require_dependency 'fixtures/course'
3
- require 'logger'
4
-
5
- ActiveRecord::Base.logger = Logger.new("debug.log")
6
-
7
- ActiveRecord::Base.configurations = {
8
- 'arunit' => {
9
- :adapter => 'sqlserver',
10
- :host => 'localhost',
11
- :username => 'sa',
12
- :database => 'activerecord_unittest'
13
- },
14
- 'arunit2' => {
15
- :adapter => 'sqlserver',
16
- :host => 'localhost',
17
- :username => 'sa',
18
- :database => 'activerecord_unittest2'
19
- }
20
- }
21
-
22
- ActiveRecord::Base.establish_connection 'arunit'
23
- Course.establish_connection 'arunit2'
@@ -1,25 +0,0 @@
1
- print "Using native SQLServer via ODBC\n"
2
- require_dependency 'fixtures/course'
3
- require 'logger'
4
-
5
- ActiveRecord::Base.logger = Logger.new("debug.log")
6
-
7
- ActiveRecord::Base.configurations = {
8
- 'arunit' => {
9
- :adapter => 'sqlserver',
10
- :mode => 'ODBC',
11
- :host => 'localhost',
12
- :username => 'sa',
13
- :dsn => 'activerecord_unittest'
14
- },
15
- 'arunit2' => {
16
- :adapter => 'sqlserver',
17
- :mode => 'ODBC',
18
- :host => 'localhost',
19
- :username => 'sa',
20
- :dsn => 'activerecord_unittest2'
21
- }
22
- }
23
-
24
- ActiveRecord::Base.establish_connection 'arunit'
25
- Course.establish_connection 'arunit2'
@@ -1,396 +0,0 @@
1
- require 'abstract_unit'
2
- require 'fixtures/developer'
3
- require 'fixtures/project'
4
- require 'fixtures/company'
5
- require 'fixtures/topic'
6
- require 'fixtures/reply'
7
-
8
- # Can't declare new classes in test case methods, so tests before that
9
- bad_collection_keys = false
10
- begin
11
- class Car < ActiveRecord::Base; has_many :wheels, :name => "wheels"; end
12
- rescue ArgumentError
13
- bad_collection_keys = true
14
- end
15
- raise "ActiveRecord should have barked on bad collection keys" unless bad_collection_keys
16
-
17
-
18
- class DeprecatedAssociationWarningsTest < Test::Unit::TestCase
19
- def test_deprecation_warnings
20
- assert_deprecated('find_first') { Firm.find_first }
21
- assert_deprecated('find_all') { Firm.find_all }
22
- assert_deprecated('has_account?') { Firm.find(:first).has_account? }
23
- assert_deprecated('has_clients?') { Firm.find(:first).has_clients? }
24
- end
25
- end
26
-
27
- class DeprecatedAssociationsTest < Test::Unit::TestCase
28
- fixtures :accounts, :companies, :developers, :projects, :topics,
29
- :developers_projects
30
-
31
- def setup
32
- @firm = companies(:first_firm)
33
- end
34
-
35
- def test_has_many_find
36
- assert_equal 2, @firm.clients.length
37
- end
38
-
39
- def test_has_many_orders
40
- assert_equal "Summit", @firm.clients.first.name
41
- end
42
-
43
- def test_has_many_class_name
44
- assert_equal "Microsoft", @firm.clients_sorted_desc.first.name
45
- end
46
-
47
- def test_has_many_foreign_key
48
- assert_equal "Microsoft", @firm.clients_of_firm.first.name
49
- end
50
-
51
- def test_has_many_conditions
52
- assert_equal "Microsoft", @firm.clients_like_ms.first.name
53
- end
54
-
55
- def test_has_many_sql
56
- assert_equal "Microsoft", @firm.clients_using_sql.first.name
57
- assert_equal 1, @firm.clients_using_sql.count
58
- assert_equal 1, @firm.clients_using_sql.count
59
- end
60
-
61
- def test_has_many_counter_sql
62
- assert_equal 1, @firm.clients_using_counter_sql.count
63
- end
64
-
65
- def test_has_many_queries
66
- assert !@firm.clients.loaded?
67
- assert_deprecated 'has_clients?' do
68
- assert_queries(1) { assert @firm.has_clients? }
69
- end
70
- assert !@firm.clients.loaded?
71
- assert_deprecated 'clients_count' do
72
- assert_queries(1) { assert_equal 2, @firm.clients_count }
73
- end
74
- assert !@firm.clients.loaded?
75
- assert_queries(1) { @firm.clients.size }
76
- assert !@firm.clients.loaded?
77
- assert_queries(0) { @firm.clients }
78
- assert !@firm.clients.loaded?
79
- assert_queries(1) { @firm.clients.reload }
80
- assert @firm.clients.loaded?
81
- assert_queries(0) { @firm.clients.size }
82
- assert_queries(1) { @firm.clients.count }
83
- end
84
-
85
- def test_has_many_dependence
86
- count = Client.count
87
- Firm.find(:first).destroy
88
- assert_equal count - 2, Client.count
89
- end
90
-
91
- uses_transaction :test_has_many_dependence_with_transaction_support_on_failure
92
- def test_has_many_dependence_with_transaction_support_on_failure
93
- count = Client.count
94
-
95
- clients = @firm.clients
96
- clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
97
-
98
- @firm.destroy rescue "do nothing"
99
-
100
- assert_equal count, Client.count
101
- end
102
-
103
- def test_has_one_dependence
104
- num_accounts = Account.count
105
- assert_not_nil @firm.account
106
- @firm.destroy
107
- assert_equal num_accounts - 1, Account.count
108
- end
109
-
110
- def test_has_one_dependence_with_missing_association
111
- Account.destroy_all
112
- assert_nil @firm.account
113
- @firm.destroy
114
- end
115
-
116
- def test_belongs_to
117
- client = companies(:second_client)
118
- assert_deprecated('has_firm?') do
119
- assert companies(:second_client).has_firm?, "Microsoft should have a firm"
120
- end
121
- assert_equal companies(:first_firm), client.firm, "Microsoft should have a firm"
122
- end
123
-
124
- def test_belongs_to_with_different_class_name
125
- assert_equal @firm, companies(:second_client).firm_with_other_name
126
- end
127
-
128
- def test_belongs_to_with_condition
129
- assert_equal @firm, companies(:second_client).firm_with_condition
130
- end
131
-
132
- def test_belongs_to_equality
133
- assert_equal @firm, companies(:second_client).firm, 'Microsoft should have 37signals as firm'
134
- end
135
-
136
- def test_has_one
137
- assert_equal accounts(:signals37), @firm.account
138
- assert_deprecated 'has_account?' do
139
- assert @firm.has_account?, "37signals should have an account"
140
- end
141
- assert_deprecated 'firm?' do
142
- assert accounts(:signals37).firm?(@firm), "37signals account should be able to backtrack"
143
- end
144
- assert_deprecated 'has_firm?' do
145
- assert accounts(:signals37).has_firm?, "37signals account should be able to backtrack"
146
- end
147
-
148
- assert_nil accounts(:unknown).firm, "Unknown isn't linked"
149
- end
150
-
151
- def test_has_many_dependence_on_account
152
- num_accounts = Account.count
153
- @firm.destroy
154
- assert_equal num_accounts - 1, Account.count
155
- end
156
-
157
- def test_find_in
158
- assert_deprecated 'find_in_clients' do
159
- assert_equal companies(:first_client), @firm.find_in_clients(2)
160
- assert_raises(ActiveRecord::RecordNotFound) { @firm.find_in_clients(6) }
161
- end
162
- end
163
-
164
- def test_force_reload
165
- ActiveSupport::Deprecation.silence do
166
- firm = Firm.new("name" => "A New Firm, Inc")
167
- firm.save
168
- firm.clients.each {|c|} # forcing to load all clients
169
- assert firm.clients.empty?, "New firm shouldn't have client objects"
170
- assert !firm.has_clients?, "New firm shouldn't have clients"
171
- assert_equal 0, firm.clients_count, "New firm should have 0 clients"
172
-
173
- client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
174
- client.save
175
-
176
- assert firm.clients.empty?, "New firm should have cached no client objects"
177
- assert !firm.has_clients?, "New firm should have cached a no-clients response"
178
- assert_equal 0, firm.clients_count, "New firm should have cached 0 clients count"
179
-
180
- assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
181
- assert firm.has_clients?(true), "New firm should have reloaded with a have-clients response"
182
- assert_equal 1, firm.clients_count(true), "New firm should have reloaded clients count"
183
- end
184
- end
185
-
186
- def test_included_in_collection
187
- assert @firm.clients.include?(Client.find(2))
188
- end
189
-
190
- def test_build_to_collection
191
- count = @firm.clients_of_firm.count
192
- new_client = nil
193
- assert_deprecated 'build_to_clients_of_firm' do
194
- new_client = @firm.build_to_clients_of_firm("name" => "Another Client")
195
- end
196
- assert_equal "Another Client", new_client.name
197
- assert new_client.save
198
-
199
- assert_equal @firm, new_client.firm
200
- assert_equal count + 1, @firm.clients_of_firm.count
201
- end
202
-
203
- def test_create_in_collection
204
- assert_deprecated 'create_in_clients_of_firm' do
205
- assert_equal @firm.create_in_clients_of_firm("name" => "Another Client"), @firm.clients_of_firm(true).last
206
- end
207
- end
208
-
209
- def test_has_and_belongs_to_many
210
- david = Developer.find(1)
211
- assert_deprecated 'has_projects?' do
212
- assert david.has_projects?
213
- end
214
- assert_deprecated 'projects_count' do
215
- assert_equal 2, david.projects_count
216
- end
217
-
218
- active_record = Project.find(1)
219
- assert_deprecated 'has_developers?' do
220
- assert active_record.has_developers?
221
- end
222
- assert_deprecated 'developers_count' do
223
- assert_equal 3, active_record.developers_count
224
- end
225
- assert active_record.developers.include?(david)
226
- end
227
-
228
- def test_has_and_belongs_to_many_removing
229
- david = Developer.find(1)
230
- active_record = Project.find(1)
231
-
232
- assert_deprecated do
233
- david.remove_projects(active_record)
234
- assert_equal 1, david.projects_count
235
- assert_equal 2, active_record.developers_count
236
- end
237
- end
238
-
239
- def test_has_and_belongs_to_many_zero
240
- david = Developer.find(1)
241
- assert_deprecated do
242
- david.remove_projects Project.find_all
243
- assert_equal 0, david.projects_count
244
- assert !david.has_projects?
245
- end
246
- end
247
-
248
- def test_has_and_belongs_to_many_adding
249
- jamis = Developer.find(2)
250
- action_controller = Project.find(2)
251
-
252
- assert_deprecated do
253
- jamis.add_projects(action_controller)
254
- assert_equal 2, jamis.projects_count
255
- assert_equal 2, action_controller.developers_count
256
- end
257
- end
258
-
259
- def test_has_and_belongs_to_many_adding_from_the_project
260
- jamis = Developer.find(2)
261
- action_controller = Project.find(2)
262
-
263
- assert_deprecated do
264
- action_controller.add_developers(jamis)
265
- assert_equal 2, jamis.projects_count
266
- assert_equal 2, action_controller.developers_count
267
- end
268
- end
269
-
270
- def test_has_and_belongs_to_many_adding_a_collection
271
- aredridel = Developer.new("name" => "Aredridel")
272
- aredridel.save
273
-
274
- assert_deprecated do
275
- aredridel.add_projects([ Project.find(1), Project.find(2) ])
276
- assert_equal 2, aredridel.projects_count
277
- end
278
- end
279
-
280
- def test_belongs_to_counter
281
- topic = Topic.create("title" => "Apple", "content" => "hello world")
282
- assert_equal 0, topic.send(:read_attribute, "replies_count"), "No replies yet"
283
-
284
- reply = assert_deprecated { topic.create_in_replies("title" => "I'm saying no!", "content" => "over here") }
285
- assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply created"
286
-
287
- reply.destroy
288
- assert_equal 0, Topic.find(topic.id).send(:read_attribute, "replies_count"), "First reply deleted"
289
- end
290
-
291
- def test_natural_assignment_of_has_one
292
- apple = Firm.create("name" => "Apple")
293
- citibank = Account.create("credit_limit" => 10)
294
- apple.account = citibank
295
- assert_equal apple.id, citibank.firm_id
296
- end
297
-
298
- def test_natural_assignment_of_belongs_to
299
- apple = Firm.create("name" => "Apple")
300
- citibank = Account.create("credit_limit" => 10)
301
- citibank.firm = apple
302
- assert_equal apple.id, citibank.firm_id
303
- end
304
-
305
- def test_natural_assignment_of_has_many
306
- apple = Firm.create("name" => "Apple")
307
- natural = Client.create("name" => "Natural Company")
308
- apple.clients << natural
309
- assert_equal apple.id, natural.firm_id
310
- assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find(natural.id)
311
- apple.clients.delete natural
312
- assert_raises(ActiveRecord::RecordNotFound) {
313
- Firm.find(apple.id).clients.find(natural.id)
314
- }
315
- end
316
-
317
- def test_natural_adding_of_has_and_belongs_to_many
318
- rails = Project.create("name" => "Rails")
319
- ap = Project.create("name" => "Action Pack")
320
- john = Developer.create("name" => "John")
321
- mike = Developer.create("name" => "Mike")
322
- rails.developers << john
323
- rails.developers << mike
324
-
325
- assert_equal Developer.find(john.id), Project.find(rails.id).developers.find(john.id)
326
- assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find(mike.id)
327
- assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find(rails.id)
328
- assert_equal Project.find(rails.id), Developer.find(john.id).projects.find(rails.id)
329
- ap.developers << john
330
- assert_equal Developer.find(john.id), Project.find(ap.id).developers.find(john.id)
331
- assert_equal Project.find(ap.id), Developer.find(john.id).projects.find(ap.id)
332
-
333
- ap.developers.delete john
334
- assert_raises(ActiveRecord::RecordNotFound) {
335
- Project.find(ap.id).developers.find(john.id)
336
- }
337
- assert_raises(ActiveRecord::RecordNotFound) {
338
- Developer.find(john.id).projects.find(ap.id)
339
- }
340
- end
341
-
342
- def test_storing_in_pstore
343
- require "pstore"
344
- require "tmpdir"
345
- apple = Firm.create("name" => "Apple")
346
- natural = Client.new("name" => "Natural Company")
347
- apple.clients << natural
348
-
349
- db = PStore.new(File.join(Dir.tmpdir, "ar-pstore-association-test"))
350
- db.transaction do
351
- db["apple"] = apple
352
- end
353
-
354
- db = PStore.new(File.join(Dir.tmpdir, "ar-pstore-association-test"))
355
- db.transaction do
356
- assert_equal "Natural Company", db["apple"].clients.first.name
357
- end
358
- end
359
-
360
- def test_has_many_find_all
361
- assert_deprecated 'find_all_in_clients' do
362
- assert_equal 2, @firm.find_all_in_clients("#{QUOTED_TYPE} = 'Client'").length
363
- assert_equal 1, @firm.find_all_in_clients("name = 'Summit'").length
364
- end
365
- end
366
-
367
- def test_has_one
368
- assert_equal Account.find(1), @firm.account, "37signals should have an account"
369
- assert_equal @firm, Account.find(1).firm, "37signals account should be able to backtrack"
370
- assert_nil Account.find(2).firm, "Unknown isn't linked"
371
- end
372
-
373
- def test_has_one_build
374
- firm = Firm.new("name" => "GlobalMegaCorp")
375
- assert firm.save
376
-
377
- account = firm.build_account(:credit_limit => 1000)
378
- assert account.save
379
- assert_equal account, firm.account
380
- end
381
-
382
- def test_has_one_failing_build_association
383
- firm = Firm.new("name" => "GlobalMegaCorp")
384
- firm.save
385
-
386
- account = firm.build_account
387
- assert !account.save
388
- assert_equal "can't be empty", account.errors.on("credit_limit")
389
- end
390
-
391
- def test_has_one_create
392
- firm = Firm.new("name" => "GlobalMegaCorp")
393
- firm.save
394
- assert_equal firm.create_account("credit_limit" => 1000), firm.account
395
- end
396
- end