activerecord 3.0.0 → 4.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 (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +2102 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +35 -44
  5. data/examples/performance.rb +110 -100
  6. data/lib/active_record/aggregations.rb +59 -75
  7. data/lib/active_record/associations/alias_tracker.rb +76 -0
  8. data/lib/active_record/associations/association.rb +248 -0
  9. data/lib/active_record/associations/association_scope.rb +135 -0
  10. data/lib/active_record/associations/belongs_to_association.rb +60 -59
  11. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +16 -59
  12. data/lib/active_record/associations/builder/association.rb +108 -0
  13. data/lib/active_record/associations/builder/belongs_to.rb +98 -0
  14. data/lib/active_record/associations/builder/collection_association.rb +89 -0
  15. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +39 -0
  16. data/lib/active_record/associations/builder/has_many.rb +15 -0
  17. data/lib/active_record/associations/builder/has_one.rb +25 -0
  18. data/lib/active_record/associations/builder/singular_association.rb +32 -0
  19. data/lib/active_record/associations/collection_association.rb +608 -0
  20. data/lib/active_record/associations/collection_proxy.rb +986 -0
  21. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +40 -112
  22. data/lib/active_record/associations/has_many_association.rb +83 -76
  23. data/lib/active_record/associations/has_many_through_association.rb +147 -66
  24. data/lib/active_record/associations/has_one_association.rb +67 -108
  25. data/lib/active_record/associations/has_one_through_association.rb +21 -25
  26. data/lib/active_record/associations/join_dependency/join_association.rb +174 -0
  27. data/lib/active_record/associations/join_dependency/join_base.rb +24 -0
  28. data/lib/active_record/associations/join_dependency/join_part.rb +78 -0
  29. data/lib/active_record/associations/join_dependency.rb +235 -0
  30. data/lib/active_record/associations/join_helper.rb +45 -0
  31. data/lib/active_record/associations/preloader/association.rb +121 -0
  32. data/lib/active_record/associations/preloader/belongs_to.rb +17 -0
  33. data/lib/active_record/associations/preloader/collection_association.rb +24 -0
  34. data/lib/active_record/associations/preloader/has_and_belongs_to_many.rb +60 -0
  35. data/lib/active_record/associations/preloader/has_many.rb +17 -0
  36. data/lib/active_record/associations/preloader/has_many_through.rb +19 -0
  37. data/lib/active_record/associations/preloader/has_one.rb +23 -0
  38. data/lib/active_record/associations/preloader/has_one_through.rb +9 -0
  39. data/lib/active_record/associations/preloader/singular_association.rb +21 -0
  40. data/lib/active_record/associations/preloader/through_association.rb +63 -0
  41. data/lib/active_record/associations/preloader.rb +178 -0
  42. data/lib/active_record/associations/singular_association.rb +64 -0
  43. data/lib/active_record/associations/through_association.rb +87 -0
  44. data/lib/active_record/associations.rb +512 -1224
  45. data/lib/active_record/attribute_assignment.rb +201 -0
  46. data/lib/active_record/attribute_methods/before_type_cast.rb +49 -12
  47. data/lib/active_record/attribute_methods/dirty.rb +51 -28
  48. data/lib/active_record/attribute_methods/primary_key.rb +94 -22
  49. data/lib/active_record/attribute_methods/query.rb +5 -4
  50. data/lib/active_record/attribute_methods/read.rb +63 -72
  51. data/lib/active_record/attribute_methods/serialization.rb +162 -0
  52. data/lib/active_record/attribute_methods/time_zone_conversion.rb +39 -41
  53. data/lib/active_record/attribute_methods/write.rb +39 -13
  54. data/lib/active_record/attribute_methods.rb +362 -29
  55. data/lib/active_record/autosave_association.rb +132 -75
  56. data/lib/active_record/base.rb +83 -1627
  57. data/lib/active_record/callbacks.rb +69 -47
  58. data/lib/active_record/coders/yaml_column.rb +38 -0
  59. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +411 -138
  60. data/lib/active_record/connection_adapters/abstract/database_limits.rb +21 -11
  61. data/lib/active_record/connection_adapters/abstract/database_statements.rb +234 -173
  62. data/lib/active_record/connection_adapters/abstract/query_cache.rb +36 -22
  63. data/lib/active_record/connection_adapters/abstract/quoting.rb +82 -25
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +176 -414
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +70 -0
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +562 -232
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +203 -0
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +281 -53
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +782 -0
  70. data/lib/active_record/connection_adapters/column.rb +318 -0
  71. data/lib/active_record/connection_adapters/connection_specification.rb +96 -0
  72. data/lib/active_record/connection_adapters/mysql2_adapter.rb +273 -0
  73. data/lib/active_record/connection_adapters/mysql_adapter.rb +365 -450
  74. data/lib/active_record/connection_adapters/postgresql/array_parser.rb +97 -0
  75. data/lib/active_record/connection_adapters/postgresql/cast.rb +152 -0
  76. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +242 -0
  77. data/lib/active_record/connection_adapters/postgresql/oid.rb +366 -0
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +171 -0
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +30 -0
  80. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +489 -0
  81. data/lib/active_record/connection_adapters/postgresql_adapter.rb +672 -752
  82. data/lib/active_record/connection_adapters/schema_cache.rb +129 -0
  83. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +588 -17
  84. data/lib/active_record/connection_adapters/statement_pool.rb +40 -0
  85. data/lib/active_record/connection_handling.rb +98 -0
  86. data/lib/active_record/core.rb +463 -0
  87. data/lib/active_record/counter_cache.rb +108 -101
  88. data/lib/active_record/dynamic_matchers.rb +131 -0
  89. data/lib/active_record/errors.rb +54 -13
  90. data/lib/active_record/explain.rb +38 -0
  91. data/lib/active_record/explain_registry.rb +30 -0
  92. data/lib/active_record/explain_subscriber.rb +29 -0
  93. data/lib/active_record/fixture_set/file.rb +55 -0
  94. data/lib/active_record/fixtures.rb +703 -785
  95. data/lib/active_record/inheritance.rb +200 -0
  96. data/lib/active_record/integration.rb +60 -0
  97. data/lib/active_record/locale/en.yml +8 -1
  98. data/lib/active_record/locking/optimistic.rb +69 -60
  99. data/lib/active_record/locking/pessimistic.rb +34 -12
  100. data/lib/active_record/log_subscriber.rb +40 -6
  101. data/lib/active_record/migration/command_recorder.rb +164 -0
  102. data/lib/active_record/migration/join_table.rb +15 -0
  103. data/lib/active_record/migration.rb +614 -216
  104. data/lib/active_record/model_schema.rb +345 -0
  105. data/lib/active_record/nested_attributes.rb +248 -119
  106. data/lib/active_record/null_relation.rb +65 -0
  107. data/lib/active_record/persistence.rb +275 -57
  108. data/lib/active_record/query_cache.rb +29 -9
  109. data/lib/active_record/querying.rb +62 -0
  110. data/lib/active_record/railtie.rb +135 -21
  111. data/lib/active_record/railties/console_sandbox.rb +5 -0
  112. data/lib/active_record/railties/controller_runtime.rb +17 -5
  113. data/lib/active_record/railties/databases.rake +249 -359
  114. data/lib/active_record/railties/jdbcmysql_error.rb +16 -0
  115. data/lib/active_record/readonly_attributes.rb +30 -0
  116. data/lib/active_record/reflection.rb +283 -103
  117. data/lib/active_record/relation/batches.rb +38 -34
  118. data/lib/active_record/relation/calculations.rb +252 -139
  119. data/lib/active_record/relation/delegation.rb +125 -0
  120. data/lib/active_record/relation/finder_methods.rb +182 -188
  121. data/lib/active_record/relation/merger.rb +161 -0
  122. data/lib/active_record/relation/predicate_builder.rb +86 -21
  123. data/lib/active_record/relation/query_methods.rb +917 -134
  124. data/lib/active_record/relation/spawn_methods.rb +53 -92
  125. data/lib/active_record/relation.rb +405 -143
  126. data/lib/active_record/result.rb +67 -0
  127. data/lib/active_record/runtime_registry.rb +17 -0
  128. data/lib/active_record/sanitization.rb +168 -0
  129. data/lib/active_record/schema.rb +20 -14
  130. data/lib/active_record/schema_dumper.rb +55 -46
  131. data/lib/active_record/schema_migration.rb +39 -0
  132. data/lib/active_record/scoping/default.rb +146 -0
  133. data/lib/active_record/scoping/named.rb +175 -0
  134. data/lib/active_record/scoping.rb +82 -0
  135. data/lib/active_record/serialization.rb +8 -46
  136. data/lib/active_record/serializers/xml_serializer.rb +21 -68
  137. data/lib/active_record/statement_cache.rb +26 -0
  138. data/lib/active_record/store.rb +156 -0
  139. data/lib/active_record/tasks/database_tasks.rb +203 -0
  140. data/lib/active_record/tasks/firebird_database_tasks.rb +56 -0
  141. data/lib/active_record/tasks/mysql_database_tasks.rb +143 -0
  142. data/lib/active_record/tasks/oracle_database_tasks.rb +45 -0
  143. data/lib/active_record/tasks/postgresql_database_tasks.rb +90 -0
  144. data/lib/active_record/tasks/sqlite_database_tasks.rb +51 -0
  145. data/lib/active_record/tasks/sqlserver_database_tasks.rb +48 -0
  146. data/lib/active_record/test_case.rb +57 -28
  147. data/lib/active_record/timestamp.rb +49 -18
  148. data/lib/active_record/transactions.rb +106 -63
  149. data/lib/active_record/translation.rb +22 -0
  150. data/lib/active_record/validations/associated.rb +25 -24
  151. data/lib/active_record/validations/presence.rb +65 -0
  152. data/lib/active_record/validations/uniqueness.rb +123 -83
  153. data/lib/active_record/validations.rb +29 -29
  154. data/lib/active_record/version.rb +7 -5
  155. data/lib/active_record.rb +83 -34
  156. data/lib/rails/generators/active_record/migration/migration_generator.rb +46 -9
  157. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb +19 -0
  158. data/lib/rails/generators/active_record/migration/templates/migration.rb +30 -8
  159. data/lib/rails/generators/active_record/model/model_generator.rb +15 -5
  160. data/lib/rails/generators/active_record/model/templates/model.rb +7 -2
  161. data/lib/rails/generators/active_record/model/templates/module.rb +3 -1
  162. data/lib/rails/generators/active_record.rb +4 -8
  163. metadata +163 -121
  164. data/CHANGELOG +0 -6023
  165. data/examples/associations.png +0 -0
  166. data/lib/active_record/association_preload.rb +0 -403
  167. data/lib/active_record/associations/association_collection.rb +0 -562
  168. data/lib/active_record/associations/association_proxy.rb +0 -295
  169. data/lib/active_record/associations/through_association_scope.rb +0 -154
  170. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +0 -113
  171. data/lib/active_record/connection_adapters/sqlite_adapter.rb +0 -401
  172. data/lib/active_record/dynamic_finder_match.rb +0 -53
  173. data/lib/active_record/dynamic_scope_match.rb +0 -32
  174. data/lib/active_record/named_scope.rb +0 -138
  175. data/lib/active_record/observer.rb +0 -140
  176. data/lib/active_record/session_store.rb +0 -340
  177. data/lib/rails/generators/active_record/model/templates/migration.rb +0 -16
  178. data/lib/rails/generators/active_record/observer/observer_generator.rb +0 -15
  179. data/lib/rails/generators/active_record/observer/templates/observer.rb +0 -2
  180. data/lib/rails/generators/active_record/session_migration/session_migration_generator.rb +0 -24
  181. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +0 -16
@@ -2,59 +2,392 @@ require 'active_support/core_ext/enumerable'
2
2
 
3
3
  module ActiveRecord
4
4
  # = Active Record Attribute Methods
5
- module AttributeMethods #:nodoc:
5
+ module AttributeMethods
6
6
  extend ActiveSupport::Concern
7
7
  include ActiveModel::AttributeMethods
8
8
 
9
+ included do
10
+ include Read
11
+ include Write
12
+ include BeforeTypeCast
13
+ include Query
14
+ include PrimaryKey
15
+ include TimeZoneConversion
16
+ include Dirty
17
+ include Serialization
18
+ end
19
+
9
20
  module ClassMethods
10
21
  # Generates all the attribute related methods for columns in the database
11
22
  # accessors, mutators and query methods.
12
- def define_attribute_methods
13
- super(columns_hash.keys)
23
+ def define_attribute_methods # :nodoc:
24
+ # Use a mutex; we don't want two thread simultaneously trying to define
25
+ # attribute methods.
26
+ @attribute_methods_mutex.synchronize do
27
+ return if attribute_methods_generated?
28
+ superclass.define_attribute_methods unless self == base_class
29
+ super(column_names)
30
+ @attribute_methods_generated = true
31
+ end
14
32
  end
15
33
 
16
- # Checks whether the method is defined in the model or any of its subclasses
17
- # that also derive from Active Record. Raises DangerousAttributeError if the
18
- # method is defined by Active Record though.
34
+ def attribute_methods_generated? # :nodoc:
35
+ @attribute_methods_generated ||= false
36
+ end
37
+
38
+ def undefine_attribute_methods # :nodoc:
39
+ super if attribute_methods_generated?
40
+ @attribute_methods_generated = false
41
+ end
42
+
43
+ # Raises a <tt>ActiveRecord::DangerousAttributeError</tt> exception when an
44
+ # \Active \Record method is defined in the model, otherwise +false+.
45
+ #
46
+ # class Person < ActiveRecord::Base
47
+ # def save
48
+ # 'already defined by Active Record'
49
+ # end
50
+ # end
51
+ #
52
+ # Person.instance_method_already_implemented?(:save)
53
+ # # => ActiveRecord::DangerousAttributeError: save is defined by ActiveRecord
54
+ #
55
+ # Person.instance_method_already_implemented?(:name)
56
+ # # => false
19
57
  def instance_method_already_implemented?(method_name)
20
- method_name = method_name.to_s
21
- @_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.map {|m| m.to_s }.to_set
22
- @@_defined_activerecord_methods ||= defined_activerecord_methods
23
- raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
24
- @_defined_class_methods.include?(method_name)
58
+ if dangerous_attribute_method?(method_name)
59
+ raise DangerousAttributeError, "#{method_name} is defined by Active Record"
60
+ end
61
+
62
+ if superclass == Base
63
+ super
64
+ else
65
+ # If B < A and A defines its own attribute method, then we don't want to overwrite that.
66
+ defined = method_defined_within?(method_name, superclass, superclass.generated_attribute_methods)
67
+ defined && !ActiveRecord::Base.method_defined?(method_name) || super
68
+ end
69
+ end
70
+
71
+ # A method name is 'dangerous' if it is already defined by Active Record, but
72
+ # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
73
+ def dangerous_attribute_method?(name) # :nodoc:
74
+ method_defined_within?(name, Base)
25
75
  end
26
76
 
27
- def defined_activerecord_methods
28
- active_record = ActiveRecord::Base
29
- super_klass = ActiveRecord::Base.superclass
30
- methods = active_record.public_instance_methods - super_klass.public_instance_methods
31
- methods += active_record.private_instance_methods - super_klass.private_instance_methods
32
- methods += active_record.protected_instance_methods - super_klass.protected_instance_methods
33
- methods.map {|m| m.to_s }.to_set
77
+ def method_defined_within?(name, klass, sup = klass.superclass) # :nodoc:
78
+ if klass.method_defined?(name) || klass.private_method_defined?(name)
79
+ if sup.method_defined?(name) || sup.private_method_defined?(name)
80
+ klass.instance_method(name).owner != sup.instance_method(name).owner
81
+ else
82
+ true
83
+ end
84
+ else
85
+ false
86
+ end
87
+ end
88
+
89
+ # Returns +true+ if +attribute+ is an attribute method and table exists,
90
+ # +false+ otherwise.
91
+ #
92
+ # class Person < ActiveRecord::Base
93
+ # end
94
+ #
95
+ # Person.attribute_method?('name') # => true
96
+ # Person.attribute_method?(:age=) # => true
97
+ # Person.attribute_method?(:nothing) # => false
98
+ def attribute_method?(attribute)
99
+ super || (table_exists? && column_names.include?(attribute.to_s.sub(/=$/, '')))
100
+ end
101
+
102
+ # Returns an array of column names as strings if it's not an abstract class and
103
+ # table exists. Otherwise it returns an empty array.
104
+ #
105
+ # class Person < ActiveRecord::Base
106
+ # end
107
+ #
108
+ # Person.attribute_names
109
+ # # => ["id", "created_at", "updated_at", "name", "age"]
110
+ def attribute_names
111
+ @attribute_names ||= if !abstract_class? && table_exists?
112
+ column_names
113
+ else
114
+ []
115
+ end
34
116
  end
35
117
  end
36
118
 
37
- def method_missing(method_id, *args, &block)
38
- # If we haven't generated any methods yet, generate them, then
39
- # see if we've created the method we're looking for.
40
- if !self.class.attribute_methods_generated?
119
+ # If we haven't generated any methods yet, generate them, then
120
+ # see if we've created the method we're looking for.
121
+ def method_missing(method, *args, &block) # :nodoc:
122
+ unless self.class.attribute_methods_generated?
41
123
  self.class.define_attribute_methods
42
- method_name = method_id.to_s
43
- guard_private_attribute_method!(method_name, args)
44
- send(method_id, *args, &block)
124
+
125
+ if respond_to_without_attributes?(method)
126
+ send(method, *args, &block)
127
+ else
128
+ super
129
+ end
45
130
  else
46
131
  super
47
132
  end
48
133
  end
49
134
 
50
- def respond_to?(*args)
51
- self.class.define_attribute_methods
135
+ def attribute_missing(match, *args, &block) # :nodoc:
136
+ if self.class.columns_hash[match.attr_name]
137
+ ActiveSupport::Deprecation.warn(
138
+ "The method `#{match.method_name}', matching the attribute `#{match.attr_name}' has " \
139
+ "dispatched through method_missing. This shouldn't happen, because `#{match.attr_name}' " \
140
+ "is a column of the table. If this error has happened through normal usage of Active " \
141
+ "Record (rather than through your own code or external libraries), please report it as " \
142
+ "a bug."
143
+ )
144
+ end
145
+
52
146
  super
53
147
  end
54
148
 
149
+ # A Person object with a name attribute can ask <tt>person.respond_to?(:name)</tt>,
150
+ # <tt>person.respond_to?(:name=)</tt>, and <tt>person.respond_to?(:name?)</tt>
151
+ # which will all return +true+. It also define the attribute methods if they have
152
+ # not been generated.
153
+ #
154
+ # class Person < ActiveRecord::Base
155
+ # end
156
+ #
157
+ # person = Person.new
158
+ # person.respond_to(:name) # => true
159
+ # person.respond_to(:name=) # => true
160
+ # person.respond_to(:name?) # => true
161
+ # person.respond_to('age') # => true
162
+ # person.respond_to('age=') # => true
163
+ # person.respond_to('age?') # => true
164
+ # person.respond_to(:nothing) # => false
165
+ def respond_to?(name, include_private = false)
166
+ name = name.to_s
167
+ self.class.define_attribute_methods unless self.class.attribute_methods_generated?
168
+ result = super
169
+
170
+ # If the result is false the answer is false.
171
+ return false unless result
172
+
173
+ # If the result is true then check for the select case.
174
+ # For queries selecting a subset of columns, return false for unselected columns.
175
+ # We check defined?(@attributes) not to issue warnings if called on objects that
176
+ # have been allocated but not yet initialized.
177
+ if defined?(@attributes) && @attributes.present? && self.class.column_names.include?(name)
178
+ return has_attribute?(name)
179
+ end
180
+
181
+ return true
182
+ end
183
+
184
+ # Returns +true+ if the given attribute is in the attributes hash, otherwise +false+.
185
+ #
186
+ # class Person < ActiveRecord::Base
187
+ # end
188
+ #
189
+ # person = Person.new
190
+ # person.has_attribute?(:name) # => true
191
+ # person.has_attribute?('age') # => true
192
+ # person.has_attribute?(:nothing) # => false
193
+ def has_attribute?(attr_name)
194
+ @attributes.has_key?(attr_name.to_s)
195
+ end
196
+
197
+ # Returns an array of names for the attributes available on this object.
198
+ #
199
+ # class Person < ActiveRecord::Base
200
+ # end
201
+ #
202
+ # person = Person.new
203
+ # person.attribute_names
204
+ # # => ["id", "created_at", "updated_at", "name", "age"]
205
+ def attribute_names
206
+ @attributes.keys
207
+ end
208
+
209
+ # Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
210
+ #
211
+ # class Person < ActiveRecord::Base
212
+ # end
213
+ #
214
+ # person = Person.create(name: 'Francesco', age: 22)
215
+ # person.attributes
216
+ # # => {"id"=>3, "created_at"=>Sun, 21 Oct 2012 04:53:04, "updated_at"=>Sun, 21 Oct 2012 04:53:04, "name"=>"Francesco", "age"=>22}
217
+ def attributes
218
+ attribute_names.each_with_object({}) { |name, attrs|
219
+ attrs[name] = read_attribute(name)
220
+ }
221
+ end
222
+
223
+ # Returns an <tt>#inspect</tt>-like string for the value of the
224
+ # attribute +attr_name+. String attributes are truncated upto 50
225
+ # characters, and Date and Time attributes are returned in the
226
+ # <tt>:db</tt> format. Other attributes return the value of
227
+ # <tt>#inspect</tt> without modification.
228
+ #
229
+ # person = Person.create!(name: 'David Heinemeier Hansson ' * 3)
230
+ #
231
+ # person.attribute_for_inspect(:name)
232
+ # # => "\"David Heinemeier Hansson David Heinemeier Hansson D...\""
233
+ #
234
+ # person.attribute_for_inspect(:created_at)
235
+ # # => "\"2012-10-22 00:15:07\""
236
+ def attribute_for_inspect(attr_name)
237
+ value = read_attribute(attr_name)
238
+
239
+ if value.is_a?(String) && value.length > 50
240
+ "#{value[0..50]}...".inspect
241
+ elsif value.is_a?(Date) || value.is_a?(Time)
242
+ %("#{value.to_s(:db)}")
243
+ else
244
+ value.inspect
245
+ end
246
+ end
247
+
248
+ # Returns +true+ if the specified +attribute+ has been set by the user or by a
249
+ # database load and is neither +nil+ nor <tt>empty?</tt> (the latter only applies
250
+ # to objects that respond to <tt>empty?</tt>, most notably Strings). Otherwise, +false+.
251
+ # Note that it always returns +true+ with boolean attributes.
252
+ #
253
+ # class Task < ActiveRecord::Base
254
+ # end
255
+ #
256
+ # person = Task.new(title: '', is_done: false)
257
+ # person.attribute_present?(:title) # => false
258
+ # person.attribute_present?(:is_done) # => true
259
+ # person.name = 'Francesco'
260
+ # person.is_done = true
261
+ # person.attribute_present?(:title) # => true
262
+ # person.attribute_present?(:is_done) # => true
263
+ def attribute_present?(attribute)
264
+ value = read_attribute(attribute)
265
+ !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
266
+ end
267
+
268
+ # Returns the column object for the named attribute. Returns +nil+ if the
269
+ # named attribute not exists.
270
+ #
271
+ # class Person < ActiveRecord::Base
272
+ # end
273
+ #
274
+ # person = Person.new
275
+ # person.column_for_attribute(:name) # the result depends on the ConnectionAdapter
276
+ # # => #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...>
277
+ #
278
+ # person.column_for_attribute(:nothing)
279
+ # # => nil
280
+ def column_for_attribute(name)
281
+ # FIXME: should this return a null object for columns that don't exist?
282
+ self.class.columns_hash[name.to_s]
283
+ end
284
+
285
+ # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
286
+ # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)). It raises
287
+ # <tt>ActiveModel::MissingAttributeError</tt> if the identified attribute is missing.
288
+ #
289
+ # Alias for the <tt>read_attribute</tt> method.
290
+ #
291
+ # class Person < ActiveRecord::Base
292
+ # belongs_to :organization
293
+ # end
294
+ #
295
+ # person = Person.new(name: 'Francesco', age: '22')
296
+ # person[:name] # => "Francesco"
297
+ # person[:age] # => 22
298
+ #
299
+ # person = Person.select('id').first
300
+ # person[:name] # => ActiveModel::MissingAttributeError: missing attribute: name
301
+ # person[:organization_id] # => ActiveModel::MissingAttributeError: missing attribute: organization_id
302
+ def [](attr_name)
303
+ read_attribute(attr_name) { |n| missing_attribute(n, caller) }
304
+ end
305
+
306
+ # Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
307
+ # (Alias for the protected <tt>write_attribute</tt> method).
308
+ #
309
+ # class Person < ActiveRecord::Base
310
+ # end
311
+ #
312
+ # person = Person.new
313
+ # person[:age] = '22'
314
+ # person[:age] # => 22
315
+ # person[:age] # => Fixnum
316
+ def []=(attr_name, value)
317
+ write_attribute(attr_name, value)
318
+ end
319
+
55
320
  protected
56
- def attribute_method?(attr_name)
57
- attr_name == 'id' || attributes.include?(attr_name)
321
+
322
+ def clone_attributes(reader_method = :read_attribute, attributes = {}) # :nodoc:
323
+ attribute_names.each do |name|
324
+ attributes[name] = clone_attribute_value(reader_method, name)
325
+ end
326
+ attributes
327
+ end
328
+
329
+ def clone_attribute_value(reader_method, attribute_name) # :nodoc:
330
+ value = send(reader_method, attribute_name)
331
+ value.duplicable? ? value.clone : value
332
+ rescue TypeError, NoMethodError
333
+ value
334
+ end
335
+
336
+ def arel_attributes_with_values_for_create(attribute_names) # :nodoc:
337
+ arel_attributes_with_values(attributes_for_create(attribute_names))
338
+ end
339
+
340
+ def arel_attributes_with_values_for_update(attribute_names) # :nodoc:
341
+ arel_attributes_with_values(attributes_for_update(attribute_names))
342
+ end
343
+
344
+ def attribute_method?(attr_name) # :nodoc:
345
+ # We check defined? because Syck calls respond_to? before actually calling initialize.
346
+ defined?(@attributes) && @attributes.include?(attr_name)
347
+ end
348
+
349
+ private
350
+
351
+ # Returns a Hash of the Arel::Attributes and attribute values that have been
352
+ # typecasted for use in an Arel insert/update method.
353
+ def arel_attributes_with_values(attribute_names)
354
+ attrs = {}
355
+ arel_table = self.class.arel_table
356
+
357
+ attribute_names.each do |name|
358
+ attrs[arel_table[name]] = typecasted_attribute_value(name)
58
359
  end
360
+ attrs
361
+ end
362
+
363
+ # Filters the primary keys and readonly attributes from the attribute names.
364
+ def attributes_for_update(attribute_names)
365
+ attribute_names.select do |name|
366
+ column_for_attribute(name) && !readonly_attribute?(name)
367
+ end
368
+ end
369
+
370
+ # Filters out the primary keys, from the attribute names, when the primary
371
+ # key is to be generated (e.g. the id attribute has no value).
372
+ def attributes_for_create(attribute_names)
373
+ attribute_names.select do |name|
374
+ column_for_attribute(name) && !(pk_attribute?(name) && id.nil?)
375
+ end
376
+ end
377
+
378
+ def readonly_attribute?(name)
379
+ self.class.readonly_attributes.include?(name)
380
+ end
381
+
382
+ def pk_attribute?(name)
383
+ column_for_attribute(name).primary
384
+ end
385
+
386
+ def typecasted_attribute_value(name)
387
+ # FIXME: we need @attributes to be used consistently.
388
+ # If the values stored in @attributes were already typecasted, this code
389
+ # could be simplified
390
+ read_attribute(name)
391
+ end
59
392
  end
60
393
  end