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,32 +0,0 @@
1
- require "cases/helper"
2
-
3
- class TestRecord < ActiveRecord::Base
4
- end
5
-
6
- class TestUnconnectedAdapter < ActiveRecord::TestCase
7
- self.use_transactional_fixtures = false
8
-
9
- def setup
10
- @underlying = ActiveRecord::Base.connection
11
- @specification = ActiveRecord::Base.remove_connection
12
- end
13
-
14
- def teardown
15
- @underlying = nil
16
- ActiveRecord::Base.establish_connection(@specification)
17
- end
18
-
19
- def test_connection_no_longer_established
20
- assert_raise(ActiveRecord::ConnectionNotEstablished) do
21
- TestRecord.find(1)
22
- end
23
-
24
- assert_raise(ActiveRecord::ConnectionNotEstablished) do
25
- TestRecord.new.save
26
- end
27
- end
28
-
29
- def test_underlying_adapter_no_longer_active
30
- assert !@underlying.active?, "Removed adapter should no longer be active"
31
- end
32
- end
@@ -1,925 +0,0 @@
1
- require "cases/helper"
2
- require 'models/topic'
3
- require 'models/reply'
4
- require 'models/person'
5
-
6
- module ActiveRecordValidationsI18nTestHelper
7
- def store_translations(*args)
8
- data = args.extract_options!
9
- locale = args.shift || 'en'
10
- I18n.backend.send(:init_translations)
11
- I18n.backend.store_translations(locale, :activerecord => data)
12
- end
13
-
14
- def delete_translation(key)
15
- I18n.backend.instance_eval do
16
- keys = I18n.send(:normalize_translation_keys, 'en', key, nil)
17
- keys.inject(translations) { |result, k| keys.last == k ? result.delete(k.to_sym) : result[k.to_sym] }
18
- end
19
- end
20
-
21
- def reset_callbacks(*models)
22
- models.each do |model|
23
- model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
24
- model.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
25
- model.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
26
- end
27
- end
28
- end
29
-
30
-
31
- # ACTIVERECORD VALIDATIONS
32
- #
33
- # For each validation:
34
- #
35
- # * test expect that it adds an error with the appropriate arguments
36
- # * test that it looks up the correct default message
37
-
38
- class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase
39
- include ActiveRecordValidationsI18nTestHelper
40
-
41
- def setup
42
- reset_callbacks(Topic)
43
- @topic = Topic.new
44
- @reply = Reply.new
45
- @old_load_path, @old_backend = I18n.load_path, I18n.backend
46
- I18n.load_path.clear
47
- I18n.backend = I18n::Backend::Simple.new
48
- I18n.backend.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}})
49
- end
50
-
51
- def teardown
52
- reset_callbacks(Topic)
53
- I18n.load_path.replace(@old_load_path)
54
- I18n.backend = @old_backend
55
- end
56
-
57
- def expect_error_added(model, attribute, type, options)
58
- model.errors.expects(:add).with(attribute, type, options)
59
- yield
60
- model.valid?
61
- end
62
-
63
- def assert_message_translations(model, attribute, type, &block)
64
- assert_default_message_translation(model, attribute, type, &block)
65
- reset_callbacks(model.class)
66
- model.errors.clear
67
- assert_custom_message_translation(model, attribute, type, &block)
68
- end
69
-
70
- def assert_custom_message_translation(model, attribute, type)
71
- store_translations(:errors => { :models => { model.class.name.underscore => { :attributes => { attribute => { type => 'custom message' } } } } })
72
- yield
73
- model.valid?
74
- assert_equal 'custom message', model.errors.on(attribute)
75
- end
76
-
77
- def assert_default_message_translation(model, attribute, type)
78
- store_translations(:errors => { :messages => { type => 'default message' } })
79
- yield
80
- model.valid?
81
- assert_equal 'default message', model.errors.on(attribute)
82
- end
83
-
84
- def unique_topic
85
- @unique ||= Topic.create(:title => 'unique!')
86
- end
87
-
88
- def replied_topic
89
- @replied_topic ||= begin
90
- topic = Topic.create(:title => "topic")
91
- topic.replies << Reply.new
92
- topic
93
- end
94
- end
95
-
96
- # validates_confirmation_of
97
-
98
- test "#validates_confirmation_of given no custom message" do
99
- expect_error_added(@topic, :title, :confirmation, :default => nil) do
100
- Topic.validates_confirmation_of :title
101
- @topic.title = 'title'
102
- @topic.title_confirmation = 'foo'
103
- end
104
- end
105
-
106
- test "#validates_confirmation_of given a custom message" do
107
- expect_error_added(@topic, :title, :confirmation, :default => 'custom') do
108
- Topic.validates_confirmation_of :title, :message => 'custom'
109
- @topic.title_confirmation = 'foo'
110
- end
111
- end
112
-
113
- test "#validates_confirmation_of finds the correct message translations" do
114
- assert_message_translations(@topic, :title, :confirmation) do
115
- Topic.validates_confirmation_of :title
116
- @topic.title_confirmation = 'foo'
117
- end
118
- end
119
-
120
- # validates_acceptance_of
121
-
122
- test "#validates_acceptance_of given no custom message" do
123
- expect_error_added(@topic, :title, :accepted, :default => nil) do
124
- Topic.validates_acceptance_of :title, :allow_nil => false
125
- end
126
- end
127
-
128
- test "#validates_acceptance_of given a custom message" do
129
- expect_error_added(@topic, :title, :accepted, :default => 'custom') do
130
- Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
131
- end
132
- end
133
-
134
- test "#validates_acceptance_of finds the correct message translations" do
135
- assert_message_translations(@topic, :title, :accepted) do
136
- Topic.validates_acceptance_of :title, :allow_nil => false
137
- end
138
- end
139
-
140
- # validates_presence_of
141
-
142
- test "#validates_presence_of given no custom message" do
143
- expect_error_added(@topic, :title, :blank, :default => nil) do
144
- Topic.validates_presence_of :title
145
- end
146
- end
147
-
148
- test "#validates_presence_of given a custom message" do
149
- expect_error_added(@topic, :title, :blank, :default => 'custom') do
150
- Topic.validates_presence_of :title, :message => 'custom'
151
- end
152
- end
153
-
154
- test "#validates_presence_of finds the correct message translations" do
155
- assert_message_translations(@topic, :title, :blank) do
156
- Topic.validates_presence_of :title
157
- end
158
- end
159
-
160
- # validates_length_of :too_short
161
-
162
- test "#validates_length_of (:too_short) and no custom message" do
163
- expect_error_added(@topic, :title, :too_short, :default => nil, :count => 3) do
164
- Topic.validates_length_of :title, :within => 3..5
165
- end
166
- end
167
-
168
- test "#validates_length_of (:too_short) and a custom message" do
169
- expect_error_added(@topic, :title, :too_short, :default => 'custom', :count => 3) do
170
- Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
171
- end
172
- end
173
-
174
- test "#validates_length_of (:too_short) finds the correct message translations" do
175
- assert_message_translations(@topic, :title, :too_short) do
176
- Topic.validates_length_of :title, :within => 3..5
177
- end
178
- end
179
-
180
- # validates_length_of :too_long
181
-
182
- test "#validates_length_of (:too_long) and no custom message" do
183
- expect_error_added(@topic, :title, :too_long, :default => nil, :count => 5) do
184
- Topic.validates_length_of :title, :within => 3..5
185
- @topic.title = 'this title is too long'
186
- end
187
- end
188
-
189
- test "#validates_length_of (:too_long) and a custom message" do
190
- expect_error_added(@topic, :title, :too_long, :default => 'custom', :count => 5) do
191
- Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
192
- @topic.title = 'this title is too long'
193
- end
194
- end
195
-
196
- test "#validates_length_of (:too_long) finds the correct message translations" do
197
- assert_message_translations(@topic, :title, :too_long) do
198
- Topic.validates_length_of :title, :within => 3..5
199
- @topic.title = 'this title is too long'
200
- end
201
- end
202
-
203
- # validates_length_of :is
204
-
205
- test "#validates_length_of (:is) and no custom message" do
206
- expect_error_added(@topic, :title, :wrong_length, :default => nil, :count => 5) do
207
- Topic.validates_length_of :title, :is => 5
208
- @topic.title = 'this title has the wrong length'
209
- end
210
- end
211
-
212
- test "#validates_length_of (:is) and a custom message" do
213
- expect_error_added(@topic, :title, :wrong_length, :default => 'custom', :count => 5) do
214
- Topic.validates_length_of :title, :is => 5, :wrong_length => 'custom'
215
- @topic.title = 'this title has the wrong length'
216
- end
217
- end
218
-
219
- test "#validates_length_of (:is) finds the correct message translations" do
220
- assert_message_translations(@topic, :title, :wrong_length) do
221
- Topic.validates_length_of :title, :is => 5
222
- @topic.title = 'this title has the wrong length'
223
- end
224
- end
225
-
226
- # validates_uniqueness_of
227
-
228
- test "#validates_uniqueness_of and no custom message" do
229
- expect_error_added(@topic, :title, :taken, :default => nil, :value => 'unique!') do
230
- Topic.validates_uniqueness_of :title
231
- @topic.title = unique_topic.title
232
- end
233
- end
234
-
235
- test "#validates_uniqueness_of and a custom message" do
236
- expect_error_added(@topic, :title, :taken, :default => 'custom', :value => 'unique!') do
237
- Topic.validates_uniqueness_of :title, :message => 'custom'
238
- @topic.title = unique_topic.title
239
- end
240
- end
241
-
242
- test "#validates_uniqueness_of finds the correct message translations" do
243
- assert_message_translations(@topic, :title, :taken) do
244
- Topic.validates_uniqueness_of :title
245
- @topic.title = unique_topic.title
246
- end
247
- end
248
-
249
- # validates_format_of
250
-
251
- test "#validates_format_of and no custom message" do
252
- expect_error_added(@topic, :title, :invalid, :default => nil, :value => '72x') do
253
- Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
254
- @topic.title = '72x'
255
- end
256
- end
257
-
258
- test "#validates_format_of and a custom message" do
259
- expect_error_added(@topic, :title, :invalid, :default => 'custom', :value => '72x') do
260
- Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
261
- @topic.title = '72x'
262
- end
263
- end
264
-
265
- test "#validates_format_of finds the correct message translations" do
266
- assert_message_translations(@topic, :title, :invalid) do
267
- Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
268
- @topic.title = '72x'
269
- end
270
- end
271
-
272
- # validates_inclusion_of
273
-
274
- test "#validates_inclusion_of and no custom message" do
275
- list = %w(a b c)
276
- expect_error_added(@topic, :title, :inclusion, :default => nil, :value => 'z') do
277
- Topic.validates_inclusion_of :title, :in => list
278
- @topic.title = 'z'
279
- end
280
- end
281
-
282
- test "#validates_inclusion_of and a custom message" do
283
- list = %w(a b c)
284
- expect_error_added(@topic, :title, :inclusion, :default => 'custom', :value => 'z') do
285
- Topic.validates_inclusion_of :title, :in => list, :message => 'custom'
286
- @topic.title = 'z'
287
- end
288
- end
289
-
290
- test "#validates_inclusion_of finds the correct message translations" do
291
- list = %w(a b c)
292
- assert_message_translations(@topic, :title, :inclusion) do
293
- Topic.validates_inclusion_of :title, :in => list
294
- @topic.title = 'z'
295
- end
296
- end
297
-
298
- # validates_exclusion_of
299
-
300
- test "#validates_exclusion_of and no custom message" do
301
- list = %w(a b c)
302
- expect_error_added(@topic, :title, :exclusion, :default => nil, :value => 'a') do
303
- Topic.validates_exclusion_of :title, :in => list
304
- @topic.title = 'a'
305
- end
306
- end
307
-
308
- test "#validates_exclusion_of and a custom message" do
309
- list = %w(a b c)
310
- expect_error_added(@topic, :title, :exclusion, :default => 'custom', :value => 'a') do
311
- Topic.validates_exclusion_of :title, :in => list, :message => 'custom'
312
- @topic.title = 'a'
313
- end
314
- end
315
-
316
- test "#validates_exclusion_of finds the correct message translations" do
317
- list = %w(a b c)
318
- assert_message_translations(@topic, :title, :exclusion) do
319
- Topic.validates_exclusion_of :title, :in => list
320
- @topic.title = 'a'
321
- end
322
- end
323
-
324
- # validates_numericality_of :not_a_number, without :only_integer
325
-
326
- test "#validates_numericality_of (:not_a_number, w/o :only_integer) no custom message" do
327
- expect_error_added(@topic, :title, :not_a_number, :default => nil, :value => 'a') do
328
- Topic.validates_numericality_of :title
329
- @topic.title = 'a'
330
- end
331
- end
332
-
333
- test "#validates_numericality_of (:not_a_number, w/o :only_integer) and a custom message" do
334
- expect_error_added(@topic, :title, :not_a_number, :default => 'custom', :value => 'a') do
335
- Topic.validates_numericality_of :title, :message => 'custom'
336
- @topic.title = 'a'
337
- end
338
- end
339
-
340
- test "#validates_numericality_of (:not_a_number, w/o :only_integer) finds the correct message translations" do
341
- assert_message_translations(@topic, :title, :not_a_number) do
342
- Topic.validates_numericality_of :title
343
- @topic.title = 'a'
344
- end
345
- end
346
-
347
- # validates_numericality_of :not_a_number, with :only_integer
348
-
349
- test "#validates_numericality_of (:not_a_number, with :only_integer) no custom message" do
350
- expect_error_added(@topic, :title, :not_a_number, :default => nil, :value => 'a') do
351
- Topic.validates_numericality_of :title, :only_integer => true
352
- @topic.title = 'a'
353
- end
354
- end
355
-
356
- test "#validates_numericality_of (:not_a_number, with :only_integer) and a custom message" do
357
- expect_error_added(@topic, :title, :not_a_number, :default => 'custom', :value => 'a') do
358
- Topic.validates_numericality_of :title, :only_integer => true, :message => 'custom'
359
- @topic.title = 'a'
360
- end
361
- end
362
-
363
- test "#validates_numericality_of (:not_a_number, with :only_integer) finds the correct message translations" do
364
- assert_message_translations(@topic, :title, :not_a_number) do
365
- Topic.validates_numericality_of :title, :only_integer => true
366
- @topic.title = 'a'
367
- end
368
- end
369
-
370
- # validates_numericality_of :odd
371
-
372
- test "#validates_numericality_of (:odd) no custom message" do
373
- expect_error_added(@topic, :title, :odd, :default => nil, :value => 0) do
374
- Topic.validates_numericality_of :title, :only_integer => true, :odd => true
375
- @topic.title = 0
376
- end
377
- end
378
-
379
- test "#validates_numericality_of (:odd) and a custom message" do
380
- expect_error_added(@topic, :title, :odd, :default => 'custom', :value => 0) do
381
- Topic.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
382
- @topic.title = 0
383
- end
384
- end
385
-
386
- test "#validates_numericality_of (:odd) finds the correct message translations" do
387
- assert_message_translations(@topic, :title, :odd) do
388
- Topic.validates_numericality_of :title, :only_integer => true, :odd => true
389
- @topic.title = 0
390
- end
391
- end
392
-
393
- # validates_numericality_of :even
394
-
395
- test "#validates_numericality_of (:even) no custom message" do
396
- expect_error_added(@topic, :title, :even, :default => nil, :value => 1) do
397
- Topic.validates_numericality_of :title, :only_integer => true, :even => true
398
- @topic.title = 1
399
- end
400
- end
401
-
402
- test "#validates_numericality_of (:even) and a custom message" do
403
- expect_error_added(@topic, :title, :even, :default => 'custom', :value => 1) do
404
- Topic.validates_numericality_of :title, :only_integer => true, :even => true, :message => 'custom'
405
- @topic.title = 1
406
- end
407
- end
408
-
409
- test "#validates_numericality_of (:even) finds the correct message translations" do
410
- assert_message_translations(@topic, :title, :even) do
411
- Topic.validates_numericality_of :title, :only_integer => true, :even => true
412
- @topic.title = 1
413
- end
414
- end
415
-
416
- # validates_numericality_of :less_than
417
-
418
- test "#validates_numericality_of (:less_than) no custom message" do
419
- expect_error_added(@topic, :title, :less_than, :default => nil, :value => 1, :count => 0) do
420
- Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
421
- @topic.title = 1
422
- end
423
- end
424
-
425
- test "#validates_numericality_of (:less_than) and a custom message" do
426
- expect_error_added(@topic, :title, :less_than, :default => 'custom', :value => 1, :count => 0) do
427
- Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
428
- @topic.title = 1
429
- end
430
- end
431
-
432
- test "#validates_numericality_of (:less_than) finds the correct message translations" do
433
- assert_message_translations(@topic, :title, :less_than) do
434
- Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
435
- @topic.title = 1
436
- end
437
- end
438
-
439
- # validates_associated
440
-
441
- test "#validates_associated no custom message" do
442
- expect_error_added(replied_topic, :replies, :invalid, :default => nil, :value => replied_topic.replies) do
443
- Topic.validates_associated :replies
444
- end
445
- end
446
-
447
- test "#validates_associated and a custom message" do
448
- expect_error_added(replied_topic, :replies, :invalid, :default => 'custom', :value => replied_topic.replies) do
449
- Topic.validates_associated :replies, :message => 'custom'
450
- end
451
- end
452
-
453
- test "#validates_associated finds the correct message translations" do
454
- assert_message_translations(replied_topic, :replies, :invalid) do
455
- Topic.validates_associated :replies
456
- end
457
- end
458
- end
459
-
460
-
461
- # ACTIVERECORD ERROR
462
- #
463
- # * test that it passes given interpolation arguments, the human model name and human attribute name
464
- # * test that it looks messages up with the the correct keys
465
- # * test that it looks up the correct default messages
466
-
467
- class ActiveRecordErrorI18nTests < ActiveSupport::TestCase
468
- include ActiveRecordValidationsI18nTestHelper
469
-
470
- def setup
471
- @reply = Reply.new
472
- @old_backend, I18n.backend = I18n.backend, I18n::Backend::Simple.new
473
- end
474
-
475
- def teardown
476
- I18n.backend = @old_backend
477
- I18n.locale = nil
478
- end
479
-
480
- def assert_error_message(message, *args)
481
- assert_equal message, ActiveRecord::Error.new(@reply, *args).message
482
- end
483
-
484
- def assert_full_message(message, *args)
485
- assert_equal message, ActiveRecord::Error.new(@reply, *args).full_message
486
- end
487
-
488
- test ":default is only given to message if a symbol is supplied" do
489
- store_translations(:errors => { :messages => { :"foo bar" => "You fooed: %{value}." } })
490
- @reply.errors.add(:title, :inexistent, :default => "foo bar")
491
- assert_equal "foo bar", @reply.errors[:title]
492
- end
493
-
494
- test "#generate_message passes the model attribute value for interpolation" do
495
- store_translations(:errors => { :messages => { :foo => "You fooed: %{value}." } })
496
- @reply.title = "da title"
497
- assert_error_message 'You fooed: da title.', :title, :foo
498
- end
499
-
500
- test "#generate_message passes the human_name of the model for interpolation" do
501
- store_translations(
502
- :errors => { :messages => { :foo => "You fooed: %{model}." } },
503
- :models => { :topic => 'da topic' }
504
- )
505
- assert_error_message 'You fooed: da topic.', :title, :foo
506
- end
507
-
508
- test "#generate_message passes the human_name of the attribute for interpolation" do
509
- store_translations(
510
- :errors => { :messages => { :foo => "You fooed: %{attribute}." } },
511
- :attributes => { :topic => { :title => 'da topic title' } }
512
- )
513
- assert_error_message 'You fooed: da topic title.', :title, :foo
514
- end
515
-
516
- # generate_message will look up the key for the error message (e.g. :blank) in these namespaces:
517
- #
518
- # activerecord.errors.models.reply.attributes.title
519
- # activerecord.errors.models.reply
520
- # activerecord.errors.models.topic.attributes.title
521
- # activerecord.errors.models.topic
522
- # [default from class level :validates_foo statement if this is a String]
523
- # activerecord.errors.messages
524
-
525
- test "#generate_message key fallbacks (given a String as key)" do
526
- store_translations(
527
- :errors => {
528
- :models => {
529
- :reply => {
530
- :attributes => { :title => { :custom => 'activerecord.errors.models.reply.attributes.title.custom' } },
531
- :custom => 'activerecord.errors.models.reply.custom'
532
- },
533
- :topic => {
534
- :attributes => { :title => { :custom => 'activerecord.errors.models.topic.attributes.title.custom' } },
535
- :custom => 'activerecord.errors.models.topic.custom'
536
- }
537
- },
538
- :messages => {
539
- :custom => 'activerecord.errors.messages.custom',
540
- :kaputt => 'activerecord.errors.messages.kaputt'
541
- }
542
- }
543
- )
544
-
545
- assert_error_message 'activerecord.errors.models.reply.attributes.title.custom', :title, :kaputt, :message => 'custom'
546
- delete_translation :'activerecord.errors.models.reply.attributes.title.custom'
547
-
548
- assert_error_message 'activerecord.errors.models.reply.custom', :title, :kaputt, :message => 'custom'
549
- delete_translation :'activerecord.errors.models.reply.custom'
550
-
551
- assert_error_message 'activerecord.errors.models.topic.attributes.title.custom', :title, :kaputt, :message => 'custom'
552
- delete_translation :'activerecord.errors.models.topic.attributes.title.custom'
553
-
554
- assert_error_message 'activerecord.errors.models.topic.custom', :title, :kaputt, :message => 'custom'
555
- delete_translation :'activerecord.errors.models.topic.custom'
556
-
557
- assert_error_message 'activerecord.errors.messages.custom', :title, :kaputt, :message => 'custom'
558
- delete_translation :'activerecord.errors.messages.custom'
559
-
560
- # Implementing this would clash with the AR default behaviour of using validates_foo :message => 'foo'
561
- # as an untranslated string. I.e. at this point we can either fall back to the given string from the
562
- # class-level macro (validates_*) or fall back to the default message for this validation type.
563
- # assert_error_message 'activerecord.errors.messages.kaputt', :title, :kaputt, :message => 'custom'
564
-
565
- assert_error_message 'custom', :title, :kaputt, :message => 'custom'
566
- end
567
-
568
- test "#generate_message key fallbacks (given a Symbol as key)" do
569
- store_translations(
570
- :errors => {
571
- :models => {
572
- :reply => {
573
- :attributes => { :title => { :kaputt => 'activerecord.errors.models.reply.attributes.title.kaputt' } },
574
- :kaputt => 'activerecord.errors.models.reply.kaputt'
575
- },
576
- :topic => {
577
- :attributes => { :title => { :kaputt => 'activerecord.errors.models.topic.attributes.title.kaputt' } },
578
- :kaputt => 'activerecord.errors.models.topic.kaputt'
579
- }
580
- },
581
- :messages => {
582
- :kaputt => 'activerecord.errors.messages.kaputt'
583
- }
584
- }
585
- )
586
-
587
- assert_error_message 'activerecord.errors.models.reply.attributes.title.kaputt', :title, :kaputt
588
- delete_translation :'activerecord.errors.models.reply.attributes.title.kaputt'
589
-
590
- assert_error_message 'activerecord.errors.models.reply.kaputt', :title, :kaputt
591
- delete_translation :'activerecord.errors.models.reply.kaputt'
592
-
593
- assert_error_message 'activerecord.errors.models.topic.attributes.title.kaputt', :title, :kaputt
594
- delete_translation :'activerecord.errors.models.topic.attributes.title.kaputt'
595
-
596
- assert_error_message 'activerecord.errors.models.topic.kaputt', :title, :kaputt
597
- delete_translation :'activerecord.errors.models.topic.kaputt'
598
-
599
- assert_error_message 'activerecord.errors.messages.kaputt', :title, :kaputt
600
- end
601
-
602
- # full_messages
603
-
604
- test "#full_message with no format present" do
605
- store_translations(:errors => { :messages => { :kaputt => 'is kaputt' } })
606
- assert_full_message 'Title is kaputt', :title, :kaputt
607
- end
608
-
609
- test "#full_message with a format present" do
610
- store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :format => '%{attribute}: %{message}' } })
611
- assert_full_message 'Title: is kaputt', :title, :kaputt
612
- end
613
-
614
- test "#full_message with a type specific format present" do
615
- store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :kaputt => '%{attribute} %{message}!' } })
616
- assert_full_message 'Title is kaputt!', :title, :kaputt
617
- end
618
-
619
- test "#full_message with class-level specified custom message" do
620
- store_translations(:errors => { :messages => { :broken => 'is kaputt' }, :full_messages => { :broken => '%{attribute} %{message}?!' } })
621
- assert_full_message 'Title is kaputt?!', :title, :kaputt, :message => :broken
622
- end
623
-
624
- test "#full_message with different scope" do
625
- store_translations(:my_errors => { :messages => { :kaputt => 'is kaputt' } })
626
- assert_full_message 'Title is kaputt', :title, :kaputt, :scope => [:activerecord, :my_errors]
627
-
628
- store_translations(:my_errors => { :full_messages => { :kaputt => '%{attribute} %{message}!' } })
629
- assert_full_message 'Title is kaputt!', :title, :kaputt, :scope => [:activerecord, :my_errors]
630
- end
631
-
632
- # switch locales
633
-
634
- test "#message allows to switch locales" do
635
- store_translations(:en, :errors => { :messages => { :kaputt => 'is kaputt' } })
636
- store_translations(:de, :errors => { :messages => { :kaputt => 'ist kaputt' } })
637
-
638
- assert_error_message 'is kaputt', :title, :kaputt
639
- I18n.locale = :de
640
- assert_error_message 'ist kaputt', :title, :kaputt
641
- I18n.locale = :en
642
- assert_error_message 'is kaputt', :title, :kaputt
643
- end
644
-
645
- test "#full_message allows to switch locales" do
646
- store_translations(:en, :errors => { :messages => { :kaputt => 'is kaputt' } }, :attributes => { :topic => { :title => 'The title' } })
647
- store_translations(:de, :errors => { :messages => { :kaputt => 'ist kaputt' } }, :attributes => { :topic => { :title => 'Der Titel' } })
648
-
649
- assert_full_message 'The title is kaputt', :title, :kaputt
650
- I18n.locale = :de
651
- assert_full_message 'Der Titel ist kaputt', :title, :kaputt
652
- I18n.locale = :en
653
- assert_full_message 'The title is kaputt', :title, :kaputt
654
- end
655
- end
656
-
657
- # ACTIVERECORD DEFAULT ERROR MESSAGES
658
- #
659
- # * test that Error generates the default error messages
660
-
661
- class ActiveRecordDefaultErrorMessagesI18nTests < ActiveSupport::TestCase
662
- def assert_default_error_message(message, *args)
663
- assert_equal message, error_message(*args)
664
- end
665
-
666
- def error_message(*args)
667
- ActiveRecord::Error.new(Topic.new, :title, *args).message
668
- end
669
-
670
- # used by: validates_inclusion_of
671
- test "default error message: inclusion" do
672
- assert_default_error_message 'is not included in the list', :inclusion, :value => 'title'
673
- end
674
-
675
- # used by: validates_exclusion_of
676
- test "default error message: exclusion" do
677
- assert_default_error_message 'is reserved', :exclusion, :value => 'title'
678
- end
679
-
680
- # used by: validates_associated and validates_format_of
681
- test "default error message: invalid" do
682
- assert_default_error_message 'is invalid', :invalid, :value => 'title'
683
- end
684
-
685
- # used by: validates_confirmation_of
686
- test "default error message: confirmation" do
687
- assert_default_error_message "doesn't match confirmation", :confirmation, :default => nil
688
- end
689
-
690
- # used by: validates_acceptance_of
691
- test "default error message: accepted" do
692
- assert_default_error_message "must be accepted", :accepted
693
- end
694
-
695
- # used by: add_on_empty
696
- test "default error message: empty" do
697
- assert_default_error_message "can't be empty", :empty
698
- end
699
-
700
- # used by: add_on_blank
701
- test "default error message: blank" do
702
- assert_default_error_message "can't be blank", :blank
703
- end
704
-
705
- # used by: validates_length_of
706
- test "default error message: too_long" do
707
- assert_default_error_message "is too long (maximum is 10 characters)", :too_long, :count => 10
708
- end
709
-
710
- # used by: validates_length_of
711
- test "default error message: too_short" do
712
- assert_default_error_message "is too short (minimum is 10 characters)", :too_short, :count => 10
713
- end
714
-
715
- # used by: validates_length_of
716
- test "default error message: wrong_length" do
717
- assert_default_error_message "is the wrong length (should be 10 characters)", :wrong_length, :count => 10
718
- end
719
-
720
- # used by: validates_uniqueness_of
721
- test "default error message: taken" do
722
- assert_default_error_message "has already been taken", :taken, :value => 'title'
723
- end
724
-
725
- # used by: validates_numericality_of
726
- test "default error message: not_a_number" do
727
- assert_default_error_message "is not a number", :not_a_number, :value => 'title'
728
- end
729
-
730
- # used by: validates_numericality_of
731
- test "default error message: greater_than" do
732
- assert_default_error_message "must be greater than 10", :greater_than, :value => 'title', :count => 10
733
- end
734
-
735
- # used by: validates_numericality_of
736
- test "default error message: greater_than_or_equal_to" do
737
- assert_default_error_message "must be greater than or equal to 10", :greater_than_or_equal_to, :value => 'title', :count => 10
738
- end
739
-
740
- # used by: validates_numericality_of
741
- test "default error message: equal_to" do
742
- assert_default_error_message "must be equal to 10", :equal_to, :value => 'title', :count => 10
743
- end
744
-
745
- # used by: validates_numericality_of
746
- test "default error message: less_than" do
747
- assert_default_error_message "must be less than 10", :less_than, :value => 'title', :count => 10
748
- end
749
-
750
- # used by: validates_numericality_of
751
- test "default error message: less_than_or_equal_to" do
752
- assert_default_error_message "must be less than or equal to 10", :less_than_or_equal_to, :value => 'title', :count => 10
753
- end
754
-
755
- # used by: validates_numericality_of
756
- test "default error message: odd" do
757
- assert_default_error_message "must be odd", :odd, :value => 'title', :count => 10
758
- end
759
-
760
- # used by: validates_numericality_of
761
- test "default error message: even" do
762
- assert_default_error_message "must be even", :even, :value => 'title', :count => 10
763
- end
764
-
765
- test "custom message string interpolation" do
766
- assert_equal 'custom message title', error_message(:invalid, :default => 'custom message %{value}', :value => 'title')
767
- end
768
- end
769
-
770
- # ACTIVERECORD VALIDATION ERROR MESSAGES - FULL STACK
771
- #
772
- # * test a few combinations full stack to ensure the tests above are correct
773
-
774
- class I18nPerson < Person
775
- end
776
-
777
- class ActiveRecordValidationsI18nFullStackTests < ActiveSupport::TestCase
778
- include ActiveRecordValidationsI18nTestHelper
779
-
780
- def setup
781
- reset_callbacks(I18nPerson)
782
- @old_backend, I18n.backend = I18n.backend, I18n::Backend::Simple.new
783
- @person = I18nPerson.new
784
- end
785
-
786
- def teardown
787
- reset_callbacks(I18nPerson)
788
- I18n.backend = @old_backend
789
- end
790
-
791
- def assert_name_invalid(message)
792
- yield
793
- @person.valid?
794
- assert_equal message, @person.errors.on(:name)
795
- end
796
-
797
- # Symbols as class-level validation messages
798
-
799
- test "Symbol as class level validation message translated per attribute (translation on child class)" do
800
- assert_name_invalid("is broken") do
801
- store_translations :errors => {:models => {:i18n_person => {:attributes => {:name => {:broken => "is broken"}}}}}
802
- I18nPerson.validates_presence_of :name, :message => :broken
803
- end
804
- end
805
-
806
- test "Symbol as class level validation message translated per attribute (translation on base class)" do
807
- assert_name_invalid("is broken") do
808
- store_translations :errors => {:models => {:person => {:attributes => {:name => {:broken => "is broken"}}}}}
809
- I18nPerson.validates_presence_of :name, :message => :broken
810
- end
811
- end
812
-
813
- test "Symbol as class level validation message translated per model (translation on child class)" do
814
- assert_name_invalid("is broken") do
815
- store_translations :errors => {:models => {:i18n_person => {:broken => "is broken"}}}
816
- I18nPerson.validates_presence_of :name, :message => :broken
817
- end
818
- end
819
-
820
- test "Symbol as class level validation message translated per model (translation on base class)" do
821
- assert_name_invalid("is broken") do
822
- store_translations :errors => {:models => {:person => {:broken => "is broken"}}}
823
- I18nPerson.validates_presence_of :name, :message => :broken
824
- end
825
- end
826
-
827
- test "Symbol as class level validation message translated as error message" do
828
- assert_name_invalid("is broken") do
829
- store_translations :errors => {:messages => {:broken => "is broken"}}
830
- I18nPerson.validates_presence_of :name, :message => :broken
831
- end
832
- end
833
-
834
- # Strings as class-level validation messages
835
-
836
- test "String as class level validation message translated per attribute (translation on child class)" do
837
- assert_name_invalid("is broken") do
838
- store_translations :errors => {:models => {:i18n_person => {:attributes => {:name => {"is broken" => "is broken"}}}}}
839
- I18nPerson.validates_presence_of :name, :message => "is broken"
840
- end
841
- end
842
-
843
- test "String as class level validation message translated per attribute (translation on base class)" do
844
- assert_name_invalid("is broken") do
845
- store_translations :errors => {:models => {:person => {:attributes => {:name => {"is broken" => "is broken"}}}}}
846
- I18nPerson.validates_presence_of :name, :message => "is broken"
847
- end
848
- end
849
-
850
- test "String as class level validation message translated per model (translation on child class)" do
851
- assert_name_invalid("is broken") do
852
- store_translations :errors => {:models => {:i18n_person => {"is broken" => "is broken"}}}
853
- I18nPerson.validates_presence_of :name, :message => "is broken"
854
- end
855
- end
856
-
857
- test "String as class level validation message translated per model (translation on base class)" do
858
- assert_name_invalid("is broken") do
859
- store_translations :errors => {:models => {:person => {"is broken" => "is broken"}}}
860
- I18nPerson.validates_presence_of :name, :message => "is broken"
861
- end
862
- end
863
-
864
- test "String as class level validation message translated as error message" do
865
- assert_name_invalid("is broken") do
866
- store_translations :errors => {:messages => {"is broken" => "is broken"}}
867
- I18nPerson.validates_presence_of :name, :message => "is broken"
868
- end
869
- end
870
-
871
- test "String as class level validation message not translated (uses message as default)" do
872
- assert_name_invalid("is broken!") do
873
- I18nPerson.validates_presence_of :name, :message => "is broken!"
874
- end
875
- end
876
- end
877
-
878
- class ActiveRecordValidationsI18nFullMessagesFullStackTests < ActiveSupport::TestCase
879
- include ActiveRecordValidationsI18nTestHelper
880
-
881
- def setup
882
- reset_callbacks(I18nPerson)
883
- @old_backend, I18n.backend = I18n.backend, I18n::Backend::Simple.new
884
- @person = I18nPerson.new
885
- end
886
-
887
- def teardown
888
- reset_callbacks(I18nPerson)
889
- I18n.backend = @old_backend
890
- end
891
-
892
- def assert_full_message(message)
893
- yield
894
- @person.valid?
895
- assert_equal message, @person.errors.full_messages.join
896
- end
897
-
898
- test "full_message format stored per custom error message key" do
899
- assert_full_message("Name is broken!") do
900
- store_translations :errors => { :messages => { :broken => 'is broken' }, :full_messages => { :broken => '%{attribute} %{message}!' } }
901
- I18nPerson.validates_presence_of :name, :message => :broken
902
- end
903
- end
904
-
905
- test "full_message format stored per error type" do
906
- assert_full_message("Name can't be blank!") do
907
- store_translations :errors => { :full_messages => { :blank => '%{attribute} %{message}!' } }
908
- I18nPerson.validates_presence_of :name
909
- end
910
- end
911
- # ActiveRecord#RecordInvalid exception
912
-
913
- test "full_message format stored as default" do
914
- assert_full_message("Name: can't be blank") do
915
- store_translations :errors => { :full_messages => { :format => '%{attribute}: %{message}' } }
916
- I18nPerson.validates_presence_of :name
917
- end
918
- end
919
- test "RecordInvalid exception can be localized" do
920
- topic = Topic.new
921
- topic.errors.add(:title, :invalid)
922
- topic.errors.add(:title, :blank)
923
- assert_equal "Validation failed: Title is invalid, Title can't be blank", ActiveRecord::RecordInvalid.new(topic).message
924
- end
925
- end