activerecord 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (311) hide show
  1. data/CHANGELOG +4928 -3
  2. data/README +45 -46
  3. data/RUNNING_UNIT_TESTS +8 -11
  4. data/Rakefile +247 -0
  5. data/install.rb +8 -38
  6. data/lib/active_record/aggregations.rb +64 -49
  7. data/lib/active_record/associations/association_collection.rb +217 -47
  8. data/lib/active_record/associations/association_proxy.rb +159 -0
  9. data/lib/active_record/associations/belongs_to_association.rb +56 -0
  10. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +50 -0
  11. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +155 -37
  12. data/lib/active_record/associations/has_many_association.rb +145 -75
  13. data/lib/active_record/associations/has_many_through_association.rb +283 -0
  14. data/lib/active_record/associations/has_one_association.rb +96 -0
  15. data/lib/active_record/associations.rb +1537 -304
  16. data/lib/active_record/attribute_methods.rb +328 -0
  17. data/lib/active_record/base.rb +2001 -588
  18. data/lib/active_record/calculations.rb +269 -0
  19. data/lib/active_record/callbacks.rb +169 -165
  20. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +308 -0
  21. data/lib/active_record/connection_adapters/abstract/database_statements.rb +171 -0
  22. data/lib/active_record/connection_adapters/abstract/query_cache.rb +87 -0
  23. data/lib/active_record/connection_adapters/abstract/quoting.rb +69 -0
  24. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +472 -0
  25. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +306 -0
  26. data/lib/active_record/connection_adapters/abstract_adapter.rb +125 -279
  27. data/lib/active_record/connection_adapters/mysql_adapter.rb +442 -77
  28. data/lib/active_record/connection_adapters/postgresql_adapter.rb +805 -135
  29. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +34 -0
  30. data/lib/active_record/connection_adapters/sqlite_adapter.rb +353 -69
  31. data/lib/active_record/fixtures.rb +946 -100
  32. data/lib/active_record/locking/optimistic.rb +144 -0
  33. data/lib/active_record/locking/pessimistic.rb +77 -0
  34. data/lib/active_record/migration.rb +417 -0
  35. data/lib/active_record/observer.rb +142 -32
  36. data/lib/active_record/query_cache.rb +23 -0
  37. data/lib/active_record/reflection.rb +163 -70
  38. data/lib/active_record/schema.rb +58 -0
  39. data/lib/active_record/schema_dumper.rb +171 -0
  40. data/lib/active_record/serialization.rb +98 -0
  41. data/lib/active_record/serializers/json_serializer.rb +71 -0
  42. data/lib/active_record/serializers/xml_serializer.rb +315 -0
  43. data/lib/active_record/timestamp.rb +41 -0
  44. data/lib/active_record/transactions.rb +87 -57
  45. data/lib/active_record/validations.rb +909 -122
  46. data/lib/active_record/vendor/db2.rb +362 -0
  47. data/lib/active_record/vendor/mysql.rb +126 -29
  48. data/lib/active_record/version.rb +9 -0
  49. data/lib/active_record.rb +35 -7
  50. data/lib/activerecord.rb +1 -0
  51. data/test/aaa_create_tables_test.rb +72 -0
  52. data/test/abstract_unit.rb +73 -5
  53. data/test/active_schema_test_mysql.rb +43 -0
  54. data/test/adapter_test.rb +105 -0
  55. data/test/adapter_test_sqlserver.rb +95 -0
  56. data/test/aggregations_test.rb +110 -16
  57. data/test/all.sh +2 -2
  58. data/test/ar_schema_test.rb +33 -0
  59. data/test/association_inheritance_reload.rb +14 -0
  60. data/test/associations/ar_joins_test.rb +0 -0
  61. data/test/associations/callbacks_test.rb +147 -0
  62. data/test/associations/cascaded_eager_loading_test.rb +110 -0
  63. data/test/associations/eager_singularization_test.rb +145 -0
  64. data/test/associations/eager_test.rb +442 -0
  65. data/test/associations/extension_test.rb +47 -0
  66. data/test/associations/inner_join_association_test.rb +88 -0
  67. data/test/associations/join_model_test.rb +553 -0
  68. data/test/associations_test.rb +1930 -267
  69. data/test/attribute_methods_test.rb +146 -0
  70. data/test/base_test.rb +1316 -84
  71. data/test/binary_test.rb +32 -0
  72. data/test/calculations_test.rb +251 -0
  73. data/test/callbacks_test.rb +400 -0
  74. data/test/class_inheritable_attributes_test.rb +3 -4
  75. data/test/column_alias_test.rb +17 -0
  76. data/test/connection_test_firebird.rb +8 -0
  77. data/test/connection_test_mysql.rb +30 -0
  78. data/test/connections/native_db2/connection.rb +25 -0
  79. data/test/connections/native_firebird/connection.rb +26 -0
  80. data/test/connections/native_frontbase/connection.rb +27 -0
  81. data/test/connections/native_mysql/connection.rb +21 -18
  82. data/test/connections/native_openbase/connection.rb +21 -0
  83. data/test/connections/native_oracle/connection.rb +27 -0
  84. data/test/connections/native_postgresql/connection.rb +17 -18
  85. data/test/connections/native_sqlite/connection.rb +17 -16
  86. data/test/connections/native_sqlite3/connection.rb +25 -0
  87. data/test/connections/native_sqlite3/in_memory_connection.rb +18 -0
  88. data/test/connections/native_sybase/connection.rb +23 -0
  89. data/test/copy_table_test_sqlite.rb +69 -0
  90. data/test/datatype_test_postgresql.rb +203 -0
  91. data/test/date_time_test.rb +37 -0
  92. data/test/default_test_firebird.rb +16 -0
  93. data/test/defaults_test.rb +67 -0
  94. data/test/deprecated_finder_test.rb +30 -0
  95. data/test/finder_test.rb +607 -32
  96. data/test/fixtures/accounts.yml +28 -0
  97. data/test/fixtures/all/developers.yml +0 -0
  98. data/test/fixtures/all/people.csv +0 -0
  99. data/test/fixtures/all/tasks.yml +0 -0
  100. data/test/fixtures/author.rb +107 -0
  101. data/test/fixtures/author_favorites.yml +4 -0
  102. data/test/fixtures/authors.yml +7 -0
  103. data/test/fixtures/bad_fixtures/attr_with_numeric_first_char +1 -0
  104. data/test/fixtures/bad_fixtures/attr_with_spaces +1 -0
  105. data/test/fixtures/bad_fixtures/blank_line +3 -0
  106. data/test/fixtures/bad_fixtures/duplicate_attributes +3 -0
  107. data/test/fixtures/bad_fixtures/missing_value +1 -0
  108. data/test/fixtures/binaries.yml +132 -0
  109. data/test/fixtures/binary.rb +2 -0
  110. data/test/fixtures/book.rb +4 -0
  111. data/test/fixtures/books.yml +7 -0
  112. data/test/fixtures/categories/special_categories.yml +9 -0
  113. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  114. data/test/fixtures/categories.yml +14 -0
  115. data/test/fixtures/categories_ordered.yml +7 -0
  116. data/test/fixtures/categories_posts.yml +23 -0
  117. data/test/fixtures/categorization.rb +5 -0
  118. data/test/fixtures/categorizations.yml +17 -0
  119. data/test/fixtures/category.rb +26 -0
  120. data/test/fixtures/citation.rb +6 -0
  121. data/test/fixtures/comment.rb +23 -0
  122. data/test/fixtures/comments.yml +59 -0
  123. data/test/fixtures/companies.yml +55 -0
  124. data/test/fixtures/company.rb +81 -4
  125. data/test/fixtures/company_in_module.rb +32 -6
  126. data/test/fixtures/computer.rb +4 -0
  127. data/test/fixtures/computers.yml +4 -0
  128. data/test/fixtures/contact.rb +16 -0
  129. data/test/fixtures/courses.yml +7 -0
  130. data/test/fixtures/customer.rb +28 -3
  131. data/test/fixtures/customers.yml +17 -0
  132. data/test/fixtures/db_definitions/db2.drop.sql +33 -0
  133. data/test/fixtures/db_definitions/db2.sql +235 -0
  134. data/test/fixtures/db_definitions/db22.drop.sql +2 -0
  135. data/test/fixtures/db_definitions/db22.sql +5 -0
  136. data/test/fixtures/db_definitions/firebird.drop.sql +65 -0
  137. data/test/fixtures/db_definitions/firebird.sql +310 -0
  138. data/test/fixtures/db_definitions/firebird2.drop.sql +2 -0
  139. data/test/fixtures/db_definitions/firebird2.sql +6 -0
  140. data/test/fixtures/db_definitions/frontbase.drop.sql +33 -0
  141. data/test/fixtures/db_definitions/frontbase.sql +273 -0
  142. data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
  143. data/test/fixtures/db_definitions/frontbase2.sql +4 -0
  144. data/test/fixtures/db_definitions/openbase.drop.sql +2 -0
  145. data/test/fixtures/db_definitions/openbase.sql +318 -0
  146. data/test/fixtures/db_definitions/openbase2.drop.sql +2 -0
  147. data/test/fixtures/db_definitions/openbase2.sql +7 -0
  148. data/test/fixtures/db_definitions/oracle.drop.sql +67 -0
  149. data/test/fixtures/db_definitions/oracle.sql +330 -0
  150. data/test/fixtures/db_definitions/oracle2.drop.sql +2 -0
  151. data/test/fixtures/db_definitions/oracle2.sql +6 -0
  152. data/test/fixtures/db_definitions/postgresql.drop.sql +44 -0
  153. data/test/fixtures/db_definitions/postgresql.sql +217 -38
  154. data/test/fixtures/db_definitions/postgresql2.drop.sql +2 -0
  155. data/test/fixtures/db_definitions/postgresql2.sql +2 -2
  156. data/test/fixtures/db_definitions/schema.rb +354 -0
  157. data/test/fixtures/db_definitions/schema2.rb +11 -0
  158. data/test/fixtures/db_definitions/sqlite.drop.sql +33 -0
  159. data/test/fixtures/db_definitions/sqlite.sql +139 -5
  160. data/test/fixtures/db_definitions/sqlite2.drop.sql +2 -0
  161. data/test/fixtures/db_definitions/sqlite2.sql +1 -0
  162. data/test/fixtures/db_definitions/sybase.drop.sql +35 -0
  163. data/test/fixtures/db_definitions/sybase.sql +222 -0
  164. data/test/fixtures/db_definitions/sybase2.drop.sql +4 -0
  165. data/test/fixtures/db_definitions/sybase2.sql +5 -0
  166. data/test/fixtures/developer.rb +70 -6
  167. data/test/fixtures/developers.yml +21 -0
  168. data/test/fixtures/developers_projects/david_action_controller +2 -1
  169. data/test/fixtures/developers_projects/david_active_record +2 -1
  170. data/test/fixtures/developers_projects.yml +17 -0
  171. data/test/fixtures/edge.rb +5 -0
  172. data/test/fixtures/edges.yml +6 -0
  173. data/test/fixtures/entrants.yml +14 -0
  174. data/test/fixtures/example.log +1 -0
  175. data/test/fixtures/fk_test_has_fk.yml +3 -0
  176. data/test/fixtures/fk_test_has_pk.yml +2 -0
  177. data/test/fixtures/flowers.jpg +0 -0
  178. data/test/fixtures/funny_jokes.yml +10 -0
  179. data/test/fixtures/item.rb +7 -0
  180. data/test/fixtures/items.yml +4 -0
  181. data/test/fixtures/joke.rb +3 -0
  182. data/test/fixtures/keyboard.rb +3 -0
  183. data/test/fixtures/legacy_thing.rb +3 -0
  184. data/test/fixtures/legacy_things.yml +3 -0
  185. data/test/fixtures/matey.rb +4 -0
  186. data/test/fixtures/mateys.yml +4 -0
  187. data/test/fixtures/migrations/1_people_have_last_names.rb +9 -0
  188. data/test/fixtures/migrations/2_we_need_reminders.rb +12 -0
  189. data/test/fixtures/migrations/3_innocent_jointable.rb +12 -0
  190. data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
  191. data/test/fixtures/migrations_with_duplicate/1_people_have_last_names.rb +9 -0
  192. data/test/fixtures/migrations_with_duplicate/2_we_need_reminders.rb +12 -0
  193. data/test/fixtures/migrations_with_duplicate/3_foo.rb +7 -0
  194. data/test/fixtures/migrations_with_duplicate/3_innocent_jointable.rb +12 -0
  195. data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
  196. data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
  197. data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
  198. data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
  199. data/test/fixtures/minimalistic.rb +2 -0
  200. data/test/fixtures/minimalistics.yml +2 -0
  201. data/test/fixtures/mixed_case_monkey.rb +3 -0
  202. data/test/fixtures/mixed_case_monkeys.yml +6 -0
  203. data/test/fixtures/mixins.yml +29 -0
  204. data/test/fixtures/movies.yml +7 -0
  205. data/test/fixtures/naked/csv/accounts.csv +1 -0
  206. data/test/fixtures/naked/yml/accounts.yml +1 -0
  207. data/test/fixtures/naked/yml/companies.yml +1 -0
  208. data/test/fixtures/naked/yml/courses.yml +1 -0
  209. data/test/fixtures/order.rb +4 -0
  210. data/test/fixtures/parrot.rb +13 -0
  211. data/test/fixtures/parrots.yml +27 -0
  212. data/test/fixtures/parrots_pirates.yml +7 -0
  213. data/test/fixtures/people.yml +3 -0
  214. data/test/fixtures/person.rb +4 -0
  215. data/test/fixtures/pirate.rb +5 -0
  216. data/test/fixtures/pirates.yml +9 -0
  217. data/test/fixtures/post.rb +59 -0
  218. data/test/fixtures/posts.yml +48 -0
  219. data/test/fixtures/project.rb +27 -2
  220. data/test/fixtures/projects.yml +7 -0
  221. data/test/fixtures/reader.rb +4 -0
  222. data/test/fixtures/readers.yml +4 -0
  223. data/test/fixtures/reply.rb +18 -2
  224. data/test/fixtures/reserved_words/distinct.yml +5 -0
  225. data/test/fixtures/reserved_words/distincts_selects.yml +11 -0
  226. data/test/fixtures/reserved_words/group.yml +14 -0
  227. data/test/fixtures/reserved_words/select.yml +8 -0
  228. data/test/fixtures/reserved_words/values.yml +7 -0
  229. data/test/fixtures/ship.rb +3 -0
  230. data/test/fixtures/ships.yml +5 -0
  231. data/test/fixtures/subject.rb +4 -0
  232. data/test/fixtures/subscriber.rb +4 -3
  233. data/test/fixtures/tag.rb +7 -0
  234. data/test/fixtures/tagging.rb +10 -0
  235. data/test/fixtures/taggings.yml +25 -0
  236. data/test/fixtures/tags.yml +7 -0
  237. data/test/fixtures/task.rb +3 -0
  238. data/test/fixtures/tasks.yml +7 -0
  239. data/test/fixtures/topic.rb +20 -3
  240. data/test/fixtures/topics.yml +22 -0
  241. data/test/fixtures/treasure.rb +4 -0
  242. data/test/fixtures/treasures.yml +10 -0
  243. data/test/fixtures/vertex.rb +9 -0
  244. data/test/fixtures/vertices.yml +4 -0
  245. data/test/fixtures_test.rb +574 -8
  246. data/test/inheritance_test.rb +113 -27
  247. data/test/json_serialization_test.rb +180 -0
  248. data/test/lifecycle_test.rb +56 -29
  249. data/test/locking_test.rb +273 -0
  250. data/test/method_scoping_test.rb +416 -0
  251. data/test/migration_test.rb +933 -0
  252. data/test/migration_test_firebird.rb +124 -0
  253. data/test/mixin_test.rb +95 -0
  254. data/test/modules_test.rb +23 -10
  255. data/test/multiple_db_test.rb +17 -3
  256. data/test/pk_test.rb +59 -15
  257. data/test/query_cache_test.rb +104 -0
  258. data/test/readonly_test.rb +107 -0
  259. data/test/reflection_test.rb +124 -27
  260. data/test/reserved_word_test_mysql.rb +177 -0
  261. data/test/schema_authorization_test_postgresql.rb +75 -0
  262. data/test/schema_dumper_test.rb +131 -0
  263. data/test/schema_test_postgresql.rb +64 -0
  264. data/test/serialization_test.rb +47 -0
  265. data/test/synonym_test_oracle.rb +17 -0
  266. data/test/table_name_test_sqlserver.rb +23 -0
  267. data/test/threaded_connections_test.rb +48 -0
  268. data/test/transactions_test.rb +227 -29
  269. data/test/unconnected_test.rb +14 -6
  270. data/test/validations_test.rb +1293 -32
  271. data/test/xml_serialization_test.rb +202 -0
  272. metadata +347 -143
  273. data/dev-utils/eval_debugger.rb +0 -9
  274. data/examples/associations.rb +0 -87
  275. data/examples/shared_setup.rb +0 -15
  276. data/examples/validation.rb +0 -88
  277. data/lib/active_record/deprecated_associations.rb +0 -70
  278. data/lib/active_record/support/class_attribute_accessors.rb +0 -43
  279. data/lib/active_record/support/class_inheritable_attributes.rb +0 -37
  280. data/lib/active_record/support/clean_logger.rb +0 -10
  281. data/lib/active_record/support/inflector.rb +0 -70
  282. data/lib/active_record/vendor/simple.rb +0 -702
  283. data/lib/active_record/wrappers/yaml_wrapper.rb +0 -15
  284. data/lib/active_record/wrappings.rb +0 -59
  285. data/rakefile +0 -122
  286. data/test/deprecated_associations_test.rb +0 -336
  287. data/test/fixtures/accounts/signals37 +0 -3
  288. data/test/fixtures/accounts/unknown +0 -2
  289. data/test/fixtures/companies/first_client +0 -6
  290. data/test/fixtures/companies/first_firm +0 -4
  291. data/test/fixtures/companies/second_client +0 -6
  292. data/test/fixtures/courses/java +0 -2
  293. data/test/fixtures/courses/ruby +0 -2
  294. data/test/fixtures/customers/david +0 -6
  295. data/test/fixtures/db_definitions/mysql.sql +0 -96
  296. data/test/fixtures/db_definitions/mysql2.sql +0 -4
  297. data/test/fixtures/developers/david +0 -2
  298. data/test/fixtures/developers/jamis +0 -2
  299. data/test/fixtures/entrants/first +0 -3
  300. data/test/fixtures/entrants/second +0 -3
  301. data/test/fixtures/entrants/third +0 -3
  302. data/test/fixtures/fixture_database.sqlite +0 -0
  303. data/test/fixtures/fixture_database_2.sqlite +0 -0
  304. data/test/fixtures/movies/first +0 -2
  305. data/test/fixtures/movies/second +0 -2
  306. data/test/fixtures/projects/action_controller +0 -2
  307. data/test/fixtures/projects/active_record +0 -2
  308. data/test/fixtures/topics/first +0 -9
  309. data/test/fixtures/topics/second +0 -8
  310. data/test/inflector_test.rb +0 -104
  311. data/test/thread_safety_test.rb +0 -33
@@ -1,23 +1,88 @@
1
- class Company < ActiveRecord::Base
1
+ class AbstractCompany < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
4
+
5
+ class Company < AbstractCompany
2
6
  attr_protected :rating
7
+ set_sequence_name :companies_nonstd_seq
8
+
9
+ validates_presence_of :name
10
+
11
+ has_one :dummy_account, :foreign_key => "firm_id", :class_name => "Account"
12
+
13
+ def arbitrary_method
14
+ "I am Jack's profound disappointment"
15
+ end
3
16
  end
4
17
 
5
18
 
6
19
  class Firm < Company
7
- has_many :clients, :order => "id", :dependent => true
20
+ has_many :clients, :order => "id", :dependent => :destroy, :counter_sql =>
21
+ "SELECT COUNT(*) FROM companies WHERE firm_id = 1 " +
22
+ "AND (#{QUOTED_TYPE} = 'Client' OR #{QUOTED_TYPE} = 'SpecialClient' OR #{QUOTED_TYPE} = 'VerySpecialClient' )"
8
23
  has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
9
24
  has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
25
+ has_many :dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :destroy
26
+ has_many :exclusively_dependent_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all
27
+ has_many :limited_clients, :class_name => "Client", :order => "id", :limit => 1
10
28
  has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
29
+ has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}'
30
+ has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id"
11
31
  has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
32
+ has_many :clients_using_counter_sql, :class_name => "Client",
33
+ :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
34
+ :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}'
35
+ has_many :clients_using_zero_counter_sql, :class_name => "Client",
36
+ :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}',
37
+ :counter_sql => 'SELECT 0 FROM companies WHERE client_of = #{id}'
38
+ has_many :no_clients_using_counter_sql, :class_name => "Client",
39
+ :finder_sql => 'SELECT * FROM companies WHERE client_of = 1000',
40
+ :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000'
41
+ has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1'
42
+ has_many :plain_clients, :class_name => 'Client'
43
+
44
+ has_one :account, :foreign_key => "firm_id", :dependent => :destroy
45
+ end
46
+
47
+ class DependentFirm < Company
48
+ has_one :account, :foreign_key => "firm_id", :dependent => :nullify
49
+ has_many :companies, :foreign_key => 'client_of', :order => "id", :dependent => :nullify
50
+ end
12
51
 
13
- has_one :account, :dependent => true
52
+ class ExclusivelyDependentFirm < Company
53
+ has_one :account, :foreign_key => "firm_id", :dependent => :delete
54
+ has_many :dependent_sanitized_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => "name = 'BigShot Inc.'"
55
+ has_many :dependent_conditional_clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id", :dependent => :delete_all, :conditions => ["name = ?", 'BigShot Inc.']
14
56
  end
15
57
 
16
58
  class Client < Company
17
59
  belongs_to :firm, :foreign_key => "client_of"
18
60
  belongs_to :firm_with_basic_id, :class_name => "Firm", :foreign_key => "firm_id"
19
61
  belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
20
- belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => "1 = 1"
62
+ belongs_to :firm_with_condition, :class_name => "Firm", :foreign_key => "client_of", :conditions => ["1 = ?", 1]
63
+
64
+ # Record destruction so we can test whether firm.clients.clear has
65
+ # is calling client.destroy, deleting from the database, or setting
66
+ # foreign keys to NULL.
67
+ def self.destroyed_client_ids
68
+ @destroyed_client_ids ||= Hash.new { |h,k| h[k] = [] }
69
+ end
70
+
71
+ before_destroy do |client|
72
+ if client.firm
73
+ Client.destroyed_client_ids[client.firm.id] << client.id
74
+ end
75
+ true
76
+ end
77
+
78
+ # Used to test that read and question methods are not generated for these attributes
79
+ def ruby_type
80
+ read_attribute :ruby_type
81
+ end
82
+
83
+ def rating?
84
+ query_attribute :rating
85
+ end
21
86
  end
22
87
 
23
88
 
@@ -30,6 +95,18 @@ end
30
95
  class Account < ActiveRecord::Base
31
96
  belongs_to :firm
32
97
 
98
+ def self.destroyed_account_ids
99
+ @destroyed_account_ids ||= Hash.new { |h,k| h[k] = [] }
100
+ end
101
+
102
+ before_destroy do |account|
103
+ if account.firm
104
+ Account.destroyed_account_ids[account.firm.id] << account.id
105
+ end
106
+ true
107
+ end
108
+
109
+
33
110
  protected
34
111
  def validate
35
112
  errors.add_on_empty "credit_limit"
@@ -3,27 +3,53 @@ module MyApplication
3
3
  class Company < ActiveRecord::Base
4
4
  attr_protected :rating
5
5
  end
6
-
6
+
7
7
  class Firm < Company
8
- has_many :clients, :order => "id", :dependent => true
8
+ has_many :clients, :order => "id", :dependent => :destroy
9
9
  has_many :clients_sorted_desc, :class_name => "Client", :order => "id DESC"
10
10
  has_many :clients_of_firm, :foreign_key => "client_of", :class_name => "Client", :order => "id"
11
11
  has_many :clients_like_ms, :conditions => "name = 'Microsoft'", :class_name => "Client", :order => "id"
12
12
  has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}'
13
13
 
14
- has_one :account, :dependent => true
14
+ has_one :account, :dependent => :destroy
15
15
  end
16
16
 
17
17
  class Client < Company
18
18
  belongs_to :firm, :foreign_key => "client_of"
19
19
  belongs_to :firm_with_other_name, :class_name => "Firm", :foreign_key => "client_of"
20
20
  end
21
+
22
+ class Developer < ActiveRecord::Base
23
+ has_and_belongs_to_many :projects
24
+ validates_length_of :name, :within => (3..20)
25
+ end
26
+
27
+ class Project < ActiveRecord::Base
28
+ has_and_belongs_to_many :developers
29
+ end
30
+
21
31
  end
22
-
32
+
23
33
  module Billing
34
+ class Firm < ActiveRecord::Base
35
+ self.table_name = 'companies'
36
+ end
37
+
38
+ module Nested
39
+ class Firm < ActiveRecord::Base
40
+ self.table_name = 'companies'
41
+ end
42
+ end
43
+
24
44
  class Account < ActiveRecord::Base
25
- belongs_to :firm, :class_name => "MyApplication::Business::Firm"
26
-
45
+ with_options(:foreign_key => :firm_id) do |i|
46
+ i.belongs_to :firm, :class_name => 'MyApplication::Business::Firm'
47
+ i.belongs_to :qualified_billing_firm, :class_name => 'MyApplication::Billing::Firm'
48
+ i.belongs_to :unqualified_billing_firm, :class_name => 'Firm'
49
+ i.belongs_to :nested_qualified_billing_firm, :class_name => 'MyApplication::Billing::Nested::Firm'
50
+ i.belongs_to :nested_unqualified_billing_firm, :class_name => 'Nested::Firm'
51
+ end
52
+
27
53
  protected
28
54
  def validate
29
55
  errors.add_on_empty "credit_limit"
@@ -0,0 +1,4 @@
1
+ class Computer < ActiveRecord::Base
2
+ belongs_to :developer, :foreign_key=>'developer'
3
+ end
4
+
@@ -0,0 +1,4 @@
1
+ workstation:
2
+ id: 1
3
+ developer: 1
4
+ extendedWarranty: 1
@@ -0,0 +1,16 @@
1
+ class Contact < ActiveRecord::Base
2
+ # mock out self.columns so no pesky db is needed for these tests
3
+ def self.column(name, sql_type = nil, options = {})
4
+ @columns ||= []
5
+ @columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, options[:default], sql_type.to_s, options[:null])
6
+ end
7
+
8
+ column :name, :string
9
+ column :age, :integer
10
+ column :avatar, :binary
11
+ column :created_at, :datetime
12
+ column :awesome, :boolean
13
+ column :preferences, :string
14
+
15
+ serialize :preferences
16
+ end
@@ -0,0 +1,7 @@
1
+ ruby:
2
+ id: 1
3
+ name: Ruby Development
4
+
5
+ java:
6
+ id: 2
7
+ name: Java Development
@@ -1,6 +1,7 @@
1
1
  class Customer < ActiveRecord::Base
2
- composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ]
3
- composed_of :balance, :class_name => "Money", :mapping => %w(balance amount)
2
+ composed_of :address, :mapping => [ %w(address_street street), %w(address_city city), %w(address_country country) ], :allow_nil => true
3
+ composed_of(:balance, :class_name => "Money", :mapping => %w(balance amount)) { |balance| balance.to_money }
4
+ composed_of :gps_location, :allow_nil => true
4
5
  end
5
6
 
6
7
  class Address
@@ -13,6 +14,10 @@ class Address
13
14
  def close_to?(other_address)
14
15
  city == other_address.city && country == other_address.country
15
16
  end
17
+
18
+ def ==(other)
19
+ other.is_a?(self.class) && other.street == street && other.city == city && other.country == country
20
+ end
16
21
  end
17
22
 
18
23
  class Money
@@ -27,4 +32,24 @@ class Money
27
32
  def exchange_to(other_currency)
28
33
  Money.new((amount * EXCHANGE_RATES["#{currency}_TO_#{other_currency}"]).floor, other_currency)
29
34
  end
30
- end
35
+ end
36
+
37
+ class GpsLocation
38
+ attr_reader :gps_location
39
+
40
+ def initialize(gps_location)
41
+ @gps_location = gps_location
42
+ end
43
+
44
+ def latitude
45
+ gps_location.split("x").first
46
+ end
47
+
48
+ def longitude
49
+ gps_location.split("x").last
50
+ end
51
+
52
+ def ==(other)
53
+ self.latitude == other.latitude && self.longitude == other.longitude
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ david:
2
+ id: 1
3
+ name: David
4
+ balance: 50
5
+ address_street: Funny Street
6
+ address_city: Scary Town
7
+ address_country: Loony Land
8
+ gps_location: 35.544623640962634x-105.9309951055148
9
+
10
+ zaphod:
11
+ id: 2
12
+ name: Zaphod
13
+ balance: 62
14
+ address_street: Avenue Road
15
+ address_city: Hamlet Town
16
+ address_country: Nation Land
17
+ gps_location: NULL
@@ -0,0 +1,33 @@
1
+ DROP TABLE accounts;
2
+ DROP TABLE funny_jokes;
3
+ DROP TABLE companies;
4
+ DROP TABLE topics;
5
+ DROP TABLE developers;
6
+ DROP TABLE projects;
7
+ DROP TABLE developers_projects;
8
+ DROP TABLE orders;
9
+ DROP TABLE customers;
10
+ DROP TABLE movies;
11
+ DROP TABLE subscribers;
12
+ DROP TABLE booleantests;
13
+ DROP TABLE auto_id_tests;
14
+ DROP TABLE entrants;
15
+ DROP TABLE colnametests;
16
+ DROP TABLE mixins;
17
+ DROP TABLE people;
18
+ DROP TABLE readers;
19
+ DROP TABLE binaries;
20
+ DROP TABLE computers;
21
+ DROP TABLE posts;
22
+ DROP TABLE comments;
23
+ DROP TABLE authors;
24
+ DROP TABLE tasks;
25
+ DROP TABLE categories;
26
+ DROP TABLE categories_posts;
27
+ DROP TABLE fk_test_has_pk;
28
+ DROP TABLE fk_test_has_fk;
29
+ DROP TABLE keyboards;
30
+ DROP TABLE legacy_things;
31
+ DROP TABLE numeric_data;
32
+ DROP TABLE mixed_case_monkeys;
33
+ DROP TABLE minimalistics;
@@ -0,0 +1,235 @@
1
+ CREATE TABLE accounts (
2
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
3
+ firm_id INT DEFAULT NULL,
4
+ credit_limit INT DEFAULT NULL,
5
+ PRIMARY KEY (id)
6
+ );
7
+
8
+ CREATE TABLE funny_jokes (
9
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
10
+ name VARCHAR(50) DEFAULT NULL,
11
+ PRIMARY KEY (id)
12
+ );
13
+
14
+ CREATE TABLE companies (
15
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
16
+ type VARCHAR(50) DEFAULT NULL,
17
+ ruby_type VARCHAR(50) DEFAULT NULL,
18
+ firm_id INT DEFAULT NULL,
19
+ name VARCHAR(50) DEFAULT NULL,
20
+ client_of INT DEFAULT NULL,
21
+ rating INT DEFAULT 1,
22
+ PRIMARY KEY (id)
23
+ );
24
+
25
+ CREATE TABLE topics (
26
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
27
+ title VARCHAR(255) DEFAULT NULL,
28
+ author_name VARCHAR(255) DEFAULT NULL,
29
+ author_email_address VARCHAR(255) DEFAULT NULL,
30
+ written_on TIMESTAMP DEFAULT NULL,
31
+ bonus_time TIME DEFAULT NULL,
32
+ last_read DATE DEFAULT NULL,
33
+ content VARCHAR(3000),
34
+ approved SMALLINT DEFAULT 1,
35
+ replies_count INT DEFAULT 0,
36
+ parent_id INT DEFAULT NULL,
37
+ type VARCHAR(50) DEFAULT NULL,
38
+ PRIMARY KEY (id)
39
+ );
40
+
41
+ CREATE TABLE developers (
42
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
43
+ name VARCHAR(100) DEFAULT NULL,
44
+ salary INT DEFAULT 70000,
45
+ created_at TIMESTAMP DEFAULT NULL,
46
+ updated_at TIMESTAMP DEFAULT NULL,
47
+ PRIMARY KEY (id)
48
+ );
49
+
50
+ CREATE TABLE projects (
51
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
52
+ name VARCHAR(100) DEFAULT NULL,
53
+ type VARCHAR(255) DEFAULT NULL,
54
+ PRIMARY KEY (id)
55
+ );
56
+
57
+ CREATE TABLE developers_projects (
58
+ developer_id INT NOT NULL,
59
+ project_id INT NOT NULL,
60
+ joined_on DATE DEFAULT NULL,
61
+ access_level SMALLINT DEFAULT 1
62
+ );
63
+
64
+ CREATE TABLE orders (
65
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
66
+ name VARCHAR(100) DEFAULT NULL,
67
+ billing_customer_id INT DEFAULT NULL,
68
+ shipping_customer_id INT DEFAULT NULL,
69
+ PRIMARY KEY (id)
70
+ );
71
+
72
+ CREATE TABLE customers (
73
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
74
+ name VARCHAR(100) DEFAULT NULL,
75
+ balance INT DEFAULT 0,
76
+ address_street VARCHAR(100) DEFAULT NULL,
77
+ address_city VARCHAR(100) DEFAULT NULL,
78
+ address_country VARCHAR(100) DEFAULT NULL,
79
+ gps_location VARCHAR(100) DEFAULT NULL,
80
+ PRIMARY KEY (id)
81
+ );
82
+
83
+ CREATE TABLE movies (
84
+ movieid INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
85
+ name VARCHAR(100) DEFAULT NULL,
86
+ PRIMARY KEY (movieid)
87
+ );
88
+
89
+ CREATE TABLE subscribers (
90
+ nick VARCHAR(100) NOT NULL,
91
+ name VARCHAR(100) DEFAULT NULL,
92
+ PRIMARY KEY (nick)
93
+ );
94
+
95
+ CREATE TABLE booleantests (
96
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
97
+ value INT DEFAULT NULL,
98
+ PRIMARY KEY (id)
99
+ );
100
+
101
+ CREATE TABLE auto_id_tests (
102
+ auto_id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
103
+ value INT DEFAULT NULL,
104
+ PRIMARY KEY (auto_id)
105
+ );
106
+
107
+ CREATE TABLE entrants (
108
+ id INT NOT NULL PRIMARY KEY,
109
+ name VARCHAR(255) NOT NULL,
110
+ course_id INT NOT NULL
111
+ );
112
+
113
+ CREATE TABLE colnametests (
114
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
115
+ references INT NOT NULL,
116
+ PRIMARY KEY (id)
117
+ );
118
+
119
+ CREATE TABLE mixins (
120
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
121
+ parent_id INT DEFAULT NULL,
122
+ pos INT DEFAULT NULL,
123
+ created_at TIMESTAMP DEFAULT NULL,
124
+ updated_at TIMESTAMP DEFAULT NULL,
125
+ lft INT DEFAULT NULL,
126
+ rgt INT DEFAULT NULL,
127
+ root_id INT DEFAULT NULL,
128
+ type VARCHAR(40) DEFAULT NULL,
129
+ PRIMARY KEY (id)
130
+ );
131
+
132
+ CREATE TABLE people (
133
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
134
+ first_name VARCHAR(40) NOT NULL,
135
+ lock_version INT DEFAULT 0,
136
+ PRIMARY KEY (id)
137
+ );
138
+
139
+ CREATE TABLE readers (
140
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
141
+ post_id INT NOT NULL,
142
+ person_id INT NOT NULL,
143
+ PRIMARY KEY (id)
144
+ );
145
+
146
+ CREATE TABLE binaries (
147
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
148
+ data BLOB(50000),
149
+ PRIMARY KEY (id)
150
+ );
151
+
152
+ CREATE TABLE computers (
153
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
154
+ developer INT NOT NULL,
155
+ extendedWarranty INT NOT NULL
156
+ );
157
+
158
+ CREATE TABLE posts (
159
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
160
+ author_id INT DEFAULT NULL,
161
+ title VARCHAR(255) DEFAULT NULL,
162
+ type VARCHAR(255) DEFAULT NULL,
163
+ body VARCHAR(3000) DEFAULT NULL
164
+ );
165
+
166
+ CREATE TABLE comments (
167
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
168
+ post_id INT DEFAULT NULL,
169
+ type VARCHAR(255) DEFAULT NULL,
170
+ body VARCHAR(3000) DEFAULT NULL
171
+ );
172
+
173
+ CREATE TABLE authors (
174
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
175
+ name VARCHAR(255) DEFAULT NULL
176
+ );
177
+
178
+ CREATE TABLE tasks (
179
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
180
+ starting TIMESTAMP DEFAULT NULL,
181
+ ending TIMESTAMP DEFAULT NULL
182
+ );
183
+
184
+ CREATE TABLE categories (
185
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
186
+ name VARCHAR(255) NOT NULL,
187
+ type VARCHAR(40) DEFAULT NULL
188
+ );
189
+
190
+ CREATE TABLE categories_posts (
191
+ category_id INT NOT NULL,
192
+ post_id INT NOT NULL
193
+ );
194
+
195
+ CREATE TABLE keyboards (
196
+ key_number INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
197
+ name VARCHAR(255)
198
+ );
199
+
200
+ CREATE TABLE fk_test_has_pk (
201
+ id INT NOT NULL PRIMARY KEY
202
+ );
203
+
204
+ CREATE TABLE fk_test_has_fk (
205
+ id INT NOT NULL PRIMARY KEY,
206
+ fk_id INT NOT NULL,
207
+
208
+ FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
209
+ );
210
+
211
+ --This table has an altered lock_version column name
212
+ CREATE TABLE legacy_things (
213
+ id INT GENERATED BY DEFAULT AS IDENTITY (START WITH 10000),
214
+ tps_report_number INT DEFAULT NULL,
215
+ version INT DEFAULT 0,
216
+ PRIMARY KEY (id)
217
+ );
218
+
219
+ CREATE TABLE numeric_data (
220
+ id INT NOT NULL PRIMARY KEY,
221
+ bank_balance DECIMAL(10,2),
222
+ big_bank_balance DECIMAL(15,2),
223
+ world_population DECIMAL(10),
224
+ my_house_population DECIMAL(2),
225
+ decimal_number_with_default DECIMAL(3,2) DEFAULT 2.78
226
+ );
227
+
228
+ CREATE TABLE mixed_case_monkeys (
229
+ monkeyID INT NOT NULL PRIMARY KEY,
230
+ fleaCount INT
231
+ );
232
+
233
+ CREATE TABLE minimalistics (
234
+ id INT NOT NULL PRIMARY KEY
235
+ );
@@ -0,0 +1,2 @@
1
+ DROP TABLE courses;
2
+
@@ -0,0 +1,5 @@
1
+ CREATE TABLE courses (
2
+ id INT NOT NULL PRIMARY KEY,
3
+ name VARCHAR(255) NOT NULL
4
+ );
5
+
@@ -0,0 +1,65 @@
1
+ DROP TABLE accounts;
2
+ DROP TABLE funny_jokes;
3
+ DROP TABLE companies;
4
+ DROP TABLE topics;
5
+ DROP TABLE developers;
6
+ DROP TABLE projects;
7
+ DROP TABLE developers_projects;
8
+ DROP TABLE orders;
9
+ DROP TABLE customers;
10
+ DROP TABLE movies;
11
+ DROP TABLE subscribers;
12
+ DROP TABLE booleantests;
13
+ DROP TABLE auto_id_tests;
14
+ DROP TABLE entrants;
15
+ DROP TABLE colnametests;
16
+ DROP TABLE mixins;
17
+ DROP TABLE people;
18
+ DROP TABLE readers;
19
+ DROP TABLE binaries;
20
+ DROP TABLE computers;
21
+ DROP TABLE posts;
22
+ DROP TABLE comments;
23
+ DROP TABLE authors;
24
+ DROP TABLE tasks;
25
+ DROP TABLE categories;
26
+ DROP TABLE categories_posts;
27
+ DROP TABLE fk_test_has_fk;
28
+ DROP TABLE fk_test_has_pk;
29
+ DROP TABLE keyboards;
30
+ DROP TABLE defaults;
31
+ DROP TABLE legacy_things;
32
+ DROP TABLE numeric_data;
33
+ DROP TABLE mixed_case_monkeys;
34
+ DROP TABLE minimalistics;
35
+
36
+ DROP DOMAIN D_BOOLEAN;
37
+
38
+ DROP GENERATOR accounts_seq;
39
+ DROP GENERATOR funny_jokes_seq;
40
+ DROP GENERATOR companies_nonstd_seq;
41
+ DROP GENERATOR topics_seq;
42
+ DROP GENERATOR developers_seq;
43
+ DROP GENERATOR projects_seq;
44
+ DROP GENERATOR orders_seq;
45
+ DROP GENERATOR customers_seq;
46
+ DROP GENERATOR movies_seq;
47
+ DROP GENERATOR booleantests_seq;
48
+ DROP GENERATOR auto_id_tests_seq;
49
+ DROP GENERATOR entrants_seq;
50
+ DROP GENERATOR colnametests_seq;
51
+ DROP GENERATOR mixins_seq;
52
+ DROP GENERATOR people_seq;
53
+ DROP GENERATOR binaries_seq;
54
+ DROP GENERATOR computers_seq;
55
+ DROP GENERATOR posts_seq;
56
+ DROP GENERATOR comments_seq;
57
+ DROP GENERATOR authors_seq;
58
+ DROP GENERATOR tasks_seq;
59
+ DROP GENERATOR categories_seq;
60
+ DROP GENERATOR keyboards_seq;
61
+ DROP GENERATOR defaults_seq;
62
+ DROP GENERATOR legacy_things_seq;
63
+ DROP GENERATOR numeric_data_seq;
64
+ DROP GENERATOR mixed_case_monkeys_seq;
65
+ DROP GENERATOR minimalistics_seq;