activerecord-nuodb-adapter 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (456) hide show
  1. data/.DS_Store +0 -0
  2. data/.travis.yml +26 -2
  3. data/{CONTRIBUTION.rdoc → CONTRIBUTION.md} +16 -15
  4. data/Gemfile +1 -1
  5. data/README.md +48 -0
  6. data/active_setup_linux.sh +97 -0
  7. data/activerecord-nuodb-adapter.gemspec +1 -1
  8. data/lib/active_record/connection_adapters/nuodb/version.rb +1 -1
  9. data/lib/active_record/connection_adapters/nuodb_adapter.rb +45 -9
  10. data/test/.DS_Store +0 -0
  11. data/test/.gitignore +1 -0
  12. data/test/active_record/connection_adapters/fake_adapter.rb +45 -0
  13. data/test/ar_schema_test.rb +70 -0
  14. data/test/assets/example.log +1 -0
  15. data/test/assets/flowers.jpg +0 -0
  16. data/test/assets/test.txt +1 -0
  17. data/test/base_test.rb +1481 -0
  18. data/test/cases/.DS_Store +0 -0
  19. data/test/cases/adapter_test.rb +211 -0
  20. data/test/cases/adapters/firebird/connection_test.rb +8 -0
  21. data/test/cases/adapters/firebird/default_test.rb +16 -0
  22. data/test/cases/adapters/firebird/migration_test.rb +124 -0
  23. data/test/cases/adapters/mysql/active_schema_test.rb +143 -0
  24. data/test/cases/adapters/mysql/case_sensitivity_test.rb +35 -0
  25. data/test/cases/adapters/mysql/connection_test.rb +172 -0
  26. data/test/cases/adapters/mysql/enum_test.rb +10 -0
  27. data/test/cases/adapters/mysql/mysql_adapter_test.rb +125 -0
  28. data/test/cases/adapters/mysql/quoting_test.rb +25 -0
  29. data/test/cases/adapters/mysql/reserved_word_test.rb +153 -0
  30. data/test/cases/adapters/mysql/schema_test.rb +62 -0
  31. data/test/cases/adapters/mysql/sp_test.rb +15 -0
  32. data/test/cases/adapters/mysql/sql_types_test.rb +14 -0
  33. data/test/cases/adapters/mysql/statement_pool_test.rb +23 -0
  34. data/test/cases/adapters/mysql2/active_schema_test.rb +143 -0
  35. data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -0
  36. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +35 -0
  37. data/test/cases/adapters/mysql2/connection_test.rb +97 -0
  38. data/test/cases/adapters/mysql2/enum_test.rb +10 -0
  39. data/test/cases/adapters/mysql2/explain_test.rb +26 -0
  40. data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
  41. data/test/cases/adapters/mysql2/schema_migrations_test.rb +26 -0
  42. data/test/cases/adapters/mysql2/schema_test.rb +70 -0
  43. data/test/cases/adapters/mysql2/sql_types_test.rb +14 -0
  44. data/test/cases/adapters/oracle/synonym_test.rb +17 -0
  45. data/test/cases/adapters/postgresql/active_schema_test.rb +58 -0
  46. data/test/cases/adapters/postgresql/array_test.rb +104 -0
  47. data/test/cases/adapters/postgresql/bytea_test.rb +104 -0
  48. data/test/cases/adapters/postgresql/connection_test.rb +163 -0
  49. data/test/cases/adapters/postgresql/datatype_test.rb +604 -0
  50. data/test/cases/adapters/postgresql/explain_test.rb +28 -0
  51. data/test/cases/adapters/postgresql/hstore_test.rb +200 -0
  52. data/test/cases/adapters/postgresql/json_test.rb +99 -0
  53. data/test/cases/adapters/postgresql/ltree_test.rb +41 -0
  54. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +288 -0
  55. data/test/cases/adapters/postgresql/quoting_test.rb +58 -0
  56. data/test/cases/adapters/postgresql/schema_authorization_test.rb +114 -0
  57. data/test/cases/adapters/postgresql/schema_test.rb +376 -0
  58. data/test/cases/adapters/postgresql/sql_types_test.rb +18 -0
  59. data/test/cases/adapters/postgresql/statement_pool_test.rb +39 -0
  60. data/test/cases/adapters/postgresql/timestamp_test.rb +113 -0
  61. data/test/cases/adapters/postgresql/utils_test.rb +20 -0
  62. data/test/cases/adapters/postgresql/uuid_test.rb +95 -0
  63. data/test/cases/adapters/postgresql/view_test.rb +49 -0
  64. data/test/cases/adapters/sqlite3/copy_table_test.rb +99 -0
  65. data/test/cases/adapters/sqlite3/explain_test.rb +26 -0
  66. data/test/cases/adapters/sqlite3/quoting_test.rb +102 -0
  67. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +366 -0
  68. data/test/cases/adapters/sqlite3/statement_pool_test.rb +24 -0
  69. data/test/cases/aggregations_test.rb +158 -0
  70. data/test/cases/ar_schema_test.rb +70 -0
  71. data/test/cases/associations/association_scope_test.rb +15 -0
  72. data/test/cases/associations/belongs_to_associations_test.rb +831 -0
  73. data/test/cases/associations/callbacks_test.rb +168 -0
  74. data/test/cases/associations/cascaded_eager_loading_test.rb +179 -0
  75. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +36 -0
  76. data/test/cases/associations/eager_load_nested_include_test.rb +128 -0
  77. data/test/cases/associations/eager_singularization_test.rb +145 -0
  78. data/test/cases/associations/eager_test.rb +1179 -0
  79. data/test/cases/associations/extension_test.rb +80 -0
  80. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +779 -0
  81. data/test/cases/associations/has_many_associations_test.rb +1699 -0
  82. data/test/cases/associations/has_many_through_associations_test.rb +950 -0
  83. data/test/cases/associations/has_one_associations_test.rb +525 -0
  84. data/test/cases/associations/has_one_through_associations_test.rb +317 -0
  85. data/test/cases/associations/inner_join_association_test.rb +115 -0
  86. data/test/cases/associations/inverse_associations_test.rb +631 -0
  87. data/test/cases/associations/join_dependency_test.rb +8 -0
  88. data/test/cases/associations/join_model_test.rb +750 -0
  89. data/test/cases/associations/nested_through_associations_test.rb +575 -0
  90. data/test/cases/associations_test.rb +344 -0
  91. data/test/cases/attribute_methods/read_test.rb +58 -0
  92. data/test/cases/attribute_methods/serialization_test.rb +29 -0
  93. data/test/cases/attribute_methods_test.rb +819 -0
  94. data/test/cases/autosave_association_test.rb +1447 -0
  95. data/test/cases/base_test.rb +1481 -0
  96. data/test/cases/batches_test.rb +173 -0
  97. data/test/cases/binary_test.rb +49 -0
  98. data/test/cases/bind_parameter_test.rb +89 -0
  99. data/test/cases/calculations_test.rb +589 -0
  100. data/test/cases/callbacks_test.rb +535 -0
  101. data/test/cases/clone_test.rb +40 -0
  102. data/test/cases/coders/yaml_column_test.rb +63 -0
  103. data/test/cases/column_alias_test.rb +17 -0
  104. data/test/cases/column_definition_test.rb +143 -0
  105. data/test/cases/column_test.rb +115 -0
  106. data/test/cases/connection_adapters/abstract_adapter_test.rb +62 -0
  107. data/test/cases/connection_adapters/connection_handler_test.rb +53 -0
  108. data/test/cases/connection_adapters/connection_specification_test.rb +12 -0
  109. data/test/cases/connection_adapters/quoting_test.rb +13 -0
  110. data/test/cases/connection_adapters/schema_cache_test.rb +56 -0
  111. data/test/cases/connection_management_test.rb +112 -0
  112. data/test/cases/connection_pool_test.rb +344 -0
  113. data/test/cases/connection_specification/resolver_test.rb +63 -0
  114. data/test/cases/core_test.rb +33 -0
  115. data/test/cases/counter_cache_test.rb +163 -0
  116. data/test/cases/custom_locking_test.rb +17 -0
  117. data/test/cases/database_statements_test.rb +19 -0
  118. data/test/cases/date_time_test.rb +43 -0
  119. data/test/cases/defaults_test.rb +214 -0
  120. data/test/cases/dirty_test.rb +626 -0
  121. data/test/cases/disconnected_test.rb +27 -0
  122. data/test/cases/dup_test.rb +136 -0
  123. data/test/cases/explain_subscriber_test.rb +59 -0
  124. data/test/cases/explain_test.rb +72 -0
  125. data/test/cases/finder_respond_to_test.rb +50 -0
  126. data/test/cases/finder_test.rb +886 -0
  127. data/test/cases/fixture_set/file_test.rb +83 -0
  128. data/test/cases/fixtures_test.rb +808 -0
  129. data/test/cases/forbidden_attributes_protection_test.rb +64 -0
  130. data/test/cases/habtm_destroy_order_test.rb +61 -0
  131. data/test/cases/helper.rb +151 -0
  132. data/test/cases/hot_compatibility_test.rb +54 -0
  133. data/test/cases/i18n_test.rb +45 -0
  134. data/test/cases/inheritance_test.rb +355 -0
  135. data/test/cases/integration_test.rb +87 -0
  136. data/test/cases/invalid_connection_test.rb +22 -0
  137. data/test/cases/invalid_date_test.rb +32 -0
  138. data/test/cases/invertible_migration_test.rb +259 -0
  139. data/test/cases/json_serialization_test.rb +300 -0
  140. data/test/cases/locking_test.rb +453 -0
  141. data/test/cases/log_subscriber_test.rb +129 -0
  142. data/test/cases/migration/change_schema_test.rb +379 -0
  143. data/test/cases/migration/change_table_test.rb +204 -0
  144. data/test/cases/migration/column_attributes_test.rb +186 -0
  145. data/test/cases/migration/column_positioning_test.rb +60 -0
  146. data/test/cases/migration/columns_test.rb +279 -0
  147. data/test/cases/migration/command_recorder_test.rb +247 -0
  148. data/test/cases/migration/create_join_table_test.rb +125 -0
  149. data/test/cases/migration/helper.rb +43 -0
  150. data/test/cases/migration/index_test.rb +209 -0
  151. data/test/cases/migration/logger_test.rb +37 -0
  152. data/test/cases/migration/references_index_test.rb +102 -0
  153. data/test/cases/migration/references_statements_test.rb +111 -0
  154. data/test/cases/migration/rename_table_test.rb +90 -0
  155. data/test/cases/migration/table_and_index_test.rb +24 -0
  156. data/test/cases/migration_test.rb +861 -0
  157. data/test/cases/migrator_test.rb +378 -0
  158. data/test/cases/mixin_test.rb +96 -0
  159. data/test/cases/modules_test.rb +143 -0
  160. data/test/cases/multiparameter_attributes_test.rb +350 -0
  161. data/test/cases/multiple_db_test.rb +108 -0
  162. data/test/cases/nested_attributes_test.rb +1056 -0
  163. data/test/cases/persistence_test.rb +802 -0
  164. data/test/cases/pooled_connections_test.rb +51 -0
  165. data/test/cases/primary_keys_test.rb +218 -0
  166. data/test/cases/query_cache_test.rb +276 -0
  167. data/test/cases/quoting_test.rb +229 -0
  168. data/test/cases/readonly_test.rb +111 -0
  169. data/test/cases/reaper_test.rb +81 -0
  170. data/test/cases/reflection_test.rb +386 -0
  171. data/test/cases/relation/where_chain_test.rb +80 -0
  172. data/test/cases/relation/where_test.rb +120 -0
  173. data/test/cases/relation_test.rb +315 -0
  174. data/test/cases/relations_test.rb +1559 -0
  175. data/test/cases/reload_models_test.rb +22 -0
  176. data/test/cases/result_test.rb +32 -0
  177. data/test/cases/sanitize_test.rb +34 -0
  178. data/test/cases/schema_dumper_test.rb +391 -0
  179. data/test/cases/scoping/default_scoping_test.rb +374 -0
  180. data/test/cases/scoping/named_scoping_test.rb +451 -0
  181. data/test/cases/scoping/relation_scoping_test.rb +331 -0
  182. data/test/cases/serialization_test.rb +68 -0
  183. data/test/cases/serialized_attribute_test.rb +252 -0
  184. data/test/cases/statement_cache_test.rb +64 -0
  185. data/test/cases/store_test.rb +153 -0
  186. data/test/cases/tasks/database_tasks_test.rb +315 -0
  187. data/test/cases/tasks/mysql_rake_test.rb +301 -0
  188. data/test/cases/tasks/postgresql_rake_test.rb +236 -0
  189. data/test/cases/tasks/sqlite_rake_test.rb +191 -0
  190. data/test/cases/test_case.rb +95 -0
  191. data/test/cases/test_simple.rb +194 -0
  192. data/test/cases/timestamp_test.rb +308 -0
  193. data/test/cases/transaction_callbacks_test.rb +317 -0
  194. data/test/cases/transaction_isolation_test.rb +114 -0
  195. data/test/cases/transactions_test.rb +595 -0
  196. data/test/cases/unconnected_test.rb +33 -0
  197. data/test/cases/validations/association_validation_test.rb +121 -0
  198. data/test/cases/validations/i18n_generate_message_validation_test.rb +76 -0
  199. data/test/cases/validations/i18n_validation_test.rb +89 -0
  200. data/test/cases/validations/presence_validation_test.rb +51 -0
  201. data/test/cases/validations/uniqueness_validation_test.rb +379 -0
  202. data/test/cases/validations_repair_helper.rb +23 -0
  203. data/test/cases/validations_test.rb +124 -0
  204. data/test/cases/xml_serialization_test.rb +455 -0
  205. data/test/cases/yaml_serialization_test.rb +52 -0
  206. data/test/config.example.yml +154 -0
  207. data/test/config.rb +5 -0
  208. data/test/database_statements_test.rb +19 -0
  209. data/test/fixtures/.gitignore +1 -0
  210. data/test/fixtures/accounts.yml +29 -0
  211. data/test/fixtures/admin/accounts.yml +2 -0
  212. data/test/fixtures/admin/randomly_named_a9.yml +7 -0
  213. data/test/fixtures/admin/randomly_named_b0.yml +7 -0
  214. data/test/fixtures/admin/users.yml +10 -0
  215. data/test/fixtures/all/developers.yml +0 -0
  216. data/test/fixtures/all/people.yml +0 -0
  217. data/test/fixtures/all/tasks.yml +0 -0
  218. data/test/fixtures/author_addresses.yml +5 -0
  219. data/test/fixtures/author_favorites.yml +4 -0
  220. data/test/fixtures/authors.yml +15 -0
  221. data/test/fixtures/binaries.yml +133 -0
  222. data/test/fixtures/books.yml +9 -0
  223. data/test/fixtures/cars.yml +9 -0
  224. data/test/fixtures/categories.yml +19 -0
  225. data/test/fixtures/categories/special_categories.yml +9 -0
  226. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  227. data/test/fixtures/categories_ordered.yml +7 -0
  228. data/test/fixtures/categories_posts.yml +31 -0
  229. data/test/fixtures/categorizations.yml +23 -0
  230. data/test/fixtures/clubs.yml +8 -0
  231. data/test/fixtures/collections.yml +3 -0
  232. data/test/fixtures/colleges.yml +3 -0
  233. data/test/fixtures/comments.yml +65 -0
  234. data/test/fixtures/companies.yml +59 -0
  235. data/test/fixtures/computers.yml +4 -0
  236. data/test/fixtures/courses.yml +8 -0
  237. data/test/fixtures/customers.yml +26 -0
  238. data/test/fixtures/dashboards.yml +6 -0
  239. data/test/fixtures/developers.yml +21 -0
  240. data/test/fixtures/developers_projects.yml +17 -0
  241. data/test/fixtures/dog_lovers.yml +7 -0
  242. data/test/fixtures/dogs.yml +4 -0
  243. data/test/fixtures/edges.yml +5 -0
  244. data/test/fixtures/entrants.yml +14 -0
  245. data/test/fixtures/essays.yml +6 -0
  246. data/test/fixtures/faces.yml +11 -0
  247. data/test/fixtures/fk_test_has_fk.yml +3 -0
  248. data/test/fixtures/fk_test_has_pk.yml +2 -0
  249. data/test/fixtures/friendships.yml +4 -0
  250. data/test/fixtures/funny_jokes.yml +10 -0
  251. data/test/fixtures/interests.yml +33 -0
  252. data/test/fixtures/items.yml +3 -0
  253. data/test/fixtures/jobs.yml +7 -0
  254. data/test/fixtures/legacy_things.yml +3 -0
  255. data/test/fixtures/mateys.yml +4 -0
  256. data/test/fixtures/member_details.yml +8 -0
  257. data/test/fixtures/member_types.yml +6 -0
  258. data/test/fixtures/members.yml +11 -0
  259. data/test/fixtures/memberships.yml +34 -0
  260. data/test/fixtures/men.yml +5 -0
  261. data/test/fixtures/minimalistics.yml +2 -0
  262. data/test/fixtures/minivans.yml +5 -0
  263. data/test/fixtures/mixed_case_monkeys.yml +6 -0
  264. data/test/fixtures/mixins.yml +29 -0
  265. data/test/fixtures/movies.yml +7 -0
  266. data/test/fixtures/naked/csv/accounts.csv +1 -0
  267. data/test/fixtures/naked/yml/accounts.yml +1 -0
  268. data/test/fixtures/naked/yml/companies.yml +1 -0
  269. data/test/fixtures/naked/yml/courses.yml +1 -0
  270. data/test/fixtures/organizations.yml +5 -0
  271. data/test/fixtures/other_topics.yml +42 -0
  272. data/test/fixtures/owners.yml +8 -0
  273. data/test/fixtures/parrots.yml +27 -0
  274. data/test/fixtures/parrots_pirates.yml +7 -0
  275. data/test/fixtures/people.yml +24 -0
  276. data/test/fixtures/peoples_treasures.yml +3 -0
  277. data/test/fixtures/pets.yml +19 -0
  278. data/test/fixtures/pirates.yml +9 -0
  279. data/test/fixtures/posts.yml +82 -0
  280. data/test/fixtures/price_estimates.yml +7 -0
  281. data/test/fixtures/products.yml +4 -0
  282. data/test/fixtures/projects.yml +7 -0
  283. data/test/fixtures/randomly_named_a9.yml +7 -0
  284. data/test/fixtures/ratings.yml +14 -0
  285. data/test/fixtures/readers.yml +11 -0
  286. data/test/fixtures/references.yml +17 -0
  287. data/test/fixtures/reserved_words/distinct.yml +5 -0
  288. data/test/fixtures/reserved_words/distinct_select.yml +11 -0
  289. data/test/fixtures/reserved_words/group.yml +14 -0
  290. data/test/fixtures/reserved_words/select.yml +8 -0
  291. data/test/fixtures/reserved_words/values.yml +7 -0
  292. data/test/fixtures/ships.yml +6 -0
  293. data/test/fixtures/speedometers.yml +8 -0
  294. data/test/fixtures/sponsors.yml +12 -0
  295. data/test/fixtures/string_key_objects.yml +7 -0
  296. data/test/fixtures/subscribers.yml +11 -0
  297. data/test/fixtures/subscriptions.yml +12 -0
  298. data/test/fixtures/taggings.yml +78 -0
  299. data/test/fixtures/tags.yml +11 -0
  300. data/test/fixtures/tasks.yml +7 -0
  301. data/test/fixtures/teapots.yml +3 -0
  302. data/test/fixtures/topics.yml +42 -0
  303. data/test/fixtures/toys.yml +14 -0
  304. data/test/fixtures/traffic_lights.yml +10 -0
  305. data/test/fixtures/treasures.yml +10 -0
  306. data/test/fixtures/variants.yml +4 -0
  307. data/test/fixtures/vegetables.yml +20 -0
  308. data/test/fixtures/vertices.yml +4 -0
  309. data/test/fixtures/warehouse-things.yml +3 -0
  310. data/test/fixtures/zines.yml +5 -0
  311. data/test/migration_test.rb +861 -0
  312. data/test/migrations/10_urban/9_add_expressions.rb +11 -0
  313. data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -0
  314. data/test/migrations/empty/.gitkeep +0 -0
  315. data/test/migrations/magic/1_currencies_have_symbols.rb +12 -0
  316. data/test/migrations/missing/1000_people_have_middle_names.rb +9 -0
  317. data/test/migrations/missing/1_people_have_last_names.rb +9 -0
  318. data/test/migrations/missing/3_we_need_reminders.rb +12 -0
  319. data/test/migrations/missing/4_innocent_jointable.rb +12 -0
  320. data/test/migrations/rename/1_we_need_things.rb +11 -0
  321. data/test/migrations/rename/2_rename_things.rb +9 -0
  322. data/test/migrations/to_copy/1_people_have_hobbies.rb +9 -0
  323. data/test/migrations/to_copy/2_people_have_descriptions.rb +9 -0
  324. data/test/migrations/to_copy2/1_create_articles.rb +7 -0
  325. data/test/migrations/to_copy2/2_create_comments.rb +7 -0
  326. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +9 -0
  327. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +9 -0
  328. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +9 -0
  329. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +7 -0
  330. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +7 -0
  331. data/test/migrations/valid/1_valid_people_have_last_names.rb +9 -0
  332. data/test/migrations/valid/2_we_need_reminders.rb +12 -0
  333. data/test/migrations/valid/3_innocent_jointable.rb +12 -0
  334. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +9 -0
  335. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +12 -0
  336. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +12 -0
  337. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +9 -0
  338. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +12 -0
  339. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +12 -0
  340. data/test/models/admin.rb +5 -0
  341. data/test/models/admin/account.rb +3 -0
  342. data/test/models/admin/randomly_named_c1.rb +3 -0
  343. data/test/models/admin/user.rb +39 -0
  344. data/test/models/aircraft.rb +4 -0
  345. data/test/models/arunit2_model.rb +3 -0
  346. data/test/models/author.rb +196 -0
  347. data/test/models/auto_id.rb +4 -0
  348. data/test/models/autoloadable/extra_firm.rb +2 -0
  349. data/test/models/binary.rb +2 -0
  350. data/test/models/bird.rb +12 -0
  351. data/test/models/book.rb +9 -0
  352. data/test/models/boolean.rb +2 -0
  353. data/test/models/bulb.rb +45 -0
  354. data/test/models/car.rb +28 -0
  355. data/test/models/categorization.rb +19 -0
  356. data/test/models/category.rb +34 -0
  357. data/test/models/citation.rb +6 -0
  358. data/test/models/club.rb +15 -0
  359. data/test/models/college.rb +5 -0
  360. data/test/models/column_name.rb +3 -0
  361. data/test/models/comment.rb +38 -0
  362. data/test/models/company.rb +214 -0
  363. data/test/models/company_in_module.rb +80 -0
  364. data/test/models/computer.rb +3 -0
  365. data/test/models/contact.rb +40 -0
  366. data/test/models/contract.rb +19 -0
  367. data/test/models/country.rb +7 -0
  368. data/test/models/course.rb +6 -0
  369. data/test/models/customer.rb +77 -0
  370. data/test/models/dashboard.rb +3 -0
  371. data/test/models/default.rb +2 -0
  372. data/test/models/developer.rb +250 -0
  373. data/test/models/dog.rb +5 -0
  374. data/test/models/dog_lover.rb +5 -0
  375. data/test/models/edge.rb +5 -0
  376. data/test/models/electron.rb +3 -0
  377. data/test/models/engine.rb +4 -0
  378. data/test/models/entrant.rb +3 -0
  379. data/test/models/essay.rb +5 -0
  380. data/test/models/event.rb +3 -0
  381. data/test/models/eye.rb +37 -0
  382. data/test/models/face.rb +7 -0
  383. data/test/models/friendship.rb +6 -0
  384. data/test/models/guid.rb +2 -0
  385. data/test/models/interest.rb +5 -0
  386. data/test/models/invoice.rb +4 -0
  387. data/test/models/item.rb +7 -0
  388. data/test/models/job.rb +7 -0
  389. data/test/models/joke.rb +7 -0
  390. data/test/models/keyboard.rb +3 -0
  391. data/test/models/legacy_thing.rb +3 -0
  392. data/test/models/lesson.rb +11 -0
  393. data/test/models/line_item.rb +3 -0
  394. data/test/models/liquid.rb +4 -0
  395. data/test/models/man.rb +9 -0
  396. data/test/models/matey.rb +4 -0
  397. data/test/models/member.rb +36 -0
  398. data/test/models/member_detail.rb +7 -0
  399. data/test/models/member_type.rb +3 -0
  400. data/test/models/membership.rb +15 -0
  401. data/test/models/minimalistic.rb +2 -0
  402. data/test/models/minivan.rb +9 -0
  403. data/test/models/mixed_case_monkey.rb +3 -0
  404. data/test/models/molecule.rb +4 -0
  405. data/test/models/movie.rb +5 -0
  406. data/test/models/order.rb +4 -0
  407. data/test/models/organization.rb +12 -0
  408. data/test/models/owner.rb +5 -0
  409. data/test/models/parrot.rb +29 -0
  410. data/test/models/person.rb +128 -0
  411. data/test/models/pet.rb +15 -0
  412. data/test/models/pirate.rb +86 -0
  413. data/test/models/possession.rb +3 -0
  414. data/test/models/post.rb +194 -0
  415. data/test/models/price_estimate.rb +4 -0
  416. data/test/models/project.rb +30 -0
  417. data/test/models/randomly_named_c1.rb +3 -0
  418. data/test/models/rating.rb +4 -0
  419. data/test/models/reader.rb +21 -0
  420. data/test/models/reference.rb +22 -0
  421. data/test/models/reply.rb +61 -0
  422. data/test/models/ship.rb +19 -0
  423. data/test/models/ship_part.rb +7 -0
  424. data/test/models/shop.rb +12 -0
  425. data/test/models/speedometer.rb +6 -0
  426. data/test/models/sponsor.rb +7 -0
  427. data/test/models/string_key_object.rb +3 -0
  428. data/test/models/student.rb +3 -0
  429. data/test/models/subject.rb +16 -0
  430. data/test/models/subscriber.rb +8 -0
  431. data/test/models/subscription.rb +4 -0
  432. data/test/models/tag.rb +7 -0
  433. data/test/models/tagging.rb +13 -0
  434. data/test/models/task.rb +5 -0
  435. data/test/models/topic.rb +121 -0
  436. data/test/models/toy.rb +6 -0
  437. data/test/models/traffic_light.rb +4 -0
  438. data/test/models/treasure.rb +11 -0
  439. data/test/models/treaty.rb +7 -0
  440. data/test/models/tyre.rb +3 -0
  441. data/test/models/vegetables.rb +24 -0
  442. data/test/models/vertex.rb +9 -0
  443. data/test/models/warehouse_thing.rb +5 -0
  444. data/test/models/wheel.rb +3 -0
  445. data/test/models/without_table.rb +3 -0
  446. data/test/models/zine.rb +3 -0
  447. data/test/schema/mysql2_specific_schema.rb +58 -0
  448. data/test/schema/mysql_specific_schema.rb +70 -0
  449. data/test/schema/oracle_specific_schema.rb +46 -0
  450. data/test/schema/postgresql_specific_schema.rb +224 -0
  451. data/test/schema/schema.rb +809 -0
  452. data/test/schema/sqlite_specific_schema.rb +25 -0
  453. data/test/support/config.rb +43 -0
  454. data/test/support/connection.rb +21 -0
  455. metadata +898 -6
  456. data/README.rdoc +0 -42
@@ -0,0 +1,108 @@
1
+ require "cases/helper"
2
+ require 'models/entrant'
3
+ require 'models/bird'
4
+ require 'models/course'
5
+
6
+ class MultipleDbTest < ActiveRecord::TestCase
7
+ self.use_transactional_fixtures = false
8
+
9
+ def setup
10
+ @courses = create_fixtures("courses") { Course.retrieve_connection }
11
+ @colleges = create_fixtures("colleges") { College.retrieve_connection }
12
+ @entrants = create_fixtures("entrants")
13
+ end
14
+
15
+ def test_connected
16
+ assert_not_nil Entrant.connection
17
+ assert_not_nil Course.connection
18
+ end
19
+
20
+ def test_proper_connection
21
+ assert_not_equal(Entrant.connection, Course.connection)
22
+ assert_equal(Entrant.connection, Entrant.retrieve_connection)
23
+ assert_equal(Course.connection, Course.retrieve_connection)
24
+ assert_equal(ActiveRecord::Base.connection, Entrant.connection)
25
+ end
26
+
27
+ def test_find
28
+ c1 = Course.find(1)
29
+ assert_equal "Ruby Development", c1.name
30
+ c2 = Course.find(2)
31
+ assert_equal "Java Development", c2.name
32
+ e1 = Entrant.find(1)
33
+ assert_equal "Ruby Developer", e1.name
34
+ e2 = Entrant.find(2)
35
+ assert_equal "Ruby Guru", e2.name
36
+ e3 = Entrant.find(3)
37
+ assert_equal "Java Lover", e3.name
38
+ end
39
+
40
+ def test_associations
41
+ c1 = Course.find(1)
42
+ assert_equal 2, c1.entrants.count
43
+ e1 = Entrant.find(1)
44
+ assert_equal e1.course.id, c1.id
45
+ c2 = Course.find(2)
46
+ assert_equal 1, c2.entrants.count
47
+ e3 = Entrant.find(3)
48
+ assert_equal e3.course.id, c2.id
49
+ end
50
+
51
+ def test_course_connection_should_survive_dependency_reload
52
+ assert Course.connection
53
+
54
+ ActiveSupport::Dependencies.clear
55
+ Object.send(:remove_const, :Course)
56
+ require_dependency 'models/course'
57
+
58
+ assert Course.connection
59
+ end
60
+
61
+ def test_transactions_across_databases
62
+ c1 = Course.find(1)
63
+ e1 = Entrant.find(1)
64
+
65
+ begin
66
+ Course.transaction do
67
+ Entrant.transaction do
68
+ c1.name = "Typo"
69
+ e1.name = "Typo"
70
+ c1.save
71
+ e1.save
72
+ raise "No I messed up."
73
+ end
74
+ end
75
+ rescue
76
+ # Yup caught it
77
+ end
78
+
79
+ assert_equal "Typo", c1.name
80
+ assert_equal "Typo", e1.name
81
+
82
+ assert_equal "Ruby Development", Course.find(1).name
83
+ assert_equal "Ruby Developer", Entrant.find(1).name
84
+ end
85
+
86
+ def test_arel_table_engines
87
+ assert_not_equal Entrant.arel_engine, Bird.arel_engine
88
+ assert_not_equal Entrant.arel_engine, Course.arel_engine
89
+ end
90
+
91
+ def test_connection
92
+ assert_equal Entrant.arel_engine.connection, Bird.arel_engine.connection
93
+ assert_not_equal Entrant.arel_engine.connection, Course.arel_engine.connection
94
+ end
95
+
96
+ unless in_memory_db?
97
+ def test_associations_should_work_when_model_has_no_connection
98
+ begin
99
+ ActiveRecord::Base.remove_connection
100
+ assert_nothing_raised ActiveRecord::ConnectionNotEstablished do
101
+ College.first.courses.first
102
+ end
103
+ ensure
104
+ ActiveRecord::Base.establish_connection 'arunit'
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,1056 @@
1
+ require "cases/helper"
2
+ require "models/pirate"
3
+ require "models/ship"
4
+ require "models/ship_part"
5
+ require "models/bird"
6
+ require "models/parrot"
7
+ require "models/treasure"
8
+ require "models/man"
9
+ require "models/interest"
10
+ require "models/owner"
11
+ require "models/pet"
12
+ require 'active_support/hash_with_indifferent_access'
13
+
14
+ module AssertRaiseWithMessage
15
+ def assert_raise_with_message(expected_exception, expected_message)
16
+ begin
17
+ error_raised = false
18
+ yield
19
+ rescue expected_exception => error
20
+ error_raised = true
21
+ actual_message = error.message
22
+ end
23
+ assert error_raised
24
+ assert_equal expected_message, actual_message
25
+ end
26
+ end
27
+
28
+ class TestNestedAttributesInGeneral < ActiveRecord::TestCase
29
+ include AssertRaiseWithMessage
30
+
31
+ def teardown
32
+ Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
33
+ end
34
+
35
+ def test_base_should_have_an_empty_nested_attributes_options
36
+ assert_equal Hash.new, ActiveRecord::Base.nested_attributes_options
37
+ end
38
+
39
+ def test_should_add_a_proc_to_nested_attributes_options
40
+ assert_equal ActiveRecord::NestedAttributes::ClassMethods::REJECT_ALL_BLANK_PROC,
41
+ Pirate.nested_attributes_options[:birds_with_reject_all_blank][:reject_if]
42
+
43
+ [:parrots, :birds].each do |name|
44
+ assert_instance_of Proc, Pirate.nested_attributes_options[name][:reject_if]
45
+ end
46
+ end
47
+
48
+ def test_should_not_build_a_new_record_using_reject_all_even_if_destroy_is_given
49
+ pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
50
+ pirate.birds_with_reject_all_blank_attributes = [{:name => '', :color => '', :_destroy => '0'}]
51
+ pirate.save!
52
+
53
+ assert pirate.birds_with_reject_all_blank.empty?
54
+ end
55
+
56
+ def test_should_not_build_a_new_record_if_reject_all_blank_returns_false
57
+ pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
58
+ pirate.birds_with_reject_all_blank_attributes = [{:name => '', :color => ''}]
59
+ pirate.save!
60
+
61
+ assert pirate.birds_with_reject_all_blank.empty?
62
+ end
63
+
64
+ def test_should_build_a_new_record_if_reject_all_blank_does_not_return_false
65
+ pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
66
+ pirate.birds_with_reject_all_blank_attributes = [{:name => 'Tweetie', :color => ''}]
67
+ pirate.save!
68
+
69
+ assert_equal 1, pirate.birds_with_reject_all_blank.count
70
+ assert_equal 'Tweetie', pirate.birds_with_reject_all_blank.first.name
71
+ end
72
+
73
+ def test_should_raise_an_ArgumentError_for_non_existing_associations
74
+ assert_raise_with_message ArgumentError, "No association found for name `honesty'. Has it been defined yet?" do
75
+ Pirate.accepts_nested_attributes_for :honesty
76
+ end
77
+ end
78
+
79
+ def test_should_disable_allow_destroy_by_default
80
+ Pirate.accepts_nested_attributes_for :ship
81
+
82
+ pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
83
+ ship = pirate.create_ship(name: 'Nights Dirty Lightning')
84
+
85
+ pirate.update(ship_attributes: { '_destroy' => true, :id => ship.id })
86
+
87
+ assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.ship.reload }
88
+ end
89
+
90
+ def test_a_model_should_respond_to_underscore_destroy_and_return_if_it_is_marked_for_destruction
91
+ ship = Ship.create!(:name => 'Nights Dirty Lightning')
92
+ assert !ship._destroy
93
+ ship.mark_for_destruction
94
+ assert ship._destroy
95
+ end
96
+
97
+ def test_reject_if_method_without_arguments
98
+ Pirate.accepts_nested_attributes_for :ship, :reject_if => :new_record?
99
+
100
+ pirate = Pirate.new(:catchphrase => "Stop wastin' me time")
101
+ pirate.ship_attributes = { :name => 'Black Pearl' }
102
+ assert_no_difference('Ship.count') { pirate.save! }
103
+ end
104
+
105
+ def test_reject_if_method_with_arguments
106
+ Pirate.accepts_nested_attributes_for :ship, :reject_if => :reject_empty_ships_on_create
107
+
108
+ pirate = Pirate.new(:catchphrase => "Stop wastin' me time")
109
+ pirate.ship_attributes = { :name => 'Red Pearl', :_reject_me_if_new => true }
110
+ assert_no_difference('Ship.count') { pirate.save! }
111
+
112
+ # pirate.reject_empty_ships_on_create returns false for saved pirate records
113
+ # in the previous step note that pirate gets saved but ship fails
114
+ pirate.ship_attributes = { :name => 'Red Pearl', :_reject_me_if_new => true }
115
+ assert_difference('Ship.count') { pirate.save! }
116
+ end
117
+
118
+ def test_reject_if_with_indifferent_keys
119
+ Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| attributes[:name].blank? }
120
+
121
+ pirate = Pirate.new(:catchphrase => "Stop wastin' me time")
122
+ pirate.ship_attributes = { :name => 'Hello Pearl' }
123
+ assert_difference('Ship.count') { pirate.save! }
124
+ end
125
+
126
+ def test_reject_if_with_a_proc_which_returns_true_always_for_has_one
127
+ Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| true }
128
+ pirate = Pirate.new(catchphrase: "Stop wastin' me time")
129
+ ship = pirate.create_ship(name: 's1')
130
+ pirate.update({ship_attributes: { name: 's2', id: ship.id } })
131
+ assert_equal 's1', ship.reload.name
132
+ end
133
+
134
+ def test_reuse_already_built_new_record
135
+ pirate = Pirate.new
136
+ ship_built_first = pirate.build_ship
137
+ pirate.ship_attributes = { name: 'Ship 1' }
138
+ assert_equal ship_built_first.object_id, pirate.ship.object_id
139
+ end
140
+
141
+ def test_do_not_allow_assigning_foreign_key_when_reusing_existing_new_record
142
+ pirate = Pirate.create!(catchphrase: "Don' botharrr talkin' like one, savvy?")
143
+ pirate.build_ship
144
+ pirate.ship_attributes = { name: 'Ship 1', pirate_id: pirate.id + 1 }
145
+ assert_equal pirate.id, pirate.ship.pirate_id
146
+ end
147
+
148
+ def test_reject_if_with_a_proc_which_returns_true_always_for_has_many
149
+ Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }
150
+ man = Man.create(name: "John")
151
+ interest = man.interests.create(topic: 'photography')
152
+ man.update({interests_attributes: { topic: 'gardening', id: interest.id } })
153
+ assert_equal 'photography', interest.reload.topic
154
+ end
155
+
156
+ def test_destroy_works_independent_of_reject_if
157
+ Man.accepts_nested_attributes_for :interests, :reject_if => proc {|attributes| true }, :allow_destroy => true
158
+ man = Man.create(name: "Jon")
159
+ interest = man.interests.create(topic: 'the ladies')
160
+ man.update({interests_attributes: { _destroy: "1", id: interest.id } })
161
+ assert man.reload.interests.empty?
162
+ end
163
+
164
+ def test_has_many_association_updating_a_single_record
165
+ Man.accepts_nested_attributes_for(:interests)
166
+ man = Man.create(name: 'John')
167
+ interest = man.interests.create(topic: 'photography')
168
+ man.update({interests_attributes: {topic: 'gardening', id: interest.id}})
169
+ assert_equal 'gardening', interest.reload.topic
170
+ end
171
+
172
+ def test_reject_if_with_blank_nested_attributes_id
173
+ # When using a select list to choose an existing 'ship' id, with include_blank: true
174
+ Pirate.accepts_nested_attributes_for :ship, :reject_if => proc {|attributes| attributes[:id].blank? }
175
+
176
+ pirate = Pirate.new(:catchphrase => "Stop wastin' me time")
177
+ pirate.ship_attributes = { :id => "" }
178
+ assert_nothing_raised(ActiveRecord::RecordNotFound) { pirate.save! }
179
+ end
180
+
181
+ def test_first_and_array_index_zero_methods_return_the_same_value_when_nested_attributes_are_set_to_update_existing_record
182
+ Man.accepts_nested_attributes_for(:interests)
183
+ man = Man.create(:name => "John")
184
+ interest = man.interests.create :topic => 'gardening'
185
+ man = Man.find man.id
186
+ man.interests_attributes = [{:id => interest.id, :topic => 'gardening'}]
187
+ assert_equal man.interests.first.topic, man.interests[0].topic
188
+ end
189
+
190
+ def test_allows_class_to_override_setter_and_call_super
191
+ mean_pirate_class = Class.new(Pirate) do
192
+ accepts_nested_attributes_for :parrot
193
+ def parrot_attributes=(attrs)
194
+ super(attrs.merge(:color => "blue"))
195
+ end
196
+ end
197
+ mean_pirate = mean_pirate_class.new
198
+ mean_pirate.parrot_attributes = { :name => "James" }
199
+ assert_equal "James", mean_pirate.parrot.name
200
+ assert_equal "blue", mean_pirate.parrot.color
201
+ end
202
+
203
+ def test_accepts_nested_attributes_for_can_be_overridden_in_subclasses
204
+ Pirate.accepts_nested_attributes_for(:parrot)
205
+
206
+ mean_pirate_class = Class.new(Pirate) do
207
+ accepts_nested_attributes_for :parrot
208
+ end
209
+ mean_pirate = mean_pirate_class.new
210
+ mean_pirate.parrot_attributes = { :name => "James" }
211
+ assert_equal "James", mean_pirate.parrot.name
212
+ end
213
+ end
214
+
215
+ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
216
+ include AssertRaiseWithMessage
217
+
218
+ def setup
219
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
220
+ @ship = @pirate.create_ship(:name => 'Nights Dirty Lightning')
221
+ end
222
+
223
+ def test_should_raise_argument_error_if_trying_to_build_polymorphic_belongs_to
224
+ assert_raise_with_message ArgumentError, "Cannot build association `looter'. Are you trying to build a polymorphic one-to-one association?" do
225
+ Treasure.new(:name => 'pearl', :looter_attributes => {:catchphrase => "Arrr"})
226
+ end
227
+ end
228
+
229
+ def test_should_define_an_attribute_writer_method_for_the_association
230
+ assert_respond_to @pirate, :ship_attributes=
231
+ end
232
+
233
+ def test_should_build_a_new_record_if_there_is_no_id
234
+ @ship.destroy
235
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger' }
236
+
237
+ assert !@pirate.ship.persisted?
238
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
239
+ end
240
+
241
+ def test_should_not_build_a_new_record_if_there_is_no_id_and_destroy_is_truthy
242
+ @ship.destroy
243
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger', :_destroy => '1' }
244
+
245
+ assert_nil @pirate.ship
246
+ end
247
+
248
+ def test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false
249
+ @ship.destroy
250
+ @pirate.reload.ship_attributes = {}
251
+
252
+ assert_nil @pirate.ship
253
+ end
254
+
255
+ def test_should_replace_an_existing_record_if_there_is_no_id
256
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger' }
257
+
258
+ assert !@pirate.ship.persisted?
259
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
260
+ assert_equal 'Nights Dirty Lightning', @ship.name
261
+ end
262
+
263
+ def test_should_not_replace_an_existing_record_if_there_is_no_id_and_destroy_is_truthy
264
+ @pirate.reload.ship_attributes = { :name => 'Davy Jones Gold Dagger', :_destroy => '1' }
265
+
266
+ assert_equal @ship, @pirate.ship
267
+ assert_equal 'Nights Dirty Lightning', @pirate.ship.name
268
+ end
269
+
270
+ def test_should_modify_an_existing_record_if_there_is_a_matching_id
271
+ @pirate.reload.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
272
+
273
+ assert_equal @ship, @pirate.ship
274
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
275
+ end
276
+
277
+ def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
278
+ assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find Ship with ID=1234567890 for Pirate with ID=#{@pirate.id}" do
279
+ @pirate.ship_attributes = { :id => 1234567890 }
280
+ end
281
+ end
282
+
283
+ def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
284
+ @pirate.reload.ship_attributes = { 'id' => @ship.id, 'name' => 'Davy Jones Gold Dagger' }
285
+
286
+ assert_equal @ship, @pirate.ship
287
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
288
+ end
289
+
290
+ def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
291
+ @ship.stubs(:id).returns('ABC1X')
292
+ @pirate.ship_attributes = { :id => @ship.id, :name => 'Davy Jones Gold Dagger' }
293
+
294
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
295
+ end
296
+
297
+ def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
298
+ @pirate.ship.destroy
299
+
300
+ [1, '1', true, 'true'].each do |truth|
301
+ ship = @pirate.reload.create_ship(name: 'Mister Pablo')
302
+ @pirate.update(ship_attributes: { id: ship.id, _destroy: truth })
303
+
304
+ assert_nil @pirate.reload.ship
305
+ assert_raise(ActiveRecord::RecordNotFound) { Ship.find(ship.id) }
306
+ end
307
+ end
308
+
309
+ def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
310
+ [nil, '0', 0, 'false', false].each do |not_truth|
311
+ @pirate.update(ship_attributes: { id: @pirate.ship.id, _destroy: not_truth })
312
+
313
+ assert_equal @ship, @pirate.reload.ship
314
+ end
315
+ end
316
+
317
+ def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
318
+ Pirate.accepts_nested_attributes_for :ship, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
319
+
320
+ @pirate.update(ship_attributes: { id: @pirate.ship.id, _destroy: '1' })
321
+
322
+ assert_equal @ship, @pirate.reload.ship
323
+
324
+ Pirate.accepts_nested_attributes_for :ship, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
325
+ end
326
+
327
+ def test_should_also_work_with_a_HashWithIndifferentAccess
328
+ @pirate.ship_attributes = ActiveSupport::HashWithIndifferentAccess.new(:id => @ship.id, :name => 'Davy Jones Gold Dagger')
329
+
330
+ assert @pirate.ship.persisted?
331
+ assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
332
+ end
333
+
334
+ def test_should_work_with_update_as_well
335
+ @pirate.update({ catchphrase: 'Arr', ship_attributes: { id: @ship.id, name: 'Mister Pablo' } })
336
+ @pirate.reload
337
+
338
+ assert_equal 'Arr', @pirate.catchphrase
339
+ assert_equal 'Mister Pablo', @pirate.ship.name
340
+ end
341
+
342
+ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
343
+ @pirate.attributes = { :ship_attributes => { :id => @ship.id, :_destroy => '1' } }
344
+
345
+ assert !@pirate.ship.destroyed?
346
+ assert @pirate.ship.marked_for_destruction?
347
+
348
+ @pirate.save
349
+
350
+ assert @pirate.ship.destroyed?
351
+ assert_nil @pirate.reload.ship
352
+ end
353
+
354
+ def test_should_automatically_enable_autosave_on_the_association
355
+ assert Pirate.reflect_on_association(:ship).options[:autosave]
356
+ end
357
+
358
+ def test_should_accept_update_only_option
359
+ @pirate.update(update_only_ship_attributes: { id: @pirate.ship.id, name: 'Mayflower' })
360
+ end
361
+
362
+ def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
363
+ @ship.delete
364
+
365
+ @pirate.reload.update(update_only_ship_attributes: { name: 'Mayflower' })
366
+
367
+ assert_not_nil @pirate.ship
368
+ end
369
+
370
+ def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
371
+ @ship.delete
372
+ @ship = @pirate.create_update_only_ship(name: 'Nights Dirty Lightning')
373
+
374
+ @pirate.update(update_only_ship_attributes: { name: 'Mayflower' })
375
+
376
+ assert_equal 'Mayflower', @ship.reload.name
377
+ assert_equal @ship, @pirate.reload.ship
378
+ end
379
+
380
+ def test_should_update_existing_when_update_only_is_true_and_id_is_given
381
+ @ship.delete
382
+ @ship = @pirate.create_update_only_ship(name: 'Nights Dirty Lightning')
383
+
384
+ @pirate.update(update_only_ship_attributes: { name: 'Mayflower', id: @ship.id })
385
+
386
+ assert_equal 'Mayflower', @ship.reload.name
387
+ assert_equal @ship, @pirate.reload.ship
388
+ end
389
+
390
+ def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
391
+ Pirate.accepts_nested_attributes_for :update_only_ship, :update_only => true, :allow_destroy => true
392
+ @ship.delete
393
+ @ship = @pirate.create_update_only_ship(name: 'Nights Dirty Lightning')
394
+
395
+ @pirate.update(update_only_ship_attributes: { name: 'Mayflower', id: @ship.id, _destroy: true })
396
+
397
+ assert_nil @pirate.reload.ship
398
+ assert_raise(ActiveRecord::RecordNotFound) { Ship.find(@ship.id) }
399
+
400
+ Pirate.accepts_nested_attributes_for :update_only_ship, :update_only => true, :allow_destroy => false
401
+ end
402
+
403
+ end
404
+
405
+ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
406
+ include AssertRaiseWithMessage
407
+
408
+ def setup
409
+ @ship = Ship.new(:name => 'Nights Dirty Lightning')
410
+ @pirate = @ship.build_pirate(:catchphrase => 'Aye')
411
+ @ship.save!
412
+ end
413
+
414
+ def test_should_define_an_attribute_writer_method_for_the_association
415
+ assert_respond_to @ship, :pirate_attributes=
416
+ end
417
+
418
+ def test_should_build_a_new_record_if_there_is_no_id
419
+ @pirate.destroy
420
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr' }
421
+
422
+ assert !@ship.pirate.persisted?
423
+ assert_equal 'Arr', @ship.pirate.catchphrase
424
+ end
425
+
426
+ def test_should_not_build_a_new_record_if_there_is_no_id_and_destroy_is_truthy
427
+ @pirate.destroy
428
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr', :_destroy => '1' }
429
+
430
+ assert_nil @ship.pirate
431
+ end
432
+
433
+ def test_should_not_build_a_new_record_if_a_reject_if_proc_returns_false
434
+ @pirate.destroy
435
+ @ship.reload.pirate_attributes = {}
436
+
437
+ assert_nil @ship.pirate
438
+ end
439
+
440
+ def test_should_replace_an_existing_record_if_there_is_no_id
441
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr' }
442
+
443
+ assert !@ship.pirate.persisted?
444
+ assert_equal 'Arr', @ship.pirate.catchphrase
445
+ assert_equal 'Aye', @pirate.catchphrase
446
+ end
447
+
448
+ def test_should_not_replace_an_existing_record_if_there_is_no_id_and_destroy_is_truthy
449
+ @ship.reload.pirate_attributes = { :catchphrase => 'Arr', :_destroy => '1' }
450
+
451
+ assert_equal @pirate, @ship.pirate
452
+ assert_equal 'Aye', @ship.pirate.catchphrase
453
+ end
454
+
455
+ def test_should_modify_an_existing_record_if_there_is_a_matching_id
456
+ @ship.reload.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
457
+
458
+ assert_equal @pirate, @ship.pirate
459
+ assert_equal 'Arr', @ship.pirate.catchphrase
460
+ end
461
+
462
+ def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
463
+ assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find Pirate with ID=1234567890 for Ship with ID=#{@ship.id}" do
464
+ @ship.pirate_attributes = { :id => 1234567890 }
465
+ end
466
+ end
467
+
468
+ def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
469
+ @ship.reload.pirate_attributes = { 'id' => @pirate.id, 'catchphrase' => 'Arr' }
470
+
471
+ assert_equal @pirate, @ship.pirate
472
+ assert_equal 'Arr', @ship.pirate.catchphrase
473
+ end
474
+
475
+ def test_should_modify_an_existing_record_if_there_is_a_matching_composite_id
476
+ @pirate.stubs(:id).returns('ABC1X')
477
+ @ship.pirate_attributes = { :id => @pirate.id, :catchphrase => 'Arr' }
478
+
479
+ assert_equal 'Arr', @ship.pirate.catchphrase
480
+ end
481
+
482
+ def test_should_destroy_an_existing_record_if_there_is_a_matching_id_and_destroy_is_truthy
483
+ @ship.pirate.destroy
484
+ [1, '1', true, 'true'].each do |truth|
485
+ pirate = @ship.reload.create_pirate(catchphrase: 'Arr')
486
+ @ship.update(pirate_attributes: { id: pirate.id, _destroy: truth })
487
+ assert_raise(ActiveRecord::RecordNotFound) { pirate.reload }
488
+ end
489
+ end
490
+
491
+ def test_should_unset_association_when_an_existing_record_is_destroyed
492
+ original_pirate_id = @ship.pirate.id
493
+ @ship.update! pirate_attributes: { id: @ship.pirate.id, _destroy: true }
494
+
495
+ assert_empty Pirate.where(id: original_pirate_id)
496
+ assert_nil @ship.pirate_id
497
+ assert_nil @ship.pirate
498
+
499
+ @ship.reload
500
+ assert_empty Pirate.where(id: original_pirate_id)
501
+ assert_nil @ship.pirate_id
502
+ assert_nil @ship.pirate
503
+ end
504
+
505
+ def test_should_not_destroy_an_existing_record_if_destroy_is_not_truthy
506
+ [nil, '0', 0, 'false', false].each do |not_truth|
507
+ @ship.update(pirate_attributes: { id: @ship.pirate.id, _destroy: not_truth })
508
+ assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload }
509
+ end
510
+ end
511
+
512
+ def test_should_not_destroy_an_existing_record_if_allow_destroy_is_false
513
+ Ship.accepts_nested_attributes_for :pirate, :allow_destroy => false, :reject_if => proc { |attributes| attributes.empty? }
514
+
515
+ @ship.update(pirate_attributes: { id: @ship.pirate.id, _destroy: '1' })
516
+ assert_nothing_raised(ActiveRecord::RecordNotFound) { @ship.pirate.reload }
517
+ ensure
518
+ Ship.accepts_nested_attributes_for :pirate, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
519
+ end
520
+
521
+ def test_should_work_with_update_as_well
522
+ @ship.update({ name: 'Mister Pablo', pirate_attributes: { catchphrase: 'Arr' } })
523
+ @ship.reload
524
+
525
+ assert_equal 'Mister Pablo', @ship.name
526
+ assert_equal 'Arr', @ship.pirate.catchphrase
527
+ end
528
+
529
+ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
530
+ pirate = @ship.pirate
531
+
532
+ @ship.attributes = { :pirate_attributes => { :id => pirate.id, '_destroy' => true } }
533
+ assert_nothing_raised(ActiveRecord::RecordNotFound) { Pirate.find(pirate.id) }
534
+ @ship.save
535
+ assert_raise(ActiveRecord::RecordNotFound) { Pirate.find(pirate.id) }
536
+ end
537
+
538
+ def test_should_automatically_enable_autosave_on_the_association
539
+ assert Ship.reflect_on_association(:pirate).options[:autosave]
540
+ end
541
+
542
+ def test_should_create_new_model_when_nothing_is_there_and_update_only_is_true
543
+ @pirate.delete
544
+ @ship.reload.attributes = { :update_only_pirate_attributes => { :catchphrase => 'Arr' } }
545
+
546
+ assert !@ship.update_only_pirate.persisted?
547
+ end
548
+
549
+ def test_should_update_existing_when_update_only_is_true_and_no_id_is_given
550
+ @pirate.delete
551
+ @pirate = @ship.create_update_only_pirate(catchphrase: 'Aye')
552
+
553
+ @ship.update(update_only_pirate_attributes: { catchphrase: 'Arr' })
554
+ assert_equal 'Arr', @pirate.reload.catchphrase
555
+ assert_equal @pirate, @ship.reload.update_only_pirate
556
+ end
557
+
558
+ def test_should_update_existing_when_update_only_is_true_and_id_is_given
559
+ @pirate.delete
560
+ @pirate = @ship.create_update_only_pirate(catchphrase: 'Aye')
561
+
562
+ @ship.update(update_only_pirate_attributes: { catchphrase: 'Arr', id: @pirate.id })
563
+
564
+ assert_equal 'Arr', @pirate.reload.catchphrase
565
+ assert_equal @pirate, @ship.reload.update_only_pirate
566
+ end
567
+
568
+ def test_should_destroy_existing_when_update_only_is_true_and_id_is_given_and_is_marked_for_destruction
569
+ Ship.accepts_nested_attributes_for :update_only_pirate, :update_only => true, :allow_destroy => true
570
+ @pirate.delete
571
+ @pirate = @ship.create_update_only_pirate(catchphrase: 'Aye')
572
+
573
+ @ship.update(update_only_pirate_attributes: { catchphrase: 'Arr', id: @pirate.id, _destroy: true })
574
+
575
+ assert_raise(ActiveRecord::RecordNotFound) { @pirate.reload }
576
+
577
+ Ship.accepts_nested_attributes_for :update_only_pirate, :update_only => true, :allow_destroy => false
578
+ end
579
+ end
580
+
581
+ module NestedAttributesOnACollectionAssociationTests
582
+ include AssertRaiseWithMessage
583
+
584
+ def test_should_define_an_attribute_writer_method_for_the_association
585
+ assert_respond_to @pirate, association_setter
586
+ end
587
+
588
+ def test_should_save_only_one_association_on_create
589
+ pirate = Pirate.create!({
590
+ :catchphrase => 'Arr',
591
+ association_getter => { 'foo' => { :name => 'Grace OMalley' } }
592
+ })
593
+
594
+ assert_equal 1, pirate.reload.send(@association_name).count
595
+ end
596
+
597
+ def test_should_take_a_hash_with_string_keys_and_assign_the_attributes_to_the_associated_models
598
+ @alternate_params[association_getter].stringify_keys!
599
+ @pirate.update @alternate_params
600
+ assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
601
+ end
602
+
603
+ def test_should_take_an_array_and_assign_the_attributes_to_the_associated_models
604
+ @pirate.send(association_setter, @alternate_params[association_getter].values)
605
+ @pirate.save
606
+ assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
607
+ end
608
+
609
+ def test_should_also_work_with_a_HashWithIndifferentAccess
610
+ @pirate.send(association_setter, ActiveSupport::HashWithIndifferentAccess.new('foo' => ActiveSupport::HashWithIndifferentAccess.new(:id => @child_1.id, :name => 'Grace OMalley')))
611
+ @pirate.save
612
+ assert_equal 'Grace OMalley', @child_1.reload.name
613
+ end
614
+
615
+ def test_should_take_a_hash_and_assign_the_attributes_to_the_associated_models
616
+ @pirate.attributes = @alternate_params
617
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
618
+ assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
619
+ end
620
+
621
+ def test_should_not_load_association_when_updating_existing_records
622
+ @pirate.reload
623
+ @pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
624
+ assert ! @pirate.send(@association_name).loaded?
625
+
626
+ @pirate.save
627
+ assert ! @pirate.send(@association_name).loaded?
628
+ assert_equal 'Grace OMalley', @child_1.reload.name
629
+ end
630
+
631
+ def test_should_not_overwrite_unsaved_updates_when_loading_association
632
+ @pirate.reload
633
+ @pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
634
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.name
635
+ end
636
+
637
+ def test_should_preserve_order_when_not_overwriting_unsaved_updates
638
+ @pirate.reload
639
+ @pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
640
+ assert_equal @child_1.id, @pirate.send(@association_name).send(:load_target).first.id
641
+ end
642
+
643
+ def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
644
+ @pirate.reload
645
+ record = @pirate.class.reflect_on_association(@association_name).klass.new(name: 'Grace OMalley')
646
+ @pirate.send(@association_name) << record
647
+ record.save!
648
+ @pirate.send(@association_name).last.update!(name: 'Polly')
649
+ assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name
650
+ end
651
+
652
+ def test_should_not_remove_scheduled_destroys_when_loading_association
653
+ @pirate.reload
654
+ @pirate.send(association_setter, [{ :id => @child_1.id, :_destroy => '1' }])
655
+ assert @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.marked_for_destruction?
656
+ end
657
+
658
+ def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
659
+ @child_1.stubs(:id).returns('ABC1X')
660
+ @child_2.stubs(:id).returns('ABC2X')
661
+
662
+ @pirate.attributes = {
663
+ association_getter => [
664
+ { :id => @child_1.id, :name => 'Grace OMalley' },
665
+ { :id => @child_2.id, :name => 'Privateers Greed' }
666
+ ]
667
+ }
668
+
669
+ assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
670
+ end
671
+
672
+ def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
673
+ assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find #{@child_1.class.name} with ID=1234567890 for Pirate with ID=#{@pirate.id}" do
674
+ @pirate.attributes = { association_getter => [{ :id => 1234567890 }] }
675
+ end
676
+ end
677
+
678
+ def test_should_automatically_build_new_associated_models_for_each_entry_in_a_hash_where_the_id_is_missing
679
+ @pirate.send(@association_name).destroy_all
680
+ @pirate.reload.attributes = {
681
+ association_getter => { 'foo' => { :name => 'Grace OMalley' }, 'bar' => { :name => 'Privateers Greed' }}
682
+ }
683
+
684
+ assert !@pirate.send(@association_name).first.persisted?
685
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
686
+
687
+ assert !@pirate.send(@association_name).last.persisted?
688
+ assert_equal 'Privateers Greed', @pirate.send(@association_name).last.name
689
+ end
690
+
691
+ def test_should_not_assign_destroy_key_to_a_record
692
+ assert_nothing_raised ActiveRecord::UnknownAttributeError do
693
+ @pirate.send(association_setter, { 'foo' => { '_destroy' => '0' }})
694
+ end
695
+ end
696
+
697
+ def test_should_ignore_new_associated_records_with_truthy_destroy_attribute
698
+ @pirate.send(@association_name).destroy_all
699
+ @pirate.reload.attributes = {
700
+ association_getter => {
701
+ 'foo' => { :name => 'Grace OMalley' },
702
+ 'bar' => { :name => 'Privateers Greed', '_destroy' => '1' }
703
+ }
704
+ }
705
+
706
+ assert_equal 1, @pirate.send(@association_name).length
707
+ assert_equal 'Grace OMalley', @pirate.send(@association_name).first.name
708
+ end
709
+
710
+ def test_should_ignore_new_associated_records_if_a_reject_if_proc_returns_false
711
+ @alternate_params[association_getter]['baz'] = {}
712
+ assert_no_difference("@pirate.send(@association_name).count") do
713
+ @pirate.attributes = @alternate_params
714
+ end
715
+ end
716
+
717
+ def test_should_sort_the_hash_by_the_keys_before_building_new_associated_models
718
+ attributes = {}
719
+ attributes['123726353'] = { :name => 'Grace OMalley' }
720
+ attributes['2'] = { :name => 'Privateers Greed' } # 2 is lower then 123726353
721
+ @pirate.send(association_setter, attributes)
722
+
723
+ assert_equal ['Posideons Killer', 'Killer bandita Dionne', 'Privateers Greed', 'Grace OMalley'].to_set, @pirate.send(@association_name).map(&:name).to_set
724
+ end
725
+
726
+ def test_should_raise_an_argument_error_if_something_else_than_a_hash_is_passed
727
+ assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, {}) }
728
+ assert_nothing_raised(ArgumentError) { @pirate.send(association_setter, Hash.new) }
729
+
730
+ assert_raise_with_message ArgumentError, 'Hash or Array expected, got String ("foo")' do
731
+ @pirate.send(association_setter, "foo")
732
+ end
733
+ end
734
+
735
+ def test_should_work_with_update_as_well
736
+ @pirate.update(catchphrase: 'Arr',
737
+ association_getter => { 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' }})
738
+
739
+ assert_equal 'Grace OMalley', @child_1.reload.name
740
+ end
741
+
742
+ def test_should_update_existing_records_and_add_new_ones_that_have_no_id
743
+ @alternate_params[association_getter]['baz'] = { name: 'Buccaneers Servant' }
744
+ assert_difference('@pirate.send(@association_name).count', +1) do
745
+ @pirate.update @alternate_params
746
+ end
747
+ assert_equal ['Grace OMalley', 'Privateers Greed', 'Buccaneers Servant'].to_set, @pirate.reload.send(@association_name).map(&:name).to_set
748
+ end
749
+
750
+ def test_should_be_possible_to_destroy_a_record
751
+ ['1', 1, 'true', true].each do |true_variable|
752
+ record = @pirate.reload.send(@association_name).create!(:name => 'Grace OMalley')
753
+ @pirate.send(association_setter,
754
+ @alternate_params[association_getter].merge('baz' => { :id => record.id, '_destroy' => true_variable })
755
+ )
756
+
757
+ assert_difference('@pirate.send(@association_name).count', -1) do
758
+ @pirate.save
759
+ end
760
+ end
761
+ end
762
+
763
+ def test_should_not_destroy_the_associated_model_with_a_non_truthy_argument
764
+ [nil, '', '0', 0, 'false', false].each do |false_variable|
765
+ @alternate_params[association_getter]['foo']['_destroy'] = false_variable
766
+ assert_no_difference('@pirate.send(@association_name).count') do
767
+ @pirate.update(@alternate_params)
768
+ end
769
+ end
770
+ end
771
+
772
+ def test_should_not_destroy_the_associated_model_until_the_parent_is_saved
773
+ assert_no_difference('@pirate.send(@association_name).count') do
774
+ @pirate.send(association_setter, @alternate_params[association_getter].merge('baz' => { :id => @child_1.id, '_destroy' => true }))
775
+ end
776
+ assert_difference('@pirate.send(@association_name).count', -1) { @pirate.save }
777
+ end
778
+
779
+ def test_should_automatically_enable_autosave_on_the_association
780
+ assert Pirate.reflect_on_association(@association_name).options[:autosave]
781
+ end
782
+
783
+ def test_validate_presence_of_parent_works_with_inverse_of
784
+ Man.accepts_nested_attributes_for(:interests)
785
+ assert_equal :man, Man.reflect_on_association(:interests).options[:inverse_of]
786
+ assert_equal :interests, Interest.reflect_on_association(:man).options[:inverse_of]
787
+
788
+ repair_validations(Interest) do
789
+ Interest.validates_presence_of(:man)
790
+ assert_difference 'Man.count' do
791
+ assert_difference 'Interest.count', 2 do
792
+ man = Man.create!(:name => 'John',
793
+ :interests_attributes => [{:topic=>'Cars'}, {:topic=>'Sports'}])
794
+ assert_equal 2, man.interests.count
795
+ end
796
+ end
797
+ end
798
+ end
799
+
800
+ def test_can_use_symbols_as_object_identifier
801
+ @pirate.attributes = { :parrots_attributes => { :foo => { :name => 'Lovely Day' }, :bar => { :name => 'Blown Away' } } }
802
+ assert_nothing_raised(NoMethodError) { @pirate.save! }
803
+ end
804
+
805
+ def test_numeric_column_changes_from_zero_to_no_empty_string
806
+ Man.accepts_nested_attributes_for(:interests)
807
+
808
+ repair_validations(Interest) do
809
+ Interest.validates_numericality_of(:zine_id)
810
+ man = Man.create(name: 'John')
811
+ interest = man.interests.create(topic: 'bar', zine_id: 0)
812
+ assert interest.save
813
+ assert !man.update({interests_attributes: { id: interest.id, zine_id: 'foo' }})
814
+ end
815
+ end
816
+
817
+ private
818
+
819
+ def association_setter
820
+ @association_setter ||= "#{@association_name}_attributes=".to_sym
821
+ end
822
+
823
+ def association_getter
824
+ @association_getter ||= "#{@association_name}_attributes".to_sym
825
+ end
826
+ end
827
+
828
+ class TestNestedAttributesOnAHasManyAssociation < ActiveRecord::TestCase
829
+ def setup
830
+ @association_type = :has_many
831
+ @association_name = :birds
832
+
833
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
834
+ @pirate.birds.create!(:name => 'Posideons Killer')
835
+ @pirate.birds.create!(:name => 'Killer bandita Dionne')
836
+
837
+ @child_1, @child_2 = @pirate.birds
838
+
839
+ @alternate_params = {
840
+ :birds_attributes => {
841
+ 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' },
842
+ 'bar' => { :id => @child_2.id, :name => 'Privateers Greed' }
843
+ }
844
+ }
845
+ end
846
+
847
+ include NestedAttributesOnACollectionAssociationTests
848
+ end
849
+
850
+ class TestNestedAttributesOnAHasAndBelongsToManyAssociation < ActiveRecord::TestCase
851
+ def setup
852
+ @association_type = :has_and_belongs_to_many
853
+ @association_name = :parrots
854
+
855
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
856
+ @pirate.parrots.create!(:name => 'Posideons Killer')
857
+ @pirate.parrots.create!(:name => 'Killer bandita Dionne')
858
+
859
+ @child_1, @child_2 = @pirate.parrots
860
+
861
+ @alternate_params = {
862
+ :parrots_attributes => {
863
+ 'foo' => { :id => @child_1.id, :name => 'Grace OMalley' },
864
+ 'bar' => { :id => @child_2.id, :name => 'Privateers Greed' }
865
+ }
866
+ }
867
+ end
868
+
869
+ include NestedAttributesOnACollectionAssociationTests
870
+ end
871
+
872
+ module NestedAttributesLimitTests
873
+ def teardown
874
+ Pirate.accepts_nested_attributes_for :parrots, :allow_destroy => true, :reject_if => proc { |attributes| attributes.empty? }
875
+ end
876
+
877
+ def test_limit_with_less_records
878
+ @pirate.attributes = { :parrots_attributes => { 'foo' => { :name => 'Big Big Love' } } }
879
+ assert_difference('Parrot.count') { @pirate.save! }
880
+ end
881
+
882
+ def test_limit_with_number_exact_records
883
+ @pirate.attributes = { :parrots_attributes => { 'foo' => { :name => 'Lovely Day' }, 'bar' => { :name => 'Blown Away' } } }
884
+ assert_difference('Parrot.count', 2) { @pirate.save! }
885
+ end
886
+
887
+ def test_limit_with_exceeding_records
888
+ assert_raises(ActiveRecord::NestedAttributes::TooManyRecords) do
889
+ @pirate.attributes = { :parrots_attributes => { 'foo' => { :name => 'Lovely Day' },
890
+ 'bar' => { :name => 'Blown Away' },
891
+ 'car' => { :name => 'The Happening' }} }
892
+ end
893
+ end
894
+ end
895
+
896
+ class TestNestedAttributesLimitNumeric < ActiveRecord::TestCase
897
+ def setup
898
+ Pirate.accepts_nested_attributes_for :parrots, :limit => 2
899
+
900
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
901
+ end
902
+
903
+ include NestedAttributesLimitTests
904
+ end
905
+
906
+ class TestNestedAttributesLimitSymbol < ActiveRecord::TestCase
907
+ def setup
908
+ Pirate.accepts_nested_attributes_for :parrots, :limit => :parrots_limit
909
+
910
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?", :parrots_limit => 2)
911
+ end
912
+
913
+ include NestedAttributesLimitTests
914
+ end
915
+
916
+ class TestNestedAttributesLimitProc < ActiveRecord::TestCase
917
+ def setup
918
+ Pirate.accepts_nested_attributes_for :parrots, :limit => proc { 2 }
919
+
920
+ @pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
921
+ end
922
+
923
+ include NestedAttributesLimitTests
924
+ end
925
+
926
+ class TestNestedAttributesWithNonStandardPrimaryKeys < ActiveRecord::TestCase
927
+ fixtures :owners, :pets
928
+
929
+ def setup
930
+ Owner.accepts_nested_attributes_for :pets, :allow_destroy => true
931
+
932
+ @owner = owners(:ashley)
933
+ @pet1, @pet2 = pets(:chew), pets(:mochi)
934
+
935
+ @params = {
936
+ :pets_attributes => {
937
+ '0' => { :id => @pet1.id, :name => 'Foo' },
938
+ '1' => { :id => @pet2.id, :name => 'Bar' }
939
+ }
940
+ }
941
+ end
942
+
943
+ def test_should_update_existing_records_with_non_standard_primary_key
944
+ @owner.update(@params)
945
+ assert_equal ['Foo', 'Bar'], @owner.pets.map(&:name)
946
+ end
947
+
948
+ def test_attr_accessor_of_child_should_be_value_provided_during_update
949
+ @owner = owners(:ashley)
950
+ @pet1 = pets(:chew)
951
+ attributes = {:pets_attributes => { "1"=> { :id => @pet1.id,
952
+ :name => "Foo2",
953
+ :current_user => "John",
954
+ :_destroy=>true }}}
955
+ @owner.update(attributes)
956
+ assert_equal 'John', Pet.after_destroy_output
957
+ end
958
+
959
+ end
960
+
961
+ class TestHasOneAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveRecord::TestCase
962
+ self.use_transactional_fixtures = false unless supports_savepoints?
963
+
964
+ def setup
965
+ @pirate = Pirate.create!(:catchphrase => "My baby takes tha mornin' train!")
966
+ @ship = @pirate.create_ship(:name => "The good ship Dollypop")
967
+ @part = @ship.parts.create!(:name => "Mast")
968
+ @trinket = @part.trinkets.create!(:name => "Necklace")
969
+ end
970
+
971
+ test "when great-grandchild changed in memory, saving parent should save great-grandchild" do
972
+ @trinket.name = "changed"
973
+ @pirate.save
974
+ assert_equal "changed", @trinket.reload.name
975
+ end
976
+
977
+ test "when great-grandchild changed via attributes, saving parent should save great-grandchild" do
978
+ @pirate.attributes = {:ship_attributes => {:id => @ship.id, :parts_attributes => [{:id => @part.id, :trinkets_attributes => [{:id => @trinket.id, :name => "changed"}]}]}}
979
+ @pirate.save
980
+ assert_equal "changed", @trinket.reload.name
981
+ end
982
+
983
+ test "when great-grandchild marked_for_destruction via attributes, saving parent should destroy great-grandchild" do
984
+ @pirate.attributes = {:ship_attributes => {:id => @ship.id, :parts_attributes => [{:id => @part.id, :trinkets_attributes => [{:id => @trinket.id, :_destroy => true}]}]}}
985
+ assert_difference('@part.trinkets.count', -1) { @pirate.save }
986
+ end
987
+
988
+ test "when great-grandchild added via attributes, saving parent should create great-grandchild" do
989
+ @pirate.attributes = {:ship_attributes => {:id => @ship.id, :parts_attributes => [{:id => @part.id, :trinkets_attributes => [{:name => "created"}]}]}}
990
+ assert_difference('@part.trinkets.count', 1) { @pirate.save }
991
+ end
992
+
993
+ test "when extra records exist for associations, validate (which calls nested_records_changed_for_autosave?) should not load them up" do
994
+ @trinket.name = "changed"
995
+ Ship.create!(:pirate => @pirate, :name => "The Black Rock")
996
+ ShipPart.create!(:ship => @ship, :name => "Stern")
997
+ assert_no_queries { @pirate.valid? }
998
+ end
999
+ end
1000
+
1001
+ class TestHasManyAutosaveAssociationWhichItselfHasAutosaveAssociations < ActiveRecord::TestCase
1002
+ self.use_transactional_fixtures = false unless supports_savepoints?
1003
+
1004
+ def setup
1005
+ @ship = Ship.create!(:name => "The good ship Dollypop")
1006
+ @part = @ship.parts.create!(:name => "Mast")
1007
+ @trinket = @part.trinkets.create!(:name => "Necklace")
1008
+ end
1009
+
1010
+ test "if association is not loaded and association record is saved and then in memory record attributes should be saved" do
1011
+ @ship.parts_attributes=[{:id => @part.id,:name =>'Deck'}]
1012
+ assert_equal 1, @ship.association(:parts).target.size
1013
+ assert_equal 'Deck', @ship.parts[0].name
1014
+ end
1015
+
1016
+ test "if association is not loaded and child doesn't change and I am saving a grandchild then in memory record should be used" do
1017
+ @ship.parts_attributes=[{:id => @part.id,:trinkets_attributes =>[{:id => @trinket.id, :name => 'Ruby'}]}]
1018
+ assert_equal 1, @ship.association(:parts).target.size
1019
+ assert_equal 'Mast', @ship.parts[0].name
1020
+ assert_no_difference("@ship.parts[0].association(:trinkets).target.size") do
1021
+ @ship.parts[0].association(:trinkets).target.size
1022
+ end
1023
+ assert_equal 'Ruby', @ship.parts[0].trinkets[0].name
1024
+ @ship.save
1025
+ assert_equal 'Ruby', @ship.parts[0].trinkets[0].name
1026
+ end
1027
+
1028
+ test "when grandchild changed in memory, saving parent should save grandchild" do
1029
+ @trinket.name = "changed"
1030
+ @ship.save
1031
+ assert_equal "changed", @trinket.reload.name
1032
+ end
1033
+
1034
+ test "when grandchild changed via attributes, saving parent should save grandchild" do
1035
+ @ship.attributes = {:parts_attributes => [{:id => @part.id, :trinkets_attributes => [{:id => @trinket.id, :name => "changed"}]}]}
1036
+ @ship.save
1037
+ assert_equal "changed", @trinket.reload.name
1038
+ end
1039
+
1040
+ test "when grandchild marked_for_destruction via attributes, saving parent should destroy grandchild" do
1041
+ @ship.attributes = {:parts_attributes => [{:id => @part.id, :trinkets_attributes => [{:id => @trinket.id, :_destroy => true}]}]}
1042
+ assert_difference('@part.trinkets.count', -1) { @ship.save }
1043
+ end
1044
+
1045
+ test "when grandchild added via attributes, saving parent should create grandchild" do
1046
+ @ship.attributes = {:parts_attributes => [{:id => @part.id, :trinkets_attributes => [{:name => "created"}]}]}
1047
+ assert_difference('@part.trinkets.count', 1) { @ship.save }
1048
+ end
1049
+
1050
+ test "when extra records exist for associations, validate (which calls nested_records_changed_for_autosave?) should not load them up" do
1051
+ @trinket.name = "changed"
1052
+ Ship.create!(:name => "The Black Rock")
1053
+ ShipPart.create!(:ship => @ship, :name => "Stern")
1054
+ assert_no_queries { @ship.valid? }
1055
+ end
1056
+ end