activerecord 1.1.0 → 1.2.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 (68) hide show
  1. data/CHANGELOG +250 -0
  2. data/README +17 -9
  3. data/dev-utils/eval_debugger.rb +1 -1
  4. data/install.rb +3 -1
  5. data/lib/active_record.rb +9 -2
  6. data/lib/active_record/acts/list.rb +178 -0
  7. data/lib/active_record/acts/tree.rb +44 -0
  8. data/lib/active_record/associations.rb +45 -8
  9. data/lib/active_record/associations/association_collection.rb +18 -9
  10. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +14 -13
  11. data/lib/active_record/associations/has_many_association.rb +21 -12
  12. data/lib/active_record/base.rb +137 -37
  13. data/lib/active_record/callbacks.rb +30 -25
  14. data/lib/active_record/connection_adapters/abstract_adapter.rb +57 -33
  15. data/lib/active_record/connection_adapters/mysql_adapter.rb +4 -0
  16. data/lib/active_record/connection_adapters/sqlite_adapter.rb +3 -2
  17. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +298 -0
  18. data/lib/active_record/fixtures.rb +241 -147
  19. data/lib/active_record/support/class_inheritable_attributes.rb +5 -2
  20. data/lib/active_record/support/inflector.rb +13 -12
  21. data/lib/active_record/support/misc.rb +6 -0
  22. data/lib/active_record/timestamp.rb +33 -0
  23. data/lib/active_record/transactions.rb +1 -1
  24. data/lib/active_record/validations.rb +294 -16
  25. data/rakefile +3 -7
  26. data/test/abstract_unit.rb +1 -4
  27. data/test/associations_test.rb +17 -4
  28. data/test/base_test.rb +37 -5
  29. data/test/connections/native_sqlserver/connection.rb +15 -0
  30. data/test/deprecated_associations_test.rb +40 -38
  31. data/test/finder_test.rb +82 -4
  32. data/test/fixtures/accounts.yml +8 -0
  33. data/test/fixtures/company.rb +6 -0
  34. data/test/fixtures/company_in_module.rb +1 -1
  35. data/test/fixtures/db_definitions/mysql.sql +13 -0
  36. data/test/fixtures/db_definitions/postgresql.sql +13 -0
  37. data/test/fixtures/db_definitions/sqlite.sql +14 -0
  38. data/test/fixtures/db_definitions/sqlserver.sql +110 -0
  39. data/test/fixtures/db_definitions/sqlserver2.sql +4 -0
  40. data/test/fixtures/developer.rb +2 -2
  41. data/test/fixtures/developers.yml +13 -0
  42. data/test/fixtures/fixture_database.sqlite +0 -0
  43. data/test/fixtures/fixture_database_2.sqlite +0 -0
  44. data/test/fixtures/mixin.rb +17 -0
  45. data/test/fixtures/mixins.yml +14 -0
  46. data/test/fixtures/naked/csv/accounts.csv +1 -0
  47. data/test/fixtures/naked/yml/accounts.yml +1 -0
  48. data/test/fixtures/naked/yml/companies.yml +1 -0
  49. data/test/fixtures/naked/yml/courses.yml +1 -0
  50. data/test/fixtures/project.rb +6 -0
  51. data/test/fixtures/reply.rb +14 -1
  52. data/test/fixtures/topic.rb +2 -2
  53. data/test/fixtures/topics/first +1 -0
  54. data/test/fixtures_test.rb +42 -12
  55. data/test/inflector_test.rb +2 -1
  56. data/test/inheritance_test.rb +22 -12
  57. data/test/mixin_test.rb +138 -0
  58. data/test/pk_test.rb +4 -2
  59. data/test/reflection_test.rb +3 -3
  60. data/test/transactions_test.rb +15 -0
  61. data/test/validations_test.rb +229 -4
  62. metadata +24 -10
  63. data/lib/active_record/associations.rb.orig +0 -555
  64. data/test/deprecated_associations_test.rb.orig +0 -334
  65. data/test/fixtures/accounts/signals37 +0 -3
  66. data/test/fixtures/accounts/unknown +0 -2
  67. data/test/fixtures/developers/david +0 -2
  68. data/test/fixtures/developers/jamis +0 -2
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.1
3
3
  specification_version: 1
4
4
  name: activerecord
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
7
- date: 2004-11-18
6
+ version: 1.2.0
7
+ date: 2004-12-16
8
8
  summary: Implements the ActiveRecord pattern for ORM.
9
9
  require_paths:
10
10
  - lib
@@ -33,10 +33,10 @@ files:
33
33
  - CHANGELOG
34
34
  - lib/active_record
35
35
  - lib/active_record.rb
36
+ - lib/active_record/acts
36
37
  - lib/active_record/aggregations.rb
37
38
  - lib/active_record/associations
38
39
  - lib/active_record/associations.rb
39
- - lib/active_record/associations.rb.orig
40
40
  - lib/active_record/base.rb
41
41
  - lib/active_record/callbacks.rb
42
42
  - lib/active_record/connection_adapters
@@ -45,11 +45,14 @@ files:
45
45
  - lib/active_record/observer.rb
46
46
  - lib/active_record/reflection.rb
47
47
  - lib/active_record/support
48
+ - lib/active_record/timestamp.rb
48
49
  - lib/active_record/transactions.rb
49
50
  - lib/active_record/validations.rb
50
51
  - lib/active_record/vendor
51
52
  - lib/active_record/wrappers
52
53
  - lib/active_record/wrappings.rb
54
+ - lib/active_record/acts/list.rb
55
+ - lib/active_record/acts/tree.rb
53
56
  - lib/active_record/associations/association_collection.rb
54
57
  - lib/active_record/associations/has_and_belongs_to_many_association.rb
55
58
  - lib/active_record/associations/has_many_association.rb
@@ -57,10 +60,12 @@ files:
57
60
  - lib/active_record/connection_adapters/mysql_adapter.rb
58
61
  - lib/active_record/connection_adapters/postgresql_adapter.rb
59
62
  - lib/active_record/connection_adapters/sqlite_adapter.rb
63
+ - lib/active_record/connection_adapters/sqlserver_adapter.rb
60
64
  - lib/active_record/support/class_attribute_accessors.rb
61
65
  - lib/active_record/support/class_inheritable_attributes.rb
62
66
  - lib/active_record/support/clean_logger.rb
63
67
  - lib/active_record/support/inflector.rb
68
+ - lib/active_record/support/misc.rb
64
69
  - lib/active_record/vendor/mysql.rb
65
70
  - lib/active_record/vendor/simple.rb
66
71
  - lib/active_record/wrappers/yaml_wrapper.rb
@@ -72,13 +77,13 @@ files:
72
77
  - test/class_inheritable_attributes_test.rb
73
78
  - test/connections
74
79
  - test/deprecated_associations_test.rb
75
- - test/deprecated_associations_test.rb.orig
76
80
  - test/finder_test.rb
77
81
  - test/fixtures
78
82
  - test/fixtures_test.rb
79
83
  - test/inflector_test.rb
80
84
  - test/inheritance_test.rb
81
85
  - test/lifecycle_test.rb
86
+ - test/mixin_test.rb
82
87
  - test/modules_test.rb
83
88
  - test/multiple_db_test.rb
84
89
  - test/pk_test.rb
@@ -90,10 +95,12 @@ files:
90
95
  - test/connections/native_mysql
91
96
  - test/connections/native_postgresql
92
97
  - test/connections/native_sqlite
98
+ - test/connections/native_sqlserver
93
99
  - test/connections/native_mysql/connection.rb
94
100
  - test/connections/native_postgresql/connection.rb
95
101
  - test/connections/native_sqlite/connection.rb
96
- - test/fixtures/accounts
102
+ - test/connections/native_sqlserver/connection.rb
103
+ - test/fixtures/accounts.yml
97
104
  - test/fixtures/auto_id.rb
98
105
  - test/fixtures/bad_fixtures
99
106
  - test/fixtures/column_name.rb
@@ -107,14 +114,17 @@ files:
107
114
  - test/fixtures/db_definitions
108
115
  - test/fixtures/default.rb
109
116
  - test/fixtures/developer.rb
110
- - test/fixtures/developers
117
+ - test/fixtures/developers.yml
111
118
  - test/fixtures/developers_projects
112
119
  - test/fixtures/entrant.rb
113
120
  - test/fixtures/entrants
114
121
  - test/fixtures/fixture_database.sqlite
115
122
  - test/fixtures/fixture_database_2.sqlite
123
+ - test/fixtures/mixin.rb
124
+ - test/fixtures/mixins.yml
116
125
  - test/fixtures/movie.rb
117
126
  - test/fixtures/movies
127
+ - test/fixtures/naked
118
128
  - test/fixtures/project.rb
119
129
  - test/fixtures/projects
120
130
  - test/fixtures/reply.rb
@@ -122,8 +132,6 @@ files:
122
132
  - test/fixtures/subscribers
123
133
  - test/fixtures/topic.rb
124
134
  - test/fixtures/topics
125
- - test/fixtures/accounts/signals37
126
- - test/fixtures/accounts/unknown
127
135
  - test/fixtures/bad_fixtures/attr_with_numeric_first_char
128
136
  - test/fixtures/bad_fixtures/attr_with_spaces
129
137
  - test/fixtures/bad_fixtures/blank_line
@@ -141,8 +149,8 @@ files:
141
149
  - test/fixtures/db_definitions/postgresql2.sql
142
150
  - test/fixtures/db_definitions/sqlite.sql
143
151
  - test/fixtures/db_definitions/sqlite2.sql
144
- - test/fixtures/developers/david
145
- - test/fixtures/developers/jamis
152
+ - test/fixtures/db_definitions/sqlserver.sql
153
+ - test/fixtures/db_definitions/sqlserver2.sql
146
154
  - test/fixtures/developers_projects/david_action_controller
147
155
  - test/fixtures/developers_projects/david_active_record
148
156
  - test/fixtures/developers_projects/jamis_active_record
@@ -151,6 +159,12 @@ files:
151
159
  - test/fixtures/entrants/third
152
160
  - test/fixtures/movies/first
153
161
  - test/fixtures/movies/second
162
+ - test/fixtures/naked/csv
163
+ - test/fixtures/naked/yml
164
+ - test/fixtures/naked/csv/accounts.csv
165
+ - test/fixtures/naked/yml/accounts.yml
166
+ - test/fixtures/naked/yml/companies.yml
167
+ - test/fixtures/naked/yml/courses.yml
154
168
  - test/fixtures/projects/action_controller
155
169
  - test/fixtures/projects/active_record
156
170
  - test/fixtures/subscribers/first
@@ -1,555 +0,0 @@
1
- require 'active_record/associations/association_collection'
2
- require 'active_record/associations/has_many_association'
3
- require 'active_record/associations/has_and_belongs_to_many_association'
4
- require 'active_record/deprecated_associations'
5
-
6
- module ActiveRecord
7
- module Associations # :nodoc:
8
- def self.append_features(base)
9
- super
10
- base.extend(ClassMethods)
11
- end
12
-
13
- # Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like
14
- # "Project has one Project Manager" or "Project belongs to a Portfolio". Each macro adds a number of methods to the class which are
15
- # specialized according to the collection or association symbol and the options hash. It works much the same was as Ruby's own attr*
16
- # methods. Example:
17
- #
18
- # class Project < ActiveRecord::Base
19
- # belongs_to :portfolio
20
- # has_one :project_manager
21
- # has_many :milestones
22
- # has_and_belongs_to_many :categories
23
- # end
24
- #
25
- # The project class now has the following methods (and more) to ease the traversal and manipulation of its relationships:
26
- # * <tt>Project#portfolio, Project#portfolio=(portfolio), Project#portfolio.nil?, Project#portfolio?(portfolio)</tt>
27
- # * <tt>Project#project_manager, Project#project_manager=(project_manager), Project#project_manager.nil?,</tt>
28
- # <tt>Project#project_manager?(project_manager), Project#build_project_manager, Project#create_project_manager</tt>
29
- # * <tt>Project#milestones.empty?, Project#milestones.size, Project#milestones, Project#milestones<<(milestone),</tt>
30
- # <tt>Project#milestones.delete(milestone), Project#milestones.find(milestone_id), Project#milestones.find_all(conditions),</tt>
31
- # <tt>Project#milestones.build, Project#milestones.create</tt>
32
- # * <tt>Project#categories.empty?, Project#categories.size, Project#categories, Project#categories<<(category1),</tt>
33
- # <tt>Project#categories.delete(category1)</tt>
34
- #
35
- # == Example
36
- #
37
- # link:../examples/associations.png
38
- #
39
- # == Is it belongs_to or has_one?
40
- #
41
- # Both express a 1-1 relationship, the difference is mostly where to place the foreign key, which goes on the table for the class
42
- # saying belongs_to. Example:
43
- #
44
- # class Post < ActiveRecord::Base
45
- # has_one :author
46
- # end
47
- #
48
- # class Author < ActiveRecord::Base
49
- # belongs_to :post
50
- # end
51
- #
52
- # The tables for these classes could look something like:
53
- #
54
- # CREATE TABLE posts (
55
- # id int(11) NOT NULL auto_increment,
56
- # title varchar default NULL,
57
- # PRIMARY KEY (id)
58
- # )
59
- #
60
- # CREATE TABLE authors (
61
- # id int(11) NOT NULL auto_increment,
62
- # post_id int(11) default NULL,
63
- # name varchar default NULL,
64
- # PRIMARY KEY (id)
65
- # )
66
- #
67
- # == Caching
68
- #
69
- # All of the methods are built on a simple caching principle that will keep the result of the last query around unless specifically
70
- # instructed not to. The cache is even shared across methods to make it even cheaper to use the macro-added methods without
71
- # worrying too much about performance at the first go. Example:
72
- #
73
- # project.milestones # fetches milestones from the database
74
- # project.milestones.size # uses the milestone cache
75
- # project.milestones.empty? # uses the milestone cache
76
- # project.milestones(true).size # fetches milestones from the database
77
- # project.milestones # uses the milestone cache
78
- #
79
- # == Modules
80
- #
81
- # By default, associations will look for objects within the current module scope. Consider:
82
- #
83
- # module MyApplication
84
- # module Business
85
- # class Firm < ActiveRecord::Base
86
- # has_many :clients
87
- # end
88
- #
89
- # class Company < ActiveRecord::Base; end
90
- # end
91
- # end
92
- #
93
- # When Firm#clients is called, it'll in turn call <tt>MyApplication::Business::Company.find(firm.id)</tt>. If you want to associate
94
- # with a class in another module scope this can be done by specifying the complete class name, such as:
95
- #
96
- # module MyApplication
97
- # module Business
98
- # class Firm < ActiveRecord::Base; end
99
- # end
100
- #
101
- # module Billing
102
- # class Account < ActiveRecord::Base
103
- # belongs_to :firm, :class_name => "MyApplication::Business::Firm"
104
- # end
105
- # end
106
- # end
107
- #
108
- # == Type safety with ActiveRecord::AssociationTypeMismatch
109
- #
110
- # If you attempt to assign an object to an association that doesn't match the inferred or specified <tt>:class_name</tt>, you'll
111
- # get a ActiveRecord::AssociationTypeMismatch.
112
- #
113
- # == Options
114
- #
115
- # All of the association macros can be specialized through options which makes more complex cases than the simple and guessable ones
116
- # possible.
117
- module ClassMethods
118
- # Adds the following methods for retrival and query of collections of associated objects.
119
- # +collection+ is replaced with the symbol passed as the first argument, so
120
- # <tt>has_many :clients</tt> would add among others <tt>has_clients?</tt>.
121
- # * <tt>collection(force_reload = false)</tt> - returns an array of all the associated objects.
122
- # An empty array is returned if none are found.
123
- # * <tt>collection<<(object, ...)</tt> - adds one or more objects to the collection by setting their foreign keys to the collection's primary key.
124
- # * <tt>collection.delete(object, ...)</tt> - removes one or more objects from the collection by setting their foreign keys to NULL. This does not destroy the objects.
125
- # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects.
126
- # * <tt>collection.empty?</tt> - returns true if there are no associated objects.
127
- # * <tt>collection.size</tt> - returns the number of associated objects.
128
- # * <tt>collection.find(id)</tt> - finds an associated object responding to the +id+ and that
129
- # meets the condition that it has to be associated with this object.
130
- # * <tt>collection.find_all(conditions = nil, orderings = nil, limit = nil, joins = nil)</tt> - finds all associated objects responding
131
- # criterias mentioned (like in the standard find_all) and that meets the condition that it has to be associated with this object.
132
- # * <tt>collection.build(attributes = {})</tt> - returns a new object of the collection type that has been instantiated
133
- # with +attributes+ and linked to this object through a foreign key but has not yet been saved.
134
- # * <tt>collection.create(attributes = {})</tt> - returns a new object of the collection type that has been instantiated
135
- # with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation).
136
- #
137
- # Example: A Firm class declares <tt>has_many :clients</tt>, which will add:
138
- # * <tt>Firm#clients</tt> (similar to <tt>Clients.find_all "firm_id = #{id}"</tt>)
139
- # * <tt>Firm#clients<<</tt>
140
- # * <tt>Firm#clients.delete</tt>
141
- # * <tt>Firm#clients.clear</tt>
142
- # * <tt>Firm#clients.empty?</tt> (similar to <tt>firm.clients.size == 0</tt>)
143
- # * <tt>Firm#clients.size</tt> (similar to <tt>Client.count "firm_id = #{id}"</tt>)
144
- # * <tt>Firm#clients.find</tt> (similar to <tt>Client.find_on_conditions(id, "firm_id = #{id}")</tt>)
145
- # * <tt>Firm#clients.find_all</tt> (similar to <tt>Client.find_all "firm_id = #{id}"</tt>)
146
- # * <tt>Firm#clients.build</tt> (similar to <tt>Client.new("firm_id" => id)</tt>)
147
- # * <tt>Firm#clients.create</tt> (similar to <tt>c = Client.new("client_id" => id); c.save; c</tt>)
148
- # The declaration can also include an options hash to specialize the behavior of the association.
149
- #
150
- # Options are:
151
- # * <tt>:class_name</tt> - specify the class name of the association. Use it only if that name can't be infered
152
- # from the association name. So <tt>has_many :products</tt> will by default be linked to the +Product+ class, but
153
- # if the real class name is +SpecialProduct+, you'll have to specify it with this option.
154
- # * <tt>:conditions</tt> - specify the conditions that the associated objects must meet in order to be included as a "WHERE"
155
- # sql fragment, such as "price > 5 AND name LIKE 'B%'".
156
- # * <tt>:order</tt> - specify the order in which the associated objects are returned as a "ORDER BY" sql fragment,
157
- # such as "last_name, first_name DESC"
158
- # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
159
- # of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_many association will use "person_id"
160
- # as the default foreign_key.
161
- # * <tt>:dependent</tt> - if set to true all the associated object are destroyed alongside this object
162
- # * <tt>:exclusively_dependent</tt> - if set to true all the associated object are deleted in one SQL statement without having their
163
- # before_destroy callback run. This should only be used on associations that depend solely on this class and don't need to do any
164
- # clean-up in before_destroy. The upside is that it's much faster, especially if there's a counter_cache involved.
165
- # * <tt>:finder_sql</tt> - specify a complete SQL statement to fetch the association. This is a good way to go for complex
166
- # associations that depends on multiple tables. Note: When this option is used, +find_in_collection+ is _not_ added.
167
- #
168
- # Option examples:
169
- # has_many :comments, :order => "posted_on"
170
- # has_many :people, :class_name => "Person", :conditions => "deleted = 0", :order => "name"
171
- # has_many :tracks, :order => "position", :dependent => true
172
- # has_many :subscribers, :class_name => "Person", :finder_sql =>
173
- # 'SELECT DISTINCT people.* ' +
174
- # 'FROM people p, post_subscriptions ps ' +
175
- # 'WHERE ps.post_id = #{id} AND ps.person_id = p.id ' +
176
- # 'ORDER BY p.first_name'
177
- def has_many(association_id, options = {})
178
- validate_options([ :foreign_key, :class_name, :exclusively_dependent, :dependent, :conditions, :order, :finder_sql ], options.keys)
179
- association_name, association_class_name, association_class_primary_key_name =
180
- associate_identification(association_id, options[:class_name], options[:foreign_key])
181
-
182
- if options[:dependent]
183
- module_eval "before_destroy '#{association_name}.each { |o| o.destroy }'"
184
- end
185
-
186
- if options[:exclusively_dependent]
187
- module_eval "before_destroy Proc.new{ |record| #{association_class_name}.delete_all(%(#{association_class_primary_key_name} = '\#{record.id}')) }"
188
- end
189
-
190
- define_method(association_name) do |*params|
191
- force_reload = params.first unless params.empty?
192
- association = instance_variable_get("@#{association_name}")
193
- if association.nil?
194
- association = HasManyAssociation.new(self,
195
- association_name, association_class_name,
196
- association_class_primary_key_name, options)
197
- instance_variable_set("@#{association_name}", association)
198
- end
199
- association.reload if force_reload
200
- association
201
- end
202
-
203
- # deprecated api
204
- deprecated_collection_count_method(association_name)
205
- deprecated_add_association_relation(association_name)
206
- deprecated_remove_association_relation(association_name)
207
- deprecated_has_collection_method(association_name)
208
- deprecated_find_in_collection_method(association_name)
209
- deprecated_find_all_in_collection_method(association_name)
210
- deprecated_create_method(association_name)
211
- deprecated_build_method(association_name)
212
- end
213
-
214
- # Adds the following methods for retrival and query of a single associated object.
215
- # +association+ is replaced with the symbol passed as the first argument, so
216
- # <tt>has_one :manager</tt> would add among others <tt>has_manager?</tt>.
217
- # * <tt>association(force_reload = false)</tt> - returns the associated object. Nil is returned if none is found.
218
- # * <tt>association=(associate)</tt> - assigns the associate object, extracts the primary key, sets it as the foreign key,
219
- # and saves the associate object.
220
- # * <tt>association?(object, force_reload = false)</tt> - returns true if the +object+ is of the same type and has the
221
- # same id as the associated object.
222
- # * <tt>association.nil?</tt> - returns true if there is no associated object.
223
- # * <tt>build_association(attributes = {})</tt> - returns a new object of the associated type that has been instantiated
224
- # with +attributes+ and linked to this object through a foreign key but has not yet been saved.
225
- # * <tt>create_association(attributes = {})</tt> - returns a new object of the associated type that has been instantiated
226
- # with +attributes+ and linked to this object through a foreign key and that has already been saved (if it passed the validation).
227
- #
228
- # Example: An Account class declares <tt>has_one :beneficiary</tt>, which will add:
229
- # * <tt>Account#beneficiary</tt> (similar to <tt>Beneficiary.find_first "account_id = #{id}"</tt>)
230
- # * <tt>Account#beneficiary=(beneficiary)</tt> (similar to <tt>beneficiary.account_id = account.id; beneficiary.save</tt>)
231
- # * <tt>Account#beneficiary?</tt> (similar to <tt>account.beneficiary == some_beneficiary</tt>)
232
- # * <tt>Account#beneficiary.nil?</tt>
233
- # * <tt>Account#build_beneficiary</tt> (similar to <tt>Beneficiary.new("account_id" => id)</tt>)
234
- # * <tt>Account#create_beneficiary</tt> (similar to <tt>b = Beneficiary.new("account_id" => id); b.save; b</tt>)
235
- # The declaration can also include an options hash to specialize the behavior of the association.
236
- #
237
- # Options are:
238
- # * <tt>:class_name</tt> - specify the class name of the association. Use it only if that name can't be infered
239
- # from the association name. So <tt>has_one :manager</tt> will by default be linked to the +Manager+ class, but
240
- # if the real class name is +Person+, you'll have to specify it with this option.
241
- # * <tt>:conditions</tt> - specify the conditions that the associated object must meet in order to be included as a "WHERE"
242
- # sql fragment, such as "rank = 5".
243
- # * <tt>:order</tt> - specify the order from which the associated object will be picked at the top. Specified as
244
- # an "ORDER BY" sql fragment, such as "last_name, first_name DESC"
245
- # * <tt>:dependent</tt> - if set to true the associated object is destroyed alongside this object
246
- # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
247
- # of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_one association will use "person_id"
248
- # as the default foreign_key.
249
- #
250
- # Option examples:
251
- # has_one :credit_card, :dependent => true
252
- # has_one :last_comment, :class_name => "Comment", :order => "posted_on"
253
- # has_one :project_manager, :class_name => "Person", :conditions => "role = 'project_manager'"
254
- def has_one(association_id, options = {})
255
- options.merge!({ :remote => true })
256
- belongs_to(association_id, options)
257
-
258
- association_name, association_class_name, class_primary_key_name =
259
- associate_identification(association_id, options[:class_name], options[:foreign_key], false)
260
-
261
- has_one_writer_method(association_name, association_class_name, class_primary_key_name)
262
- build_method("build_", association_name, association_class_name, class_primary_key_name)
263
- create_method("create_", association_name, association_class_name, class_primary_key_name)
264
-
265
- module_eval "before_destroy '#{association_name}.destroy if has_#{association_name}?'" if options[:dependent]
266
- end
267
-
268
- # Adds the following methods for retrival and query for a single associated object that this object holds an id to.
269
- # +association+ is replaced with the symbol passed as the first argument, so
270
- # <tt>belongs_to :author</tt> would add among others <tt>has_author?</tt>.
271
- # * <tt>association(force_reload = false)</tt> - returns the associated object. Nil is returned if none is found.
272
- # * <tt>association=(associate)</tt> - assigns the associate object, extracts the primary key, and sets it as the foreign key.
273
- # * <tt>association?(object, force_reload = false)</tt> - returns true if the +object+ is of the same type and has the
274
- # same id as the associated object.
275
- # * <tt>association.nil?</tt> - returns true if there is no associated object.
276
- #
277
- # Example: An Post class declares <tt>has_one :author</tt>, which will add:
278
- # * <tt>Post#author</tt> (similar to <tt>Author.find(author_id)</tt>)
279
- # * <tt>Post#author=(author)</tt> (similar to <tt>post.author_id = author.id</tt>)
280
- # * <tt>Post#author?</tt> (similar to <tt>post.author == some_author</tt>)
281
- # * <tt>Post#author.nil?</tt>
282
- # The declaration can also include an options hash to specialize the behavior of the association.
283
- #
284
- # Options are:
285
- # * <tt>:class_name</tt> - specify the class name of the association. Use it only if that name can't be infered
286
- # from the association name. So <tt>has_one :author</tt> will by default be linked to the +Author+ class, but
287
- # if the real class name is +Person+, you'll have to specify it with this option.
288
- # * <tt>:conditions</tt> - specify the conditions that the associated object must meet in order to be included as a "WHERE"
289
- # sql fragment, such as "authorized = 1".
290
- # * <tt>:order</tt> - specify the order from which the associated object will be picked at the top. Specified as
291
- # an "ORDER BY" sql fragment, such as "last_name, first_name DESC"
292
- # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
293
- # of the associated class in lower-case and "_id" suffixed. So a +Person+ class that makes a belongs_to association to a
294
- # +Boss+ class will use "boss_id" as the default foreign_key.
295
- # * <tt>:counter_cache</tt> - caches the number of belonging objects on the associate class through use of increment_counter
296
- # and decrement_counter. The counter cache is incremented when an object of this class is created and decremented when it's
297
- # destroyed. This requires that a column named "#{table_name}_count" (such as comments_count for a belonging Comment class)
298
- # is used on the associate class (such as a Post class).
299
- #
300
- # Option examples:
301
- # belongs_to :firm, :foreign_key => "client_of"
302
- # belongs_to :author, :class_name => "Person", :foreign_key => "author_id"
303
- # belongs_to :valid_coupon, :class_name => "Coupon", :foreign_key => "coupon_id",
304
- # :conditions => 'discounts > #{payments_count}'
305
- def belongs_to(association_id, options = {})
306
- validate_options([ :class_name, :foreign_key, :remote, :conditions, :order, :dependent, :counter_cache ], options.keys)
307
-
308
- association_name, association_class_name, class_primary_key_name =
309
- associate_identification(association_id, options[:class_name], options[:foreign_key], false)
310
-
311
- association_class_primary_key_name = options[:foreign_key] || Inflector.underscore(Inflector.demodulize(association_class_name)) + "_id"
312
-
313
- if options[:remote]
314
- association_finder = <<-"end_eval"
315
- #{association_class_name}.find_first(
316
- "#{class_primary_key_name} = '\#{id}'#{options[:conditions] ? " AND " + options[:conditions] : ""}",
317
- #{options[:order] ? "\"" + options[:order] + "\"" : "nil" }
318
- )
319
- end_eval
320
- else
321
- association_finder = options[:conditions] ?
322
- "#{association_class_name}.find_on_conditions(#{association_class_primary_key_name}, \"#{options[:conditions]}\")" :
323
- "#{association_class_name}.find(#{association_class_primary_key_name})"
324
- end
325
-
326
- has_association_method(association_name)
327
- association_reader_method(association_name, association_finder)
328
- belongs_to_writer_method(association_name, association_class_name, association_class_primary_key_name)
329
- association_comparison_method(association_name, association_class_name)
330
-
331
- if options[:counter_cache]
332
- module_eval(
333
- "after_create '#{association_class_name}.increment_counter(\"#{Inflector.pluralize(self.to_s.downcase). + "_count"}\", #{association_class_primary_key_name})" +
334
- " if has_#{association_name}?'"
335
- )
336
-
337
- module_eval(
338
- "before_destroy '#{association_class_name}.decrement_counter(\"#{Inflector.pluralize(self.to_s.downcase) + "_count"}\", #{association_class_primary_key_name})" +
339
- " if has_#{association_name}?'"
340
- )
341
- end
342
- end
343
-
344
- # Associates two classes via an intermediate join table. Unless the join table is explicitly specified as
345
- # an option, it is guessed using the lexical order of the class names. So a join between Developer and Project
346
- # will give the default join table name of "developers_projects" because "D" outranks "P".
347
- #
348
- # Any additional fields added to the join table will be placed as attributes when pulling records out through
349
- # has_and_belongs_to_many associations. This is helpful when have information about the association itself
350
- # that you want available on retrival.
351
- #
352
- # Adds the following methods for retrival and query.
353
- # +collection+ is replaced with the symbol passed as the first argument, so
354
- # <tt>has_and_belongs_to_many :categories</tt> would add among others +add_categories+.
355
- # * <tt>collection(force_reload = false)</tt> - returns an array of all the associated objects.
356
- # An empty array is returned if none is found.
357
- # * <tt>collection<<(object, ...)</tt> - adds one or more objects to the collection by creating associations in the join table
358
- # (collection.push and collection.concat are aliases to this method).
359
- # * <tt>collection.push_with_attributes(object, join_attributes)</tt> - adds one to the collection by creating an association in the join table that
360
- # also holds the attributes from <tt>join_attributes</tt> (should be a hash with the column names as keys). This can be used to have additional
361
- # attributes on the join, which will be injected into the associated objects when they are retrieved through the collection.
362
- # (collection.concat_with_attributes is an alias to this method).
363
- # * <tt>collection.delete(object, ...)</tt> - removes one or more objects from the collection by removing their associations from the join table.
364
- # This does not destroy the objects.
365
- # * <tt>collection.clear</tt> - removes every object from the collection. This does not destroy the objects.
366
- # * <tt>collection.empty?</tt> - returns true if there are no associated objects.
367
- # * <tt>collection.size</tt> - returns the number of associated objects.
368
- #
369
- # Example: An Developer class declares <tt>has_and_belongs_to_many :projects</tt>, which will add:
370
- # * <tt>Developer#projects</tt>
371
- # * <tt>Developer#projects<<</tt>
372
- # * <tt>Developer#projects.delete</tt>
373
- # * <tt>Developer#projects.clear</tt>
374
- # * <tt>Developer#projects.empty?</tt>
375
- # * <tt>Developer#projects.size</tt>
376
- # * <tt>Developer#projects.find(id)</tt>
377
- # The declaration may include an options hash to specialize the behavior of the association.
378
- #
379
- # Options are:
380
- # * <tt>:class_name</tt> - specify the class name of the association. Use it only if that name can't be infered
381
- # from the association name. So <tt>has_and_belongs_to_many :projects</tt> will by default be linked to the
382
- # +Project+ class, but if the real class name is +SuperProject+, you'll have to specify it with this option.
383
- # * <tt>:join_table</tt> - specify the name of the join table if the default based on lexical order isn't what you want.
384
- # WARNING: If you're overwriting the table name of either class, the table_name method MUST be declared underneath any
385
- # has_and_belongs_to_many declaration in order to work.
386
- # * <tt>:foreign_key</tt> - specify the foreign key used for the association. By default this is guessed to be the name
387
- # of this class in lower-case and "_id" suffixed. So a +Person+ class that makes a has_and_belongs_to_many association
388
- # will use "person_id" as the default foreign_key.
389
- # * <tt>:association_foreign_key</tt> - specify the association foreign key used for the association. By default this is
390
- # guessed to be the name of the associated class in lower-case and "_id" suffixed. So the associated class is +Project+
391
- # that makes a has_and_belongs_to_many association will use "project_id" as the default association foreign_key.
392
- # * <tt>:conditions</tt> - specify the conditions that the associated object must meet in order to be included as a "WHERE"
393
- # sql fragment, such as "authorized = 1".
394
- # * <tt>:order</tt> - specify the order in which the associated objects are returned as a "ORDER BY" sql fragment, such as "last_name, first_name DESC"
395
- # * <tt>:uniq</tt> - if set to true, duplicate associated objects will be ignored by accessors and query methods
396
- # * <tt>:finder_sql</tt> - overwrite the default generated SQL used to fetch the association with a manual one
397
- # * <tt>:delete_sql</tt> - overwrite the default generated SQL used to remove links between the associated
398
- # classes with a manual one
399
- # * <tt>:insert_sql</tt> - overwrite the default generated SQL used to add links between the associated classes
400
- # with a manual one
401
- #
402
- # Option examples:
403
- # has_and_belongs_to_many :projects
404
- # has_and_belongs_to_many :nations, :class_name => "Country"
405
- # has_and_belongs_to_many :categories, :join_table => "prods_cats"
406
- def has_and_belongs_to_many(association_id, options = {})
407
- validate_options([ :class_name, :table_name, :foreign_key, :association_foreign_key, :conditions,
408
- :join_table, :finder_sql, :delete_sql, :insert_sql, :order, :uniq ], options.keys)
409
- association_name, association_class_name, association_class_primary_key_name =
410
- associate_identification(association_id, options[:class_name], options[:foreign_key])
411
-
412
- join_table = options[:join_table] ||
413
- join_table_name(undecorated_table_name(self.to_s), undecorated_table_name(association_class_name))
414
-
415
-
416
- define_method(association_name) do |*params|
417
- force_reload = params.first unless params.empty?
418
- association = instance_variable_get("@#{association_name}")
419
- if association.nil?
420
- association = HasAndBelongsToManyAssociation.new(self,
421
- association_name, association_class_name,
422
- association_class_primary_key_name, join_table, options)
423
- instance_variable_set("@#{association_name}", association)
424
- end
425
- association.reload if force_reload
426
- association
427
- end
428
-
429
- before_destroy_sql = "DELETE FROM #{join_table} WHERE #{association_class_primary_key_name} = '\\\#{self.id}'"
430
- module_eval(%{before_destroy "self.connection.delete(%{#{before_destroy_sql}})"}) # "
431
-
432
- # deprecated api
433
- deprecated_collection_count_method(association_name)
434
- deprecated_add_association_relation(association_name)
435
- deprecated_remove_association_relation(association_name)
436
- deprecated_has_collection_method(association_name)
437
- end
438
-
439
- private
440
- # Raises an exception if an invalid option has been specified to prevent misspellings from slipping through
441
- def validate_options(valid_option_keys, supplied_option_keys)
442
- unknown_option_keys = supplied_option_keys - valid_option_keys
443
- raise(ActiveRecord::ActiveRecordError, "Unknown options: #{unknown_option_keys}") unless unknown_option_keys.empty?
444
- end
445
-
446
- def join_table_name(first_table_name, second_table_name)
447
- if first_table_name < second_table_name
448
- join_table = "#{first_table_name}_#{second_table_name}"
449
- else
450
- join_table = "#{second_table_name}_#{first_table_name}"
451
- end
452
-
453
- table_name_prefix + join_table + table_name_suffix
454
- end
455
-
456
- def associate_identification(association_id, association_class_name, foreign_key, plural = true)
457
- if association_class_name !~ /::/
458
- association_class_name = type_name_with_module(
459
- association_class_name ||
460
- Inflector.camelize(plural ? Inflector.singularize(association_id.id2name) : association_id.id2name)
461
- )
462
- end
463
-
464
- primary_key_name = foreign_key || Inflector.underscore(Inflector.demodulize(name)) + "_id"
465
-
466
- return association_id.id2name, association_class_name, primary_key_name
467
- end
468
-
469
- def association_comparison_method(association_name, association_class_name)
470
- module_eval <<-"end_eval", __FILE__, __LINE__
471
- def #{association_name}?(comparison_object, force_reload = false)
472
- if comparison_object.kind_of?(#{association_class_name})
473
- #{association_name}(force_reload) == comparison_object
474
- else
475
- raise "Comparison object is a #{association_class_name}, should have been \#{comparison_object.class.name}"
476
- end
477
- end
478
- end_eval
479
- end
480
-
481
- def association_reader_method(association_name, association_finder)
482
- module_eval <<-"end_eval", __FILE__, __LINE__
483
- def #{association_name}(force_reload = false)
484
- if @#{association_name}.nil? || force_reload
485
- begin
486
- @#{association_name} = #{association_finder}
487
- rescue ActiveRecord::StatementInvalid, ActiveRecord::RecordNotFound
488
- nil
489
- end
490
- end
491
-
492
- return @#{association_name}
493
- end
494
- end_eval
495
- end
496
-
497
- def has_one_writer_method(association_name, association_class_name, class_primary_key_name)
498
- module_eval <<-"end_eval", __FILE__, __LINE__
499
- def #{association_name}=(association)
500
- if association.nil?
501
- @#{association_name}.#{class_primary_key_name} = nil
502
- @#{association_name}.save(false)
503
- @#{association_name} = nil
504
- else
505
- raise ActiveRecord::AssociationTypeMismatch unless #{association_class_name} === association
506
- association.#{class_primary_key_name} = id
507
- association.save(false)
508
- @#{association_name} = association
509
- end
510
- end
511
- end_eval
512
- end
513
-
514
- def belongs_to_writer_method(association_name, association_class_name, association_class_primary_key_name)
515
- module_eval <<-"end_eval", __FILE__, __LINE__
516
- def #{association_name}=(association)
517
- if association.nil?
518
- @#{association_name} = self.#{association_class_primary_key_name} = nil
519
- else
520
- raise ActiveRecord::AssociationTypeMismatch unless #{association_class_name} === association
521
- @#{association_name} = association
522
- self.#{association_class_primary_key_name} = association.id
523
- end
524
- end
525
- end_eval
526
- end
527
-
528
- def has_association_method(association_name)
529
- module_eval <<-"end_eval", __FILE__, __LINE__
530
- def has_#{association_name}?(force_reload = false)
531
- !#{association_name}(force_reload).nil?
532
- end
533
- end_eval
534
- end
535
-
536
- def build_method(method_prefix, collection_name, collection_class_name, class_primary_key_name)
537
- module_eval <<-"end_eval", __FILE__, __LINE__
538
- def #{method_prefix + collection_name}(attributes = {})
539
- association = #{collection_class_name}.new
540
- association.attributes = attributes.merge({ "#{class_primary_key_name}" => id})
541
- association
542
- end
543
- end_eval
544
- end
545
-
546
- def create_method(method_prefix, collection_name, collection_class_name, class_primary_key_name)
547
- module_eval <<-"end_eval", __FILE__, __LINE__
548
- def #{method_prefix + collection_name}(attributes = nil)
549
- #{collection_class_name}.create((attributes || {}).merge({ "#{class_primary_key_name}" => id}))
550
- end
551
- end_eval
552
- end
553
- end
554
- end
555
- end