activerecord 2.3.18 → 3.0.0.beta

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 (378) hide show
  1. data/CHANGELOG +105 -34
  2. data/examples/performance.rb +3 -39
  3. data/examples/simple.rb +14 -0
  4. data/lib/active_record.rb +81 -47
  5. data/lib/active_record/aggregations.rb +1 -3
  6. data/lib/active_record/association_preload.rb +39 -54
  7. data/lib/active_record/associations.rb +262 -419
  8. data/lib/active_record/associations/association_collection.rb +85 -100
  9. data/lib/active_record/associations/association_proxy.rb +20 -18
  10. data/lib/active_record/associations/belongs_to_association.rb +8 -8
  11. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +13 -35
  12. data/lib/active_record/associations/has_many_association.rb +11 -19
  13. data/lib/active_record/associations/has_many_through_association.rb +30 -183
  14. data/lib/active_record/associations/has_one_association.rb +10 -10
  15. data/lib/active_record/associations/has_one_through_association.rb +13 -11
  16. data/lib/active_record/associations/through_association_scope.rb +153 -0
  17. data/lib/active_record/attribute_methods.rb +17 -370
  18. data/lib/active_record/attribute_methods/before_type_cast.rb +33 -0
  19. data/lib/active_record/attribute_methods/dirty.rb +87 -0
  20. data/lib/active_record/attribute_methods/primary_key.rb +44 -0
  21. data/lib/active_record/attribute_methods/query.rb +37 -0
  22. data/lib/active_record/attribute_methods/read.rb +116 -0
  23. data/lib/active_record/attribute_methods/time_zone_conversion.rb +60 -0
  24. data/lib/active_record/attribute_methods/write.rb +37 -0
  25. data/lib/active_record/autosave_association.rb +20 -41
  26. data/lib/active_record/base.rb +357 -1180
  27. data/lib/active_record/batches.rb +10 -16
  28. data/lib/active_record/callbacks.rb +66 -126
  29. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +17 -13
  30. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +5 -25
  31. data/lib/active_record/connection_adapters/abstract/database_statements.rb +4 -43
  32. data/lib/active_record/connection_adapters/abstract/query_cache.rb +3 -2
  33. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -4
  34. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +1 -1
  35. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +18 -72
  36. data/lib/active_record/connection_adapters/abstract_adapter.rb +16 -49
  37. data/lib/active_record/connection_adapters/mysql_adapter.rb +15 -27
  38. data/lib/active_record/connection_adapters/postgresql_adapter.rb +84 -46
  39. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +9 -3
  40. data/lib/active_record/connection_adapters/sqlite_adapter.rb +34 -65
  41. data/lib/active_record/fixtures.rb +21 -25
  42. data/lib/active_record/locale/en.yml +9 -27
  43. data/lib/active_record/locking/optimistic.rb +16 -48
  44. data/lib/active_record/migration.rb +59 -46
  45. data/lib/active_record/named_scope.rb +85 -92
  46. data/lib/active_record/nested_attributes.rb +18 -24
  47. data/lib/active_record/observer.rb +18 -94
  48. data/lib/active_record/railtie.rb +83 -0
  49. data/lib/active_record/railties/controller_runtime.rb +38 -0
  50. data/lib/active_record/railties/databases.rake +477 -0
  51. data/lib/active_record/railties/subscriber.rb +27 -0
  52. data/lib/active_record/reflection.rb +2 -15
  53. data/lib/active_record/relation.rb +339 -0
  54. data/lib/active_record/relation/calculations.rb +259 -0
  55. data/lib/active_record/relation/finder_methods.rb +315 -0
  56. data/lib/active_record/relation/predicate_builder.rb +46 -0
  57. data/lib/active_record/relation/query_methods.rb +218 -0
  58. data/lib/active_record/relation/spawn_methods.rb +102 -0
  59. data/lib/active_record/schema_dumper.rb +10 -6
  60. data/lib/active_record/serialization.rb +31 -74
  61. data/lib/active_record/serializers/xml_serializer.rb +33 -121
  62. data/lib/active_record/session_store.rb +1 -9
  63. data/lib/active_record/test_case.rb +1 -3
  64. data/lib/active_record/timestamp.rb +7 -5
  65. data/lib/active_record/transactions.rb +9 -9
  66. data/lib/active_record/validations.rb +51 -1100
  67. data/lib/active_record/validations/associated.rb +47 -0
  68. data/lib/active_record/validations/uniqueness.rb +181 -0
  69. data/lib/active_record/version.rb +3 -3
  70. data/lib/generators/active_record.rb +30 -0
  71. data/lib/generators/active_record/migration/migration_generator.rb +25 -0
  72. data/lib/generators/active_record/migration/templates/migration.rb +11 -0
  73. data/lib/generators/active_record/model/model_generator.rb +33 -0
  74. data/lib/generators/active_record/model/templates/migration.rb +16 -0
  75. data/lib/generators/active_record/model/templates/model.rb +5 -0
  76. data/lib/generators/active_record/observer/observer_generator.rb +15 -0
  77. data/lib/generators/active_record/observer/templates/observer.rb +2 -0
  78. data/lib/generators/active_record/session_migration/session_migration_generator.rb +24 -0
  79. data/lib/generators/active_record/session_migration/templates/migration.rb +16 -0
  80. metadata +67 -325
  81. data/RUNNING_UNIT_TESTS +0 -36
  82. data/Rakefile +0 -268
  83. data/install.rb +0 -30
  84. data/lib/active_record/calculations.rb +0 -321
  85. data/lib/active_record/connection_adapters/abstract/database_limits.rb +0 -57
  86. data/lib/active_record/dirty.rb +0 -183
  87. data/lib/active_record/serializers/json_serializer.rb +0 -91
  88. data/lib/activerecord.rb +0 -2
  89. data/test/assets/example.log +0 -1
  90. data/test/assets/flowers.jpg +0 -0
  91. data/test/cases/aaa_create_tables_test.rb +0 -24
  92. data/test/cases/active_schema_test_mysql.rb +0 -122
  93. data/test/cases/active_schema_test_postgresql.rb +0 -24
  94. data/test/cases/adapter_test.rb +0 -144
  95. data/test/cases/aggregations_test.rb +0 -167
  96. data/test/cases/ar_schema_test.rb +0 -32
  97. data/test/cases/associations/belongs_to_associations_test.rb +0 -438
  98. data/test/cases/associations/callbacks_test.rb +0 -161
  99. data/test/cases/associations/cascaded_eager_loading_test.rb +0 -131
  100. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +0 -36
  101. data/test/cases/associations/eager_load_nested_include_test.rb +0 -131
  102. data/test/cases/associations/eager_load_nested_polymorphic_include.rb +0 -19
  103. data/test/cases/associations/eager_singularization_test.rb +0 -145
  104. data/test/cases/associations/eager_test.rb +0 -852
  105. data/test/cases/associations/extension_test.rb +0 -62
  106. data/test/cases/associations/habtm_join_table_test.rb +0 -56
  107. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +0 -827
  108. data/test/cases/associations/has_many_associations_test.rb +0 -1273
  109. data/test/cases/associations/has_many_through_associations_test.rb +0 -360
  110. data/test/cases/associations/has_one_associations_test.rb +0 -330
  111. data/test/cases/associations/has_one_through_associations_test.rb +0 -209
  112. data/test/cases/associations/inner_join_association_test.rb +0 -93
  113. data/test/cases/associations/inverse_associations_test.rb +0 -566
  114. data/test/cases/associations/join_model_test.rb +0 -712
  115. data/test/cases/associations_test.rb +0 -282
  116. data/test/cases/attribute_methods_test.rb +0 -305
  117. data/test/cases/autosave_association_test.rb +0 -1218
  118. data/test/cases/base_test.rb +0 -2166
  119. data/test/cases/batches_test.rb +0 -81
  120. data/test/cases/binary_test.rb +0 -30
  121. data/test/cases/calculations_test.rb +0 -360
  122. data/test/cases/callbacks_observers_test.rb +0 -38
  123. data/test/cases/callbacks_test.rb +0 -438
  124. data/test/cases/class_inheritable_attributes_test.rb +0 -32
  125. data/test/cases/column_alias_test.rb +0 -17
  126. data/test/cases/column_definition_test.rb +0 -70
  127. data/test/cases/connection_pool_test.rb +0 -25
  128. data/test/cases/connection_test_firebird.rb +0 -8
  129. data/test/cases/connection_test_mysql.rb +0 -65
  130. data/test/cases/copy_table_test_sqlite.rb +0 -80
  131. data/test/cases/counter_cache_test.rb +0 -84
  132. data/test/cases/database_statements_test.rb +0 -12
  133. data/test/cases/datatype_test_postgresql.rb +0 -204
  134. data/test/cases/date_time_test.rb +0 -37
  135. data/test/cases/default_test_firebird.rb +0 -16
  136. data/test/cases/defaults_test.rb +0 -111
  137. data/test/cases/deprecated_finder_test.rb +0 -30
  138. data/test/cases/dirty_test.rb +0 -316
  139. data/test/cases/finder_respond_to_test.rb +0 -76
  140. data/test/cases/finder_test.rb +0 -1098
  141. data/test/cases/fixtures_test.rb +0 -661
  142. data/test/cases/helper.rb +0 -68
  143. data/test/cases/i18n_test.rb +0 -46
  144. data/test/cases/inheritance_test.rb +0 -262
  145. data/test/cases/invalid_date_test.rb +0 -24
  146. data/test/cases/json_serialization_test.rb +0 -219
  147. data/test/cases/lifecycle_test.rb +0 -193
  148. data/test/cases/locking_test.rb +0 -350
  149. data/test/cases/method_scoping_test.rb +0 -704
  150. data/test/cases/migration_test.rb +0 -1649
  151. data/test/cases/migration_test_firebird.rb +0 -124
  152. data/test/cases/mixin_test.rb +0 -96
  153. data/test/cases/modules_test.rb +0 -109
  154. data/test/cases/multiple_db_test.rb +0 -85
  155. data/test/cases/named_scope_test.rb +0 -372
  156. data/test/cases/nested_attributes_test.rb +0 -840
  157. data/test/cases/pk_test.rb +0 -119
  158. data/test/cases/pooled_connections_test.rb +0 -103
  159. data/test/cases/query_cache_test.rb +0 -129
  160. data/test/cases/readonly_test.rb +0 -107
  161. data/test/cases/reflection_test.rb +0 -234
  162. data/test/cases/reload_models_test.rb +0 -22
  163. data/test/cases/repair_helper.rb +0 -50
  164. data/test/cases/reserved_word_test_mysql.rb +0 -176
  165. data/test/cases/sanitize_test.rb +0 -25
  166. data/test/cases/schema_authorization_test_postgresql.rb +0 -75
  167. data/test/cases/schema_dumper_test.rb +0 -211
  168. data/test/cases/schema_test_postgresql.rb +0 -178
  169. data/test/cases/serialization_test.rb +0 -47
  170. data/test/cases/sp_test_mysql.rb +0 -16
  171. data/test/cases/synonym_test_oracle.rb +0 -17
  172. data/test/cases/timestamp_test.rb +0 -75
  173. data/test/cases/transactions_test.rb +0 -543
  174. data/test/cases/unconnected_test.rb +0 -32
  175. data/test/cases/validations_i18n_test.rb +0 -925
  176. data/test/cases/validations_test.rb +0 -1684
  177. data/test/cases/xml_serialization_test.rb +0 -240
  178. data/test/cases/yaml_serialization_test.rb +0 -11
  179. data/test/config.rb +0 -5
  180. data/test/connections/jdbc_jdbcderby/connection.rb +0 -18
  181. data/test/connections/jdbc_jdbch2/connection.rb +0 -18
  182. data/test/connections/jdbc_jdbchsqldb/connection.rb +0 -18
  183. data/test/connections/jdbc_jdbcmysql/connection.rb +0 -26
  184. data/test/connections/jdbc_jdbcpostgresql/connection.rb +0 -26
  185. data/test/connections/jdbc_jdbcsqlite3/connection.rb +0 -25
  186. data/test/connections/native_db2/connection.rb +0 -25
  187. data/test/connections/native_firebird/connection.rb +0 -26
  188. data/test/connections/native_frontbase/connection.rb +0 -27
  189. data/test/connections/native_mysql/connection.rb +0 -25
  190. data/test/connections/native_openbase/connection.rb +0 -21
  191. data/test/connections/native_oracle/connection.rb +0 -27
  192. data/test/connections/native_postgresql/connection.rb +0 -21
  193. data/test/connections/native_sqlite/connection.rb +0 -25
  194. data/test/connections/native_sqlite3/connection.rb +0 -25
  195. data/test/connections/native_sqlite3/in_memory_connection.rb +0 -18
  196. data/test/connections/native_sybase/connection.rb +0 -23
  197. data/test/fixtures/accounts.yml +0 -29
  198. data/test/fixtures/all/developers.yml +0 -0
  199. data/test/fixtures/all/people.csv +0 -0
  200. data/test/fixtures/all/tasks.yml +0 -0
  201. data/test/fixtures/author_addresses.yml +0 -5
  202. data/test/fixtures/author_favorites.yml +0 -4
  203. data/test/fixtures/authors.yml +0 -9
  204. data/test/fixtures/binaries.yml +0 -132
  205. data/test/fixtures/books.yml +0 -7
  206. data/test/fixtures/categories.yml +0 -14
  207. data/test/fixtures/categories/special_categories.yml +0 -9
  208. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +0 -4
  209. data/test/fixtures/categories_ordered.yml +0 -7
  210. data/test/fixtures/categories_posts.yml +0 -23
  211. data/test/fixtures/categorizations.yml +0 -17
  212. data/test/fixtures/clubs.yml +0 -6
  213. data/test/fixtures/comments.yml +0 -59
  214. data/test/fixtures/companies.yml +0 -56
  215. data/test/fixtures/computers.yml +0 -4
  216. data/test/fixtures/courses.yml +0 -7
  217. data/test/fixtures/customers.yml +0 -26
  218. data/test/fixtures/developers.yml +0 -21
  219. data/test/fixtures/developers_projects.yml +0 -17
  220. data/test/fixtures/edges.yml +0 -6
  221. data/test/fixtures/entrants.yml +0 -14
  222. data/test/fixtures/faces.yml +0 -11
  223. data/test/fixtures/fk_test_has_fk.yml +0 -3
  224. data/test/fixtures/fk_test_has_pk.yml +0 -2
  225. data/test/fixtures/funny_jokes.yml +0 -10
  226. data/test/fixtures/interests.yml +0 -33
  227. data/test/fixtures/items.yml +0 -4
  228. data/test/fixtures/jobs.yml +0 -7
  229. data/test/fixtures/legacy_things.yml +0 -3
  230. data/test/fixtures/mateys.yml +0 -4
  231. data/test/fixtures/member_types.yml +0 -6
  232. data/test/fixtures/members.yml +0 -6
  233. data/test/fixtures/memberships.yml +0 -20
  234. data/test/fixtures/men.yml +0 -5
  235. data/test/fixtures/minimalistics.yml +0 -2
  236. data/test/fixtures/mixed_case_monkeys.yml +0 -6
  237. data/test/fixtures/mixins.yml +0 -29
  238. data/test/fixtures/movies.yml +0 -7
  239. data/test/fixtures/naked/csv/accounts.csv +0 -1
  240. data/test/fixtures/naked/yml/accounts.yml +0 -1
  241. data/test/fixtures/naked/yml/companies.yml +0 -1
  242. data/test/fixtures/naked/yml/courses.yml +0 -1
  243. data/test/fixtures/organizations.yml +0 -5
  244. data/test/fixtures/owners.yml +0 -7
  245. data/test/fixtures/parrots.yml +0 -27
  246. data/test/fixtures/parrots_pirates.yml +0 -7
  247. data/test/fixtures/people.yml +0 -15
  248. data/test/fixtures/pets.yml +0 -14
  249. data/test/fixtures/pirates.yml +0 -9
  250. data/test/fixtures/polymorphic_designs.yml +0 -19
  251. data/test/fixtures/polymorphic_prices.yml +0 -19
  252. data/test/fixtures/posts.yml +0 -52
  253. data/test/fixtures/price_estimates.yml +0 -7
  254. data/test/fixtures/projects.yml +0 -7
  255. data/test/fixtures/readers.yml +0 -9
  256. data/test/fixtures/references.yml +0 -17
  257. data/test/fixtures/reserved_words/distinct.yml +0 -5
  258. data/test/fixtures/reserved_words/distincts_selects.yml +0 -11
  259. data/test/fixtures/reserved_words/group.yml +0 -14
  260. data/test/fixtures/reserved_words/select.yml +0 -8
  261. data/test/fixtures/reserved_words/values.yml +0 -7
  262. data/test/fixtures/ships.yml +0 -5
  263. data/test/fixtures/sponsors.yml +0 -9
  264. data/test/fixtures/subscribers.yml +0 -7
  265. data/test/fixtures/subscriptions.yml +0 -12
  266. data/test/fixtures/taggings.yml +0 -28
  267. data/test/fixtures/tags.yml +0 -7
  268. data/test/fixtures/tasks.yml +0 -7
  269. data/test/fixtures/tees.yml +0 -4
  270. data/test/fixtures/ties.yml +0 -4
  271. data/test/fixtures/topics.yml +0 -42
  272. data/test/fixtures/toys.yml +0 -4
  273. data/test/fixtures/treasures.yml +0 -10
  274. data/test/fixtures/vertices.yml +0 -4
  275. data/test/fixtures/warehouse-things.yml +0 -3
  276. data/test/fixtures/zines.yml +0 -5
  277. data/test/migrations/broken/100_migration_that_raises_exception.rb +0 -10
  278. data/test/migrations/decimal/1_give_me_big_numbers.rb +0 -15
  279. data/test/migrations/duplicate/1_people_have_last_names.rb +0 -9
  280. data/test/migrations/duplicate/2_we_need_reminders.rb +0 -12
  281. data/test/migrations/duplicate/3_foo.rb +0 -7
  282. data/test/migrations/duplicate/3_innocent_jointable.rb +0 -12
  283. data/test/migrations/duplicate_names/20080507052938_chunky.rb +0 -7
  284. data/test/migrations/duplicate_names/20080507053028_chunky.rb +0 -7
  285. data/test/migrations/interleaved/pass_1/3_innocent_jointable.rb +0 -12
  286. data/test/migrations/interleaved/pass_2/1_people_have_last_names.rb +0 -9
  287. data/test/migrations/interleaved/pass_2/3_innocent_jointable.rb +0 -12
  288. data/test/migrations/interleaved/pass_3/1_people_have_last_names.rb +0 -9
  289. data/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb +0 -8
  290. data/test/migrations/interleaved/pass_3/3_innocent_jointable.rb +0 -12
  291. data/test/migrations/missing/1000_people_have_middle_names.rb +0 -9
  292. data/test/migrations/missing/1_people_have_last_names.rb +0 -9
  293. data/test/migrations/missing/3_we_need_reminders.rb +0 -12
  294. data/test/migrations/missing/4_innocent_jointable.rb +0 -12
  295. data/test/migrations/valid/1_people_have_last_names.rb +0 -9
  296. data/test/migrations/valid/2_we_need_reminders.rb +0 -12
  297. data/test/migrations/valid/3_innocent_jointable.rb +0 -12
  298. data/test/models/author.rb +0 -151
  299. data/test/models/auto_id.rb +0 -4
  300. data/test/models/binary.rb +0 -2
  301. data/test/models/bird.rb +0 -9
  302. data/test/models/book.rb +0 -4
  303. data/test/models/categorization.rb +0 -5
  304. data/test/models/category.rb +0 -34
  305. data/test/models/citation.rb +0 -6
  306. data/test/models/club.rb +0 -13
  307. data/test/models/column_name.rb +0 -3
  308. data/test/models/comment.rb +0 -29
  309. data/test/models/company.rb +0 -173
  310. data/test/models/company_in_module.rb +0 -78
  311. data/test/models/computer.rb +0 -3
  312. data/test/models/contact.rb +0 -16
  313. data/test/models/contract.rb +0 -5
  314. data/test/models/course.rb +0 -3
  315. data/test/models/customer.rb +0 -73
  316. data/test/models/default.rb +0 -2
  317. data/test/models/developer.rb +0 -101
  318. data/test/models/edge.rb +0 -5
  319. data/test/models/entrant.rb +0 -3
  320. data/test/models/essay.rb +0 -3
  321. data/test/models/event.rb +0 -3
  322. data/test/models/event_author.rb +0 -8
  323. data/test/models/face.rb +0 -7
  324. data/test/models/guid.rb +0 -2
  325. data/test/models/interest.rb +0 -5
  326. data/test/models/invoice.rb +0 -4
  327. data/test/models/item.rb +0 -7
  328. data/test/models/job.rb +0 -5
  329. data/test/models/joke.rb +0 -3
  330. data/test/models/keyboard.rb +0 -3
  331. data/test/models/legacy_thing.rb +0 -3
  332. data/test/models/line_item.rb +0 -3
  333. data/test/models/man.rb +0 -9
  334. data/test/models/matey.rb +0 -4
  335. data/test/models/member.rb +0 -12
  336. data/test/models/member_detail.rb +0 -5
  337. data/test/models/member_type.rb +0 -3
  338. data/test/models/membership.rb +0 -9
  339. data/test/models/minimalistic.rb +0 -2
  340. data/test/models/mixed_case_monkey.rb +0 -3
  341. data/test/models/movie.rb +0 -5
  342. data/test/models/order.rb +0 -4
  343. data/test/models/organization.rb +0 -6
  344. data/test/models/owner.rb +0 -5
  345. data/test/models/parrot.rb +0 -22
  346. data/test/models/person.rb +0 -16
  347. data/test/models/pet.rb +0 -5
  348. data/test/models/pirate.rb +0 -80
  349. data/test/models/polymorphic_design.rb +0 -3
  350. data/test/models/polymorphic_price.rb +0 -3
  351. data/test/models/post.rb +0 -102
  352. data/test/models/price_estimate.rb +0 -3
  353. data/test/models/project.rb +0 -30
  354. data/test/models/reader.rb +0 -4
  355. data/test/models/reference.rb +0 -4
  356. data/test/models/reply.rb +0 -46
  357. data/test/models/ship.rb +0 -19
  358. data/test/models/ship_part.rb +0 -7
  359. data/test/models/sponsor.rb +0 -4
  360. data/test/models/subject.rb +0 -4
  361. data/test/models/subscriber.rb +0 -8
  362. data/test/models/subscription.rb +0 -4
  363. data/test/models/tag.rb +0 -7
  364. data/test/models/tagging.rb +0 -10
  365. data/test/models/task.rb +0 -3
  366. data/test/models/tee.rb +0 -4
  367. data/test/models/tie.rb +0 -4
  368. data/test/models/topic.rb +0 -80
  369. data/test/models/toy.rb +0 -6
  370. data/test/models/treasure.rb +0 -8
  371. data/test/models/vertex.rb +0 -9
  372. data/test/models/warehouse_thing.rb +0 -5
  373. data/test/models/zine.rb +0 -3
  374. data/test/schema/mysql_specific_schema.rb +0 -31
  375. data/test/schema/postgresql_specific_schema.rb +0 -114
  376. data/test/schema/schema.rb +0 -550
  377. data/test/schema/schema2.rb +0 -6
  378. data/test/schema/sqlite_specific_schema.rb +0 -25
@@ -1,1273 +0,0 @@
1
- require "cases/helper"
2
- require 'models/developer'
3
- require 'models/project'
4
- require 'models/company'
5
- require 'models/topic'
6
- require 'models/reply'
7
- require 'models/category'
8
- require 'models/post'
9
- require 'models/author'
10
- require 'models/comment'
11
- require 'models/person'
12
- require 'models/reader'
13
- require 'models/tagging'
14
-
15
- class HasManyAssociationsTest < ActiveRecord::TestCase
16
- fixtures :accounts, :categories, :companies, :developers, :projects,
17
- :developers_projects, :topics, :authors, :comments, :author_addresses,
18
- :people, :posts, :readers, :taggings
19
-
20
- def setup
21
- Client.destroyed_client_ids.clear
22
- end
23
-
24
- def test_create_by
25
- person = Person.create! :first_name => 'tenderlove'
26
- post = Post.find :first
27
-
28
- assert_equal [], person.readers
29
- assert_nil person.readers.find_by_post_id(post.id)
30
-
31
- reader = person.readers.create_by_post_id post.id
32
-
33
- assert_equal 1, person.readers.count
34
- assert_equal 1, person.readers.length
35
- assert_equal post, person.readers.first.post
36
- assert_equal person, person.readers.first.person
37
- end
38
-
39
- def test_create_by_multi
40
- person = Person.create! :first_name => 'tenderlove'
41
- post = Post.find :first
42
-
43
- assert_equal [], person.readers
44
-
45
- reader = person.readers.create_by_post_id_and_skimmer post.id, false
46
-
47
- assert_equal 1, person.readers.count
48
- assert_equal 1, person.readers.length
49
- assert_equal post, person.readers.first.post
50
- assert_equal person, person.readers.first.person
51
- end
52
-
53
- def test_find_or_create_by
54
- person = Person.create! :first_name => 'tenderlove'
55
- post = Post.find :first
56
-
57
- assert_equal [], person.readers
58
- assert_nil person.readers.find_by_post_id(post.id)
59
-
60
- reader = person.readers.find_or_create_by_post_id post.id
61
-
62
- assert_equal 1, person.readers.count
63
- assert_equal 1, person.readers.length
64
- assert_equal post, person.readers.first.post
65
- assert_equal person, person.readers.first.person
66
- end
67
-
68
- def test_find_or_create_by_with_additional_parameters
69
- post = Post.create! :title => 'test_find_or_create_by_with_additional_parameters', :body => 'this is the body'
70
- comment = post.comments.create! :body => 'test comment body', :type => 'test'
71
-
72
- assert_equal comment, post.comments.find_or_create_by_body('test comment body')
73
-
74
- post.comments.find_or_create_by_body(:body => 'other test comment body', :type => 'test')
75
- assert_equal 2, post.comments.count
76
- assert_equal 2, post.comments.length
77
- post.comments.find_or_create_by_body('other other test comment body', :type => 'test')
78
- assert_equal 3, post.comments.count
79
- assert_equal 3, post.comments.length
80
- post.comments.find_or_create_by_body_and_type('3rd test comment body', 'test')
81
- assert_equal 4, post.comments.count
82
- assert_equal 4, post.comments.length
83
- end
84
-
85
- def test_find_or_create_by_with_same_parameters_creates_a_single_record
86
- author = Author.first
87
- assert_difference "Post.count", +1 do
88
- 2.times do
89
- author.posts.find_or_create_by_body_and_title('one', 'two')
90
- end
91
- end
92
- end
93
-
94
- def test_find_or_create_by_with_block
95
- post = Post.create! :title => 'test_find_or_create_by_with_additional_parameters', :body => 'this is the body'
96
- comment = post.comments.find_or_create_by_body('other test comment body') { |comment| comment.type = 'test' }
97
- assert_equal 'test', comment.type
98
- end
99
-
100
- def test_find_or_create
101
- person = Person.create! :first_name => 'tenderlove'
102
- post = Post.find :first
103
-
104
- assert_equal [], person.readers
105
- assert_nil person.readers.find(:first, :conditions => {
106
- :post_id => post.id
107
- })
108
-
109
- reader = person.readers.find_or_create :post_id => post.id
110
-
111
- assert_equal 1, person.readers.count
112
- assert_equal 1, person.readers.length
113
- assert_equal post, person.readers.first.post
114
- assert_equal person, person.readers.first.person
115
- end
116
-
117
-
118
- def force_signal37_to_load_all_clients_of_firm
119
- companies(:first_firm).clients_of_firm.each {|f| }
120
- end
121
-
122
- def test_counting_with_counter_sql
123
- assert_equal 2, Firm.find(:first).clients.count
124
- end
125
-
126
- def test_counting
127
- assert_equal 2, Firm.find(:first).plain_clients.count
128
- end
129
-
130
- def test_counting_with_empty_hash_conditions
131
- assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => {})
132
- end
133
-
134
- def test_counting_with_single_conditions
135
- assert_equal 1, Firm.find(:first).plain_clients.count(:conditions => ['name=?', "Microsoft"])
136
- end
137
-
138
- def test_counting_with_single_hash
139
- assert_equal 1, Firm.find(:first).plain_clients.count(:conditions => {:name => "Microsoft"})
140
- end
141
-
142
- def test_counting_with_column_name_and_hash
143
- assert_equal 2, Firm.find(:first).plain_clients.count(:name)
144
- end
145
-
146
- def test_counting_with_association_limit
147
- firm = companies(:first_firm)
148
- assert_equal firm.limited_clients.length, firm.limited_clients.size
149
- assert_equal firm.limited_clients.length, firm.limited_clients.count
150
- end
151
-
152
- def test_finding
153
- assert_equal 2, Firm.find(:first).clients.length
154
- end
155
-
156
- def test_find_with_blank_conditions
157
- [[], {}, nil, ""].each do |blank|
158
- assert_equal 2, Firm.find(:first).clients.find(:all, :conditions => blank).size
159
- end
160
- end
161
-
162
- def test_find_many_with_merged_options
163
- assert_equal 1, companies(:first_firm).limited_clients.size
164
- assert_equal 1, companies(:first_firm).limited_clients.find(:all).size
165
- assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
166
- end
167
-
168
- def test_dynamic_find_last_without_specified_order
169
- assert_equal companies(:second_client), companies(:first_firm).unsorted_clients.find_last_by_type('Client')
170
- end
171
-
172
- def test_dynamic_find_should_respect_association_order
173
- assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'")
174
- assert_equal companies(:second_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client')
175
- end
176
-
177
- def test_dynamic_find_order_should_override_association_order
178
- assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find(:first, :conditions => "type = 'Client'", :order => 'id')
179
- assert_equal companies(:first_client), companies(:first_firm).clients_sorted_desc.find_by_type('Client', :order => 'id')
180
- end
181
-
182
- def test_dynamic_find_all_should_respect_association_order
183
- assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'")
184
- assert_equal [companies(:second_client), companies(:first_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client')
185
- end
186
-
187
- def test_dynamic_find_all_order_should_override_association_order
188
- assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find(:all, :conditions => "type = 'Client'", :order => 'id')
189
- assert_equal [companies(:first_client), companies(:second_client)], companies(:first_firm).clients_sorted_desc.find_all_by_type('Client', :order => 'id')
190
- end
191
-
192
- def test_dynamic_find_all_should_respect_association_limit
193
- assert_equal 1, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'").length
194
- assert_equal 1, companies(:first_firm).limited_clients.find_all_by_type('Client').length
195
- end
196
-
197
- def test_dynamic_find_all_limit_should_override_association_limit
198
- assert_equal 2, companies(:first_firm).limited_clients.find(:all, :conditions => "type = 'Client'", :limit => 9_000).length
199
- assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length
200
- end
201
-
202
- def test_dynamic_find_all_should_respect_readonly_access
203
- companies(:first_firm).readonly_clients.find(:all).each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
204
- companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? }
205
- end
206
-
207
- def test_cant_save_has_many_readonly_association
208
- authors(:david).readonly_comments.each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save! } }
209
- authors(:david).readonly_comments.each { |c| assert c.readonly? }
210
- end
211
-
212
- def test_triple_equality
213
- assert !(Array === Firm.find(:first).clients)
214
- assert Firm.find(:first).clients === Array
215
- end
216
-
217
- def test_finding_default_orders
218
- assert_equal "Summit", Firm.find(:first).clients.first.name
219
- end
220
-
221
- def test_finding_with_different_class_name_and_order
222
- assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name
223
- end
224
-
225
- def test_finding_with_foreign_key
226
- assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name
227
- end
228
-
229
- def test_finding_with_condition
230
- assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
231
- end
232
-
233
- def test_finding_with_condition_hash
234
- assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name
235
- end
236
-
237
- def test_finding_using_primary_key
238
- assert_equal "Summit", Firm.find(:first).clients_using_primary_key.first.name
239
- end
240
-
241
- def test_finding_using_sql
242
- firm = Firm.find(:first)
243
- first_client = firm.clients_using_sql.first
244
- assert_not_nil first_client
245
- assert_equal "Microsoft", first_client.name
246
- assert_equal 1, firm.clients_using_sql.size
247
- assert_equal 1, Firm.find(:first).clients_using_sql.size
248
- end
249
-
250
- def test_counting_using_sql
251
- assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
252
- assert Firm.find(:first).clients_using_counter_sql.any?
253
- assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
254
- assert !Firm.find(:first).clients_using_zero_counter_sql.any?
255
- end
256
-
257
- def test_counting_non_existant_items_using_sql
258
- assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size
259
- end
260
-
261
- def test_belongs_to_sanity
262
- c = Client.new
263
- assert_nil c.firm
264
-
265
- if c.firm
266
- assert false, "belongs_to failed if check"
267
- end
268
-
269
- unless c.firm
270
- else
271
- assert false, "belongs_to failed unless check"
272
- end
273
- end
274
-
275
- def test_find_ids
276
- firm = Firm.find(:first)
277
-
278
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find }
279
-
280
- client = firm.clients.find(2)
281
- assert_kind_of Client, client
282
-
283
- client_ary = firm.clients.find([2])
284
- assert_kind_of Array, client_ary
285
- assert_equal client, client_ary.first
286
-
287
- client_ary = firm.clients.find(2, 3)
288
- assert_kind_of Array, client_ary
289
- assert_equal 2, client_ary.size
290
- assert_equal client, client_ary.first
291
-
292
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
293
- end
294
-
295
- def test_find_string_ids_when_using_finder_sql
296
- firm = Firm.find(:first)
297
-
298
- client = firm.clients_using_finder_sql.find("2")
299
- assert_kind_of Client, client
300
-
301
- client_ary = firm.clients_using_finder_sql.find(["2"])
302
- assert_kind_of Array, client_ary
303
- assert_equal client, client_ary.first
304
-
305
- client_ary = firm.clients_using_finder_sql.find("2", "3")
306
- assert_kind_of Array, client_ary
307
- assert_equal 2, client_ary.size
308
- assert client_ary.include?(client)
309
- end
310
-
311
- def test_find_all
312
- firm = Firm.find(:first)
313
- assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
314
- assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
315
- end
316
-
317
- def test_find_each
318
- firm = companies(:first_firm)
319
-
320
- assert ! firm.clients.loaded?
321
-
322
- assert_queries(3) do
323
- firm.clients.find_each(:batch_size => 1) {|c| assert_equal firm.id, c.firm_id }
324
- end
325
-
326
- assert ! firm.clients.loaded?
327
- end
328
-
329
- def test_find_each_with_conditions
330
- firm = companies(:first_firm)
331
-
332
- assert_queries(2) do
333
- firm.clients.find_each(:batch_size => 1, :conditions => {:name => "Microsoft"}) do |c|
334
- assert_equal firm.id, c.firm_id
335
- assert_equal "Microsoft", c.name
336
- end
337
- end
338
-
339
- assert ! firm.clients.loaded?
340
- end
341
-
342
- def test_find_in_batches
343
- firm = companies(:first_firm)
344
-
345
- assert ! firm.clients.loaded?
346
-
347
- assert_queries(2) do
348
- firm.clients.find_in_batches(:batch_size => 2) do |clients|
349
- clients.each {|c| assert_equal firm.id, c.firm_id }
350
- end
351
- end
352
-
353
- assert ! firm.clients.loaded?
354
- end
355
-
356
- def test_find_all_sanitized
357
- firm = Firm.find(:first)
358
- summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
359
- assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
360
- assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
361
- end
362
-
363
- def test_find_first
364
- firm = Firm.find(:first)
365
- client2 = Client.find(2)
366
- assert_equal firm.clients.first, firm.clients.find(:first)
367
- assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
368
- end
369
-
370
- def test_find_first_sanitized
371
- firm = Firm.find(:first)
372
- client2 = Client.find(2)
373
- assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
374
- assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
375
- end
376
-
377
- def test_find_all_with_include_and_conditions
378
- assert_nothing_raised do
379
- Developer.find(:all, :joins => :audit_logs, :conditions => {'audit_logs.message' => nil, :name => 'Smith'})
380
- end
381
- end
382
-
383
- def test_find_in_collection
384
- assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
385
- assert_raise(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
386
- end
387
-
388
- def test_find_grouped
389
- all_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1")
390
- grouped_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count')
391
- assert_equal 2, all_clients_of_firm1.size
392
- assert_equal 1, grouped_clients_of_firm1.size
393
- end
394
-
395
- def test_find_scoped_grouped
396
- assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.size
397
- assert_equal 1, companies(:first_firm).clients_grouped_by_firm_id.length
398
- assert_equal 2, companies(:first_firm).clients_grouped_by_name.size
399
- assert_equal 2, companies(:first_firm).clients_grouped_by_name.length
400
- end
401
-
402
- def test_find_scoped_grouped_having
403
- assert_equal 1, authors(:david).popular_grouped_posts.length
404
- assert_equal 0, authors(:mary).popular_grouped_posts.length
405
- end
406
-
407
- def test_adding
408
- force_signal37_to_load_all_clients_of_firm
409
- natural = Client.new("name" => "Natural Company")
410
- companies(:first_firm).clients_of_firm << natural
411
- assert_equal 2, companies(:first_firm).clients_of_firm.size # checking via the collection
412
- assert_equal 2, companies(:first_firm).clients_of_firm(true).size # checking using the db
413
- assert_equal natural, companies(:first_firm).clients_of_firm.last
414
- end
415
-
416
- def test_adding_using_create
417
- first_firm = companies(:first_firm)
418
- assert_equal 2, first_firm.plain_clients.size
419
- natural = first_firm.plain_clients.create(:name => "Natural Company")
420
- assert_equal 3, first_firm.plain_clients.length
421
- assert_equal 3, first_firm.plain_clients.size
422
- end
423
-
424
- def test_create_with_bang_on_has_many_when_parent_is_new_raises
425
- assert_raise(ActiveRecord::RecordNotSaved) do
426
- firm = Firm.new
427
- firm.plain_clients.create! :name=>"Whoever"
428
- end
429
- end
430
-
431
- def test_regular_create_on_has_many_when_parent_is_new_raises
432
- assert_raise(ActiveRecord::RecordNotSaved) do
433
- firm = Firm.new
434
- firm.plain_clients.create :name=>"Whoever"
435
- end
436
- end
437
-
438
- def test_create_with_bang_on_has_many_raises_when_record_not_saved
439
- assert_raise(ActiveRecord::RecordInvalid) do
440
- firm = Firm.find(:first)
441
- firm.plain_clients.create!
442
- end
443
- end
444
-
445
- def test_create_with_bang_on_habtm_when_parent_is_new_raises
446
- assert_raise(ActiveRecord::RecordNotSaved) do
447
- Developer.new("name" => "Aredridel").projects.create!
448
- end
449
- end
450
-
451
- def test_adding_a_mismatch_class
452
- assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
453
- assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
454
- assert_raise(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
455
- end
456
-
457
- def test_adding_a_collection
458
- force_signal37_to_load_all_clients_of_firm
459
- companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
460
- assert_equal 3, companies(:first_firm).clients_of_firm.size
461
- assert_equal 3, companies(:first_firm).clients_of_firm(true).size
462
- end
463
-
464
- def test_build
465
- company = companies(:first_firm)
466
- new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") }
467
- assert !company.clients_of_firm.loaded?
468
-
469
- assert_equal "Another Client", new_client.name
470
- assert new_client.new_record?
471
- assert_equal new_client, company.clients_of_firm.last
472
- end
473
-
474
- def test_collection_size_after_building
475
- company = companies(:first_firm) # company already has one client
476
- company.clients_of_firm.build("name" => "Another Client")
477
- company.clients_of_firm.build("name" => "Yet Another Client")
478
- assert_equal 3, company.clients_of_firm.size
479
- end
480
-
481
- def test_collection_size_twice_for_regressions
482
- post = posts(:thinking)
483
- assert_equal 0, post.readers.size
484
- # This test needs a post that has no readers, we assert it to ensure it holds,
485
- # but need to reload the post because the very call to #size hides the bug.
486
- post.reload
487
- post.readers.build
488
- size1 = post.readers.size
489
- size2 = post.readers.size
490
- assert_equal size1, size2
491
- end
492
-
493
- def test_build_many
494
- company = companies(:first_firm)
495
- new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) }
496
- assert_equal 2, new_clients.size
497
- end
498
-
499
- def test_build_followed_by_save_does_not_load_target
500
- new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client")
501
- assert companies(:first_firm).save
502
- assert !companies(:first_firm).clients_of_firm.loaded?
503
- end
504
-
505
- def test_build_without_loading_association
506
- first_topic = topics(:first)
507
- Reply.column_names
508
-
509
- assert_equal 1, first_topic.replies.length
510
-
511
- assert_no_queries do
512
- first_topic.replies.build(:title => "Not saved", :content => "Superstars")
513
- assert_equal 2, first_topic.replies.size
514
- end
515
-
516
- assert_equal 2, first_topic.replies.to_ary.size
517
- end
518
-
519
- def test_build_via_block
520
- company = companies(:first_firm)
521
- new_client = assert_no_queries { company.clients_of_firm.build {|client| client.name = "Another Client" } }
522
- assert !company.clients_of_firm.loaded?
523
-
524
- assert_equal "Another Client", new_client.name
525
- assert new_client.new_record?
526
- assert_equal new_client, company.clients_of_firm.last
527
- end
528
-
529
- def test_build_many_via_block
530
- company = companies(:first_firm)
531
- new_clients = assert_no_queries do
532
- company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) do |client|
533
- client.name = "changed"
534
- end
535
- end
536
-
537
- assert_equal 2, new_clients.size
538
- assert_equal "changed", new_clients.first.name
539
- assert_equal "changed", new_clients.last.name
540
- end
541
-
542
- def test_create_without_loading_association
543
- first_firm = companies(:first_firm)
544
- Firm.column_names
545
- Client.column_names
546
-
547
- assert_equal 1, first_firm.clients_of_firm.size
548
- first_firm.clients_of_firm.reset
549
-
550
- assert_queries(1) do
551
- first_firm.clients_of_firm.create(:name => "Superstars")
552
- end
553
-
554
- assert_equal 2, first_firm.clients_of_firm.size
555
- end
556
-
557
- def test_create
558
- force_signal37_to_load_all_clients_of_firm
559
- new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
560
- assert !new_client.new_record?
561
- assert_equal new_client, companies(:first_firm).clients_of_firm.last
562
- assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
563
- end
564
-
565
- def test_create_many
566
- companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
567
- assert_equal 3, companies(:first_firm).clients_of_firm(true).size
568
- end
569
-
570
- def test_create_followed_by_save_does_not_load_target
571
- new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
572
- assert companies(:first_firm).save
573
- assert !companies(:first_firm).clients_of_firm.loaded?
574
- end
575
-
576
- def test_find_or_initialize
577
- the_client = companies(:first_firm).clients.find_or_initialize_by_name("Yet another client")
578
- assert_equal companies(:first_firm).id, the_client.firm_id
579
- assert_equal "Yet another client", the_client.name
580
- assert the_client.new_record?
581
- end
582
-
583
- def test_find_or_create_updates_size
584
- number_of_clients = companies(:first_firm).clients.size
585
- the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client")
586
- assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
587
- assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client")
588
- assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
589
- end
590
-
591
- def test_deleting
592
- force_signal37_to_load_all_clients_of_firm
593
- companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
594
- assert_equal 0, companies(:first_firm).clients_of_firm.size
595
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
596
- end
597
-
598
- def test_deleting_before_save
599
- new_firm = Firm.new("name" => "A New Firm, Inc.")
600
- new_client = new_firm.clients_of_firm.build("name" => "Another Client")
601
- assert_equal 1, new_firm.clients_of_firm.size
602
- new_firm.clients_of_firm.delete(new_client)
603
- assert_equal 0, new_firm.clients_of_firm.size
604
- end
605
-
606
- def test_deleting_updates_counter_cache
607
- topic = Topic.first
608
- assert_equal topic.replies.to_a.size, topic.replies_count
609
-
610
- topic.replies.delete(topic.replies.first)
611
- topic.reload
612
- assert_equal topic.replies.to_a.size, topic.replies_count
613
- end
614
-
615
- def test_deleting_updates_counter_cache_without_dependent_destroy
616
- post = posts(:welcome)
617
-
618
- assert_difference "post.reload.taggings_count", -1 do
619
- post.taggings.delete(post.taggings.first)
620
- end
621
- end
622
-
623
- def test_deleting_a_collection
624
- force_signal37_to_load_all_clients_of_firm
625
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
626
- assert_equal 2, companies(:first_firm).clients_of_firm.size
627
- companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
628
- assert_equal 0, companies(:first_firm).clients_of_firm.size
629
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
630
- end
631
-
632
- def test_delete_all
633
- force_signal37_to_load_all_clients_of_firm
634
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
635
- assert_equal 2, companies(:first_firm).clients_of_firm.size
636
- companies(:first_firm).clients_of_firm.delete_all
637
- assert_equal 0, companies(:first_firm).clients_of_firm.size
638
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
639
- end
640
-
641
- def test_delete_all_with_not_yet_loaded_association_collection
642
- force_signal37_to_load_all_clients_of_firm
643
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
644
- assert_equal 2, companies(:first_firm).clients_of_firm.size
645
- companies(:first_firm).clients_of_firm.reset
646
- companies(:first_firm).clients_of_firm.delete_all
647
- assert_equal 0, companies(:first_firm).clients_of_firm.size
648
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
649
- end
650
-
651
- def test_clearing_an_association_collection
652
- firm = companies(:first_firm)
653
- client_id = firm.clients_of_firm.first.id
654
- assert_equal 1, firm.clients_of_firm.size
655
-
656
- firm.clients_of_firm.clear
657
-
658
- assert_equal 0, firm.clients_of_firm.size
659
- assert_equal 0, firm.clients_of_firm(true).size
660
- assert_equal [], Client.destroyed_client_ids[firm.id]
661
-
662
- # Should not be destroyed since the association is not dependent.
663
- assert_nothing_raised do
664
- assert Client.find(client_id).firm.nil?
665
- end
666
- end
667
-
668
- def test_clearing_updates_counter_cache
669
- topic = Topic.first
670
-
671
- topic.replies.clear
672
- topic.reload
673
- assert_equal 0, topic.replies_count
674
- end
675
-
676
- def test_clearing_a_dependent_association_collection
677
- firm = companies(:first_firm)
678
- client_id = firm.dependent_clients_of_firm.first.id
679
- assert_equal 1, firm.dependent_clients_of_firm.size
680
-
681
- # :dependent means destroy is called on each client
682
- firm.dependent_clients_of_firm.clear
683
-
684
- assert_equal 0, firm.dependent_clients_of_firm.size
685
- assert_equal 0, firm.dependent_clients_of_firm(true).size
686
- assert_equal [client_id], Client.destroyed_client_ids[firm.id]
687
-
688
- # Should be destroyed since the association is dependent.
689
- assert Client.find_by_id(client_id).nil?
690
- end
691
-
692
- def test_clearing_an_exclusively_dependent_association_collection
693
- firm = companies(:first_firm)
694
- client_id = firm.exclusively_dependent_clients_of_firm.first.id
695
- assert_equal 1, firm.exclusively_dependent_clients_of_firm.size
696
-
697
- assert_equal [], Client.destroyed_client_ids[firm.id]
698
-
699
- # :exclusively_dependent means each client is deleted directly from
700
- # the database without looping through them calling destroy.
701
- firm.exclusively_dependent_clients_of_firm.clear
702
-
703
- assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
704
- assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size
705
- # no destroy-filters should have been called
706
- assert_equal [], Client.destroyed_client_ids[firm.id]
707
-
708
- # Should be destroyed since the association is exclusively dependent.
709
- assert Client.find_by_id(client_id).nil?
710
- end
711
-
712
- def test_dependent_association_respects_optional_conditions_on_delete
713
- firm = companies(:odegy)
714
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
715
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
716
- # only one of two clients is included in the association due to the :conditions key
717
- assert_equal 2, Client.find_all_by_client_of(firm.id).size
718
- assert_equal 1, firm.dependent_conditional_clients_of_firm.size
719
- firm.destroy
720
- # only the correctly associated client should have been deleted
721
- assert_equal 1, Client.find_all_by_client_of(firm.id).size
722
- end
723
-
724
- def test_dependent_association_respects_optional_sanitized_conditions_on_delete
725
- firm = companies(:odegy)
726
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
727
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
728
- # only one of two clients is included in the association due to the :conditions key
729
- assert_equal 2, Client.find_all_by_client_of(firm.id).size
730
- assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
731
- firm.destroy
732
- # only the correctly associated client should have been deleted
733
- assert_equal 1, Client.find_all_by_client_of(firm.id).size
734
- end
735
-
736
- def test_dependent_association_respects_optional_hash_conditions_on_delete
737
- firm = companies(:odegy)
738
- Client.create(:client_of => firm.id, :name => "BigShot Inc.")
739
- Client.create(:client_of => firm.id, :name => "SmallTime Inc.")
740
- # only one of two clients is included in the association due to the :conditions key
741
- assert_equal 2, Client.find_all_by_client_of(firm.id).size
742
- assert_equal 1, firm.dependent_sanitized_conditional_clients_of_firm.size
743
- firm.destroy
744
- # only the correctly associated client should have been deleted
745
- assert_equal 1, Client.find_all_by_client_of(firm.id).size
746
- end
747
-
748
- def test_delete_all_association_with_primary_key_deletes_correct_records
749
- firm = Firm.find(:first)
750
- # break the vanilla firm_id foreign key
751
- assert_equal 2, firm.clients.count
752
- firm.clients.first.update_attribute(:firm_id, nil)
753
- assert_equal 1, firm.clients(true).count
754
- assert_equal 1, firm.clients_using_primary_key_with_delete_all.count
755
- old_record = firm.clients_using_primary_key_with_delete_all.first
756
- firm = Firm.find(:first)
757
- firm.destroy
758
- assert Client.find_by_id(old_record.id).nil?
759
- end
760
-
761
- def test_creation_respects_hash_condition
762
- ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.build
763
-
764
- assert ms_client.save
765
- assert_equal 'Microsoft', ms_client.name
766
-
767
- another_ms_client = companies(:first_firm).clients_like_ms_with_hash_conditions.create
768
-
769
- assert !another_ms_client.new_record?
770
- assert_equal 'Microsoft', another_ms_client.name
771
- end
772
-
773
- def test_dependent_delete_and_destroy_with_belongs_to
774
- author_address = author_addresses(:david_address)
775
- assert_equal [], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
776
-
777
- assert_difference "AuthorAddress.count", -2 do
778
- authors(:david).destroy
779
- end
780
-
781
- assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_id)
782
- assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_extra_id)
783
- end
784
-
785
- def test_invalid_belongs_to_dependent_option_raises_exception
786
- assert_raise ArgumentError do
787
- Author.belongs_to :special_author_address, :dependent => :nullify
788
- end
789
- end
790
-
791
- def test_clearing_without_initial_access
792
- firm = companies(:first_firm)
793
-
794
- firm.clients_of_firm.clear
795
-
796
- assert_equal 0, firm.clients_of_firm.size
797
- assert_equal 0, firm.clients_of_firm(true).size
798
- end
799
-
800
- def test_deleting_a_item_which_is_not_in_the_collection
801
- force_signal37_to_load_all_clients_of_firm
802
- summit = Client.find_by_name('Summit')
803
- companies(:first_firm).clients_of_firm.delete(summit)
804
- assert_equal 1, companies(:first_firm).clients_of_firm.size
805
- assert_equal 1, companies(:first_firm).clients_of_firm(true).size
806
- assert_equal 2, summit.client_of
807
- end
808
-
809
- def test_deleting_type_mismatch
810
- david = Developer.find(1)
811
- david.projects.reload
812
- assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
813
- end
814
-
815
- def test_deleting_self_type_mismatch
816
- david = Developer.find(1)
817
- david.projects.reload
818
- assert_raise(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
819
- end
820
-
821
- def test_destroying
822
- force_signal37_to_load_all_clients_of_firm
823
-
824
- assert_difference "Client.count", -1 do
825
- companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first)
826
- end
827
-
828
- assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
829
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
830
- end
831
-
832
- def test_destroying_by_fixnum_id
833
- force_signal37_to_load_all_clients_of_firm
834
-
835
- assert_difference "Client.count", -1 do
836
- companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id)
837
- end
838
-
839
- assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
840
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
841
- end
842
-
843
- def test_destroying_by_string_id
844
- force_signal37_to_load_all_clients_of_firm
845
-
846
- assert_difference "Client.count", -1 do
847
- companies(:first_firm).clients_of_firm.destroy(companies(:first_firm).clients_of_firm.first.id.to_s)
848
- end
849
-
850
- assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
851
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
852
- end
853
-
854
- def test_destroying_a_collection
855
- force_signal37_to_load_all_clients_of_firm
856
- companies(:first_firm).clients_of_firm.create("name" => "Another Client")
857
- assert_equal 2, companies(:first_firm).clients_of_firm.size
858
-
859
- assert_difference "Client.count", -2 do
860
- companies(:first_firm).clients_of_firm.destroy([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
861
- end
862
-
863
- assert_equal 0, companies(:first_firm).reload.clients_of_firm.size
864
- assert_equal 0, companies(:first_firm).clients_of_firm(true).size
865
- end
866
-
867
- def test_destroy_all
868
- force_signal37_to_load_all_clients_of_firm
869
- clients = companies(:first_firm).clients_of_firm.to_a
870
- assert !clients.empty?, "37signals has clients after load"
871
- destroyed = companies(:first_firm).clients_of_firm.destroy_all
872
- assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id)
873
- assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen"
874
- assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
875
- assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
876
- end
877
-
878
- def test_destroy_all_with_creates_and_scope_that_doesnt_match_created_records
879
- company = companies(:first_firm)
880
- unloaded_client_matching_scope = companies(:second_client)
881
- created_client_matching_scope = company.clients_of_firm.create!(:name => "Somesoft")
882
- created_client_not_matching_scope = company.clients_of_firm.create!(:name => "OtherCo")
883
- destroyed = company.clients_of_firm.with_oft_in_name.destroy_all
884
- assert destroyed.include?(unloaded_client_matching_scope), "unloaded clients matching the scope destroy_all on should have been destroyed"
885
- assert destroyed.include?(created_client_matching_scope), "loaded clients matching the scope destroy_all on should have been destroyed"
886
- assert !destroyed.include?(created_client_not_matching_scope), "loaded clients not matching the scope destroy_all on should not have been destroyed"
887
- end
888
-
889
- def test_dependence
890
- firm = companies(:first_firm)
891
- assert_equal 2, firm.clients.size
892
- firm.destroy
893
- assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty?
894
- end
895
-
896
- def test_dependence_for_associations_with_hash_condition
897
- david = authors(:david)
898
- post = posts(:thinking).id
899
- assert_difference('Post.count', -1) { assert david.destroy }
900
- end
901
-
902
- def test_destroy_dependent_when_deleted_from_association
903
- firm = Firm.find(:first)
904
- assert_equal 2, firm.clients.size
905
-
906
- client = firm.clients.first
907
- firm.clients.delete(client)
908
-
909
- assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
910
- assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
911
- assert_equal 1, firm.clients.size
912
- end
913
-
914
- def test_three_levels_of_dependence
915
- topic = Topic.create "title" => "neat and simple"
916
- reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
917
- silly_reply = reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
918
-
919
- assert_nothing_raised { topic.destroy }
920
- end
921
-
922
- uses_transaction :test_dependence_with_transaction_support_on_failure
923
- def test_dependence_with_transaction_support_on_failure
924
- firm = companies(:first_firm)
925
- clients = firm.clients
926
- assert_equal 2, clients.length
927
- clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
928
-
929
- firm.destroy rescue "do nothing"
930
-
931
- assert_equal 2, Client.find(:all, :conditions => "firm_id=#{firm.id}").size
932
- end
933
-
934
- def test_dependence_on_account
935
- num_accounts = Account.count
936
- companies(:first_firm).destroy
937
- assert_equal num_accounts - 1, Account.count
938
- end
939
-
940
- def test_depends_and_nullify
941
- num_accounts = Account.count
942
- num_companies = Company.count
943
-
944
- core = companies(:rails_core)
945
- assert_equal accounts(:rails_core_account), core.account
946
- assert_equal companies(:leetsoft, :jadedpixel), core.companies
947
- core.destroy
948
- assert_nil accounts(:rails_core_account).reload.firm_id
949
- assert_nil companies(:leetsoft).reload.client_of
950
- assert_nil companies(:jadedpixel).reload.client_of
951
-
952
-
953
- assert_equal num_accounts, Account.count
954
- end
955
-
956
- def test_included_in_collection
957
- assert companies(:first_firm).clients.include?(Client.find(2))
958
- end
959
-
960
- def test_adding_array_and_collection
961
- assert_nothing_raised { Firm.find(:first).clients + Firm.find(:all).last.clients }
962
- end
963
-
964
- def test_find_all_without_conditions
965
- firm = companies(:first_firm)
966
- assert_equal 2, firm.clients.find(:all).length
967
- end
968
-
969
- def test_replace_with_less
970
- firm = Firm.find(:first)
971
- firm.clients = [companies(:first_client)]
972
- assert firm.save, "Could not save firm"
973
- firm.reload
974
- assert_equal 1, firm.clients.length
975
- end
976
-
977
- def test_replace_with_less_and_dependent_nullify
978
- num_companies = Company.count
979
- companies(:rails_core).companies = []
980
- assert_equal num_companies, Company.count
981
- end
982
-
983
- def test_replace_with_new
984
- firm = Firm.find(:first)
985
- firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
986
- firm.save
987
- firm.reload
988
- assert_equal 2, firm.clients.length
989
- assert !firm.clients.include?(:first_client)
990
- end
991
-
992
- def test_get_ids
993
- assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
994
- end
995
-
996
- def test_get_ids_for_loaded_associations
997
- company = companies(:first_firm)
998
- company.clients(true)
999
- assert_queries(0) do
1000
- company.client_ids
1001
- company.client_ids
1002
- end
1003
- end
1004
-
1005
- def test_get_ids_for_unloaded_associations_does_not_load_them
1006
- company = companies(:first_firm)
1007
- assert !company.clients.loaded?
1008
- assert_equal [companies(:first_client).id, companies(:second_client).id], company.client_ids
1009
- assert !company.clients.loaded?
1010
- end
1011
-
1012
- def test_get_ids_for_unloaded_finder_sql_associations_loads_them
1013
- company = companies(:first_firm)
1014
- assert !company.clients_using_sql.loaded?
1015
- assert_equal [companies(:second_client).id], company.clients_using_sql_ids
1016
- assert company.clients_using_sql.loaded?
1017
- end
1018
-
1019
- def test_assign_ids_ignoring_blanks
1020
- firm = Firm.create!(:name => 'Apple')
1021
- firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
1022
- firm.save!
1023
-
1024
- assert_equal 2, firm.clients(true).size
1025
- assert firm.clients.include?(companies(:second_client))
1026
- end
1027
-
1028
- def test_get_ids_for_through
1029
- assert_equal [comments(:eager_other_comment1).id], authors(:mary).comment_ids
1030
- end
1031
-
1032
- def test_modifying_a_through_a_has_many_should_raise
1033
- [
1034
- lambda { authors(:mary).comment_ids = [comments(:greetings).id, comments(:more_greetings).id] },
1035
- lambda { authors(:mary).comments = [comments(:greetings), comments(:more_greetings)] },
1036
- lambda { authors(:mary).comments << Comment.create!(:body => "Yay", :post_id => 424242) },
1037
- lambda { authors(:mary).comments.delete(authors(:mary).comments.first) },
1038
- ].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) }
1039
- end
1040
-
1041
- def test_dynamic_find_should_respect_association_order_for_through
1042
- assert_equal Comment.find(10), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'")
1043
- assert_equal Comment.find(10), authors(:david).comments_desc.find_by_type('SpecialComment')
1044
- end
1045
-
1046
- def test_dynamic_find_order_should_override_association_order_for_through
1047
- assert_equal Comment.find(3), authors(:david).comments_desc.find(:first, :conditions => "comments.type = 'SpecialComment'", :order => 'comments.id')
1048
- assert_equal Comment.find(3), authors(:david).comments_desc.find_by_type('SpecialComment', :order => 'comments.id')
1049
- end
1050
-
1051
- def test_dynamic_find_all_should_respect_association_order_for_through
1052
- assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find(:all, :conditions => "comments.type = 'SpecialComment'")
1053
- assert_equal [Comment.find(10), Comment.find(7), Comment.find(6), Comment.find(3)], authors(:david).comments_desc.find_all_by_type('SpecialComment')
1054
- end
1055
-
1056
- def test_dynamic_find_all_order_should_override_association_order_for_through
1057
- assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find(:all, :conditions => "comments.type = 'SpecialComment'", :order => 'comments.id')
1058
- assert_equal [Comment.find(3), Comment.find(6), Comment.find(7), Comment.find(10)], authors(:david).comments_desc.find_all_by_type('SpecialComment', :order => 'comments.id')
1059
- end
1060
-
1061
- def test_dynamic_find_all_should_respect_association_limit_for_through
1062
- assert_equal 1, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'").length
1063
- assert_equal 1, authors(:david).limited_comments.find_all_by_type('SpecialComment').length
1064
- end
1065
-
1066
- def test_dynamic_find_all_order_should_override_association_limit_for_through
1067
- assert_equal 4, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'", :limit => 9_000).length
1068
- assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length
1069
- end
1070
-
1071
- def test_find_all_include_over_the_same_table_for_through
1072
- assert_equal 2, people(:michael).posts.find(:all, :include => :people).length
1073
- end
1074
-
1075
- def test_has_many_through_respects_hash_conditions
1076
- assert_equal authors(:david).hello_posts, authors(:david).hello_posts_with_hash_conditions
1077
- assert_equal authors(:david).hello_post_comments, authors(:david).hello_post_comments_with_hash_conditions
1078
- end
1079
-
1080
- def test_include_uses_array_include_after_loaded
1081
- firm = companies(:first_firm)
1082
- firm.clients.class # force load target
1083
-
1084
- client = firm.clients.first
1085
-
1086
- assert_no_queries do
1087
- assert firm.clients.loaded?
1088
- assert firm.clients.include?(client)
1089
- end
1090
- end
1091
-
1092
- def test_include_checks_if_record_exists_if_target_not_loaded
1093
- firm = companies(:first_firm)
1094
- client = firm.clients.first
1095
-
1096
- firm.reload
1097
- assert ! firm.clients.loaded?
1098
- assert_queries(1) do
1099
- assert firm.clients.include?(client)
1100
- end
1101
- assert ! firm.clients.loaded?
1102
- end
1103
-
1104
- def test_include_loads_collection_if_target_uses_finder_sql
1105
- firm = companies(:first_firm)
1106
- client = firm.clients_using_sql.first
1107
-
1108
- firm.reload
1109
- assert ! firm.clients_using_sql.loaded?
1110
- assert firm.clients_using_sql.include?(client)
1111
- assert firm.clients_using_sql.loaded?
1112
- end
1113
-
1114
-
1115
- def test_include_returns_false_for_non_matching_record_to_verify_scoping
1116
- firm = companies(:first_firm)
1117
- client = Client.create!(:name => 'Not Associated')
1118
-
1119
- assert ! firm.clients.loaded?
1120
- assert ! firm.clients.include?(client)
1121
- end
1122
-
1123
- def test_calling_first_or_last_on_association_should_not_load_association
1124
- firm = companies(:first_firm)
1125
- firm.clients.first
1126
- firm.clients.last
1127
- assert !firm.clients.loaded?
1128
- end
1129
-
1130
- def test_calling_first_or_last_on_loaded_association_should_not_fetch_with_query
1131
- firm = companies(:first_firm)
1132
- firm.clients.class # force load target
1133
- assert firm.clients.loaded?
1134
-
1135
- assert_no_queries do
1136
- firm.clients.first
1137
- assert_equal 2, firm.clients.first(2).size
1138
- firm.clients.last
1139
- assert_equal 2, firm.clients.last(2).size
1140
- end
1141
- end
1142
-
1143
- def test_calling_first_or_last_on_existing_record_with_build_should_load_association
1144
- firm = companies(:first_firm)
1145
- firm.clients.build(:name => 'Foo')
1146
- assert !firm.clients.loaded?
1147
-
1148
- assert_queries 1 do
1149
- firm.clients.first
1150
- firm.clients.last
1151
- end
1152
-
1153
- assert firm.clients.loaded?
1154
- end
1155
-
1156
- def test_calling_first_or_last_on_existing_record_with_create_should_not_load_association
1157
- firm = companies(:first_firm)
1158
- firm.clients.create(:name => 'Foo')
1159
- assert !firm.clients.loaded?
1160
-
1161
- assert_queries 2 do
1162
- firm.clients.first
1163
- firm.clients.last
1164
- end
1165
-
1166
- assert !firm.clients.loaded?
1167
- end
1168
-
1169
- def test_calling_first_or_last_on_new_record_should_not_run_queries
1170
- firm = Firm.new
1171
-
1172
- assert_no_queries do
1173
- firm.clients.first
1174
- firm.clients.last
1175
- end
1176
- end
1177
-
1178
- def test_calling_first_or_last_with_find_options_on_loaded_association_should_fetch_with_query
1179
- firm = companies(:first_firm)
1180
- firm.clients.class # force load target
1181
-
1182
- assert_queries 2 do
1183
- assert firm.clients.loaded?
1184
- firm.clients.first(:order => 'name')
1185
- firm.clients.last(:order => 'name')
1186
- end
1187
- end
1188
-
1189
- def test_calling_first_or_last_with_integer_on_association_should_load_association
1190
- firm = companies(:first_firm)
1191
-
1192
- assert_queries 1 do
1193
- firm.clients.first(2)
1194
- firm.clients.last(2)
1195
- end
1196
-
1197
- assert firm.clients.loaded?
1198
- end
1199
-
1200
- def test_joins_with_namespaced_model_should_use_correct_type
1201
- old = ActiveRecord::Base.store_full_sti_class
1202
- ActiveRecord::Base.store_full_sti_class = true
1203
-
1204
- firm = Namespaced::Firm.create({ :name => 'Some Company' })
1205
- firm.clients.create({ :name => 'Some Client' })
1206
-
1207
- stats = Namespaced::Firm.find(firm.id, {
1208
- :select => "#{Namespaced::Firm.table_name}.id, COUNT(#{Namespaced::Client.table_name}.id) AS num_clients",
1209
- :joins => :clients,
1210
- :group => "#{Namespaced::Firm.table_name}.id"
1211
- })
1212
- assert_equal 1, stats.num_clients.to_i
1213
-
1214
- ensure
1215
- ActiveRecord::Base.store_full_sti_class = old
1216
- end
1217
-
1218
- def test_association_proxy_transaction_method_starts_transaction_in_association_class
1219
- Comment.expects(:transaction)
1220
- Post.find(:first).comments.transaction do
1221
- # nothing
1222
- end
1223
- end
1224
-
1225
- def test_sending_new_to_association_proxy_should_have_same_effect_as_calling_new
1226
- client_association = companies(:first_firm).clients
1227
- assert_equal client_association.new.attributes, client_association.send(:new).attributes
1228
- end
1229
-
1230
- def test_respond_to_private_class_methods
1231
- client_association = companies(:first_firm).clients
1232
- assert !client_association.respond_to?(:private_method)
1233
- assert client_association.respond_to?(:private_method, true)
1234
- end
1235
-
1236
- def test_creating_using_primary_key
1237
- firm = Firm.find(:first)
1238
- client = firm.clients_using_primary_key.create!(:name => 'test')
1239
- assert_equal firm.name, client.firm_name
1240
- end
1241
-
1242
- def test_normal_method_call_in_association_proxy
1243
- assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.sort_by(&:id).first.title
1244
- end
1245
-
1246
- def test_instance_eval_in_association_proxy
1247
- assert_equal 'Welcome to the weblog', Comment.all.map { |comment| comment.post }.sort_by(&:id).first.instance_eval{title}
1248
- end
1249
-
1250
- def test_defining_has_many_association_with_delete_all_dependency_lazily_evaluates_target_class
1251
- ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
1252
- class_eval <<-EOF
1253
- class DeleteAllModel < ActiveRecord::Base
1254
- has_many :nonentities, :dependent => :delete_all
1255
- end
1256
- EOF
1257
- end
1258
-
1259
- def test_defining_has_many_association_with_nullify_dependency_lazily_evaluates_target_class
1260
- ActiveRecord::Reflection::AssociationReflection.any_instance.expects(:class_name).never
1261
- class_eval <<-EOF
1262
- class NullifyModel < ActiveRecord::Base
1263
- has_many :nonentities, :dependent => :nullify
1264
- end
1265
- EOF
1266
- end
1267
-
1268
- def test_include_method_in_has_many_association_should_return_true_for_instance_added_with_build
1269
- post = Post.new
1270
- comment = post.comments.build
1271
- assert post.comments.include?(comment)
1272
- end
1273
- end