activerecord 3.1.11 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. data/CHANGELOG.md +6294 -97
  2. data/README.rdoc +2 -2
  3. data/examples/performance.rb +55 -31
  4. data/lib/active_record/aggregations.rb +2 -2
  5. data/lib/active_record/associations/association.rb +2 -42
  6. data/lib/active_record/associations/association_scope.rb +3 -30
  7. data/lib/active_record/associations/builder/association.rb +6 -4
  8. data/lib/active_record/associations/builder/belongs_to.rb +3 -3
  9. data/lib/active_record/associations/builder/collection_association.rb +2 -2
  10. data/lib/active_record/associations/builder/has_many.rb +4 -4
  11. data/lib/active_record/associations/builder/has_one.rb +5 -6
  12. data/lib/active_record/associations/builder/singular_association.rb +3 -16
  13. data/lib/active_record/associations/collection_association.rb +55 -28
  14. data/lib/active_record/associations/collection_proxy.rb +1 -35
  15. data/lib/active_record/associations/has_many_association.rb +5 -1
  16. data/lib/active_record/associations/has_many_through_association.rb +11 -8
  17. data/lib/active_record/associations/join_dependency.rb +1 -1
  18. data/lib/active_record/associations/preloader/association.rb +3 -1
  19. data/lib/active_record/associations.rb +82 -69
  20. data/lib/active_record/attribute_assignment.rb +221 -0
  21. data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
  22. data/lib/active_record/attribute_methods/dirty.rb +3 -3
  23. data/lib/active_record/attribute_methods/primary_key.rb +62 -25
  24. data/lib/active_record/attribute_methods/read.rb +72 -83
  25. data/lib/active_record/attribute_methods/serialization.rb +93 -0
  26. data/lib/active_record/attribute_methods/time_zone_conversion.rb +9 -14
  27. data/lib/active_record/attribute_methods/write.rb +27 -5
  28. data/lib/active_record/attribute_methods.rb +209 -30
  29. data/lib/active_record/autosave_association.rb +23 -8
  30. data/lib/active_record/base.rb +217 -1709
  31. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +98 -132
  32. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +82 -29
  33. data/lib/active_record/connection_adapters/abstract/database_statements.rb +13 -42
  34. data/lib/active_record/connection_adapters/abstract/query_cache.rb +1 -1
  35. data/lib/active_record/connection_adapters/abstract/quoting.rb +9 -12
  36. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +36 -25
  37. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +43 -22
  38. data/lib/active_record/connection_adapters/abstract_adapter.rb +78 -43
  39. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +653 -0
  40. data/lib/active_record/connection_adapters/column.rb +2 -2
  41. data/lib/active_record/connection_adapters/mysql2_adapter.rb +138 -578
  42. data/lib/active_record/connection_adapters/mysql_adapter.rb +86 -658
  43. data/lib/active_record/connection_adapters/postgresql_adapter.rb +144 -94
  44. data/lib/active_record/connection_adapters/schema_cache.rb +50 -0
  45. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +2 -6
  46. data/lib/active_record/connection_adapters/sqlite_adapter.rb +43 -22
  47. data/lib/active_record/counter_cache.rb +4 -3
  48. data/lib/active_record/dynamic_matchers.rb +79 -0
  49. data/lib/active_record/errors.rb +11 -1
  50. data/lib/active_record/explain.rb +83 -0
  51. data/lib/active_record/explain_subscriber.rb +21 -0
  52. data/lib/active_record/fixtures/file.rb +65 -0
  53. data/lib/active_record/fixtures.rb +31 -76
  54. data/lib/active_record/identity_map.rb +4 -11
  55. data/lib/active_record/inheritance.rb +167 -0
  56. data/lib/active_record/integration.rb +49 -0
  57. data/lib/active_record/locking/optimistic.rb +30 -25
  58. data/lib/active_record/locking/pessimistic.rb +23 -1
  59. data/lib/active_record/log_subscriber.rb +3 -3
  60. data/lib/active_record/migration/command_recorder.rb +8 -8
  61. data/lib/active_record/migration.rb +47 -30
  62. data/lib/active_record/model_schema.rb +366 -0
  63. data/lib/active_record/nested_attributes.rb +3 -2
  64. data/lib/active_record/persistence.rb +51 -9
  65. data/lib/active_record/querying.rb +58 -0
  66. data/lib/active_record/railtie.rb +24 -28
  67. data/lib/active_record/railties/controller_runtime.rb +3 -1
  68. data/lib/active_record/railties/databases.rake +134 -77
  69. data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
  70. data/lib/active_record/readonly_attributes.rb +26 -0
  71. data/lib/active_record/reflection.rb +7 -15
  72. data/lib/active_record/relation/batches.rb +5 -2
  73. data/lib/active_record/relation/calculations.rb +27 -6
  74. data/lib/active_record/relation/delegation.rb +49 -0
  75. data/lib/active_record/relation/finder_methods.rb +6 -5
  76. data/lib/active_record/relation/predicate_builder.rb +12 -19
  77. data/lib/active_record/relation/query_methods.rb +76 -10
  78. data/lib/active_record/relation/spawn_methods.rb +11 -2
  79. data/lib/active_record/relation.rb +77 -34
  80. data/lib/active_record/result.rb +1 -1
  81. data/lib/active_record/sanitization.rb +194 -0
  82. data/lib/active_record/schema_dumper.rb +5 -2
  83. data/lib/active_record/scoping/default.rb +142 -0
  84. data/lib/active_record/scoping/named.rb +202 -0
  85. data/lib/active_record/scoping.rb +152 -0
  86. data/lib/active_record/serialization.rb +1 -43
  87. data/lib/active_record/serializers/xml_serializer.rb +2 -44
  88. data/lib/active_record/session_store.rb +15 -15
  89. data/lib/active_record/store.rb +50 -0
  90. data/lib/active_record/test_case.rb +11 -7
  91. data/lib/active_record/timestamp.rb +16 -3
  92. data/lib/active_record/transactions.rb +5 -5
  93. data/lib/active_record/translation.rb +22 -0
  94. data/lib/active_record/validations/associated.rb +5 -4
  95. data/lib/active_record/validations/uniqueness.rb +4 -4
  96. data/lib/active_record/validations.rb +1 -1
  97. data/lib/active_record/version.rb +2 -2
  98. data/lib/active_record.rb +28 -2
  99. data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
  100. data/lib/rails/generators/active_record/migration/templates/migration.rb +9 -3
  101. data/lib/rails/generators/active_record/model/model_generator.rb +5 -1
  102. data/lib/rails/generators/active_record/model/templates/migration.rb +3 -5
  103. data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
  104. metadata +50 -40
  105. checksums.yaml +0 -7
  106. data/lib/active_record/named_scope.rb +0 -200
@@ -0,0 +1,221 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveRecord
4
+ module AttributeAssignment
5
+ extend ActiveSupport::Concern
6
+ include ActiveModel::MassAssignmentSecurity
7
+
8
+ module ClassMethods
9
+ private
10
+
11
+ # The primary key and inheritance column can never be set by mass-assignment for security reasons.
12
+ def attributes_protected_by_default
13
+ default = [ primary_key, inheritance_column ]
14
+ default << 'id' unless primary_key.eql? 'id'
15
+ default
16
+ end
17
+ end
18
+
19
+ # Allows you to set all the attributes at once by passing in a hash with keys
20
+ # matching the attribute names (which again matches the column names).
21
+ #
22
+ # If any attributes are protected by either +attr_protected+ or
23
+ # +attr_accessible+ then only settable attributes will be assigned.
24
+ #
25
+ # class User < ActiveRecord::Base
26
+ # attr_protected :is_admin
27
+ # end
28
+ #
29
+ # user = User.new
30
+ # user.attributes = { :username => 'Phusion', :is_admin => true }
31
+ # user.username # => "Phusion"
32
+ # user.is_admin? # => false
33
+ def attributes=(new_attributes)
34
+ return unless new_attributes.is_a?(Hash)
35
+
36
+ assign_attributes(new_attributes)
37
+ end
38
+
39
+ # Allows you to set all the attributes for a particular mass-assignment
40
+ # security role by passing in a hash of attributes with keys matching
41
+ # the attribute names (which again matches the column names) and the role
42
+ # name using the :as option.
43
+ #
44
+ # To bypass mass-assignment security you can use the :without_protection => true
45
+ # option.
46
+ #
47
+ # class User < ActiveRecord::Base
48
+ # attr_accessible :name
49
+ # attr_accessible :name, :is_admin, :as => :admin
50
+ # end
51
+ #
52
+ # user = User.new
53
+ # user.assign_attributes({ :name => 'Josh', :is_admin => true })
54
+ # user.name # => "Josh"
55
+ # user.is_admin? # => false
56
+ #
57
+ # user = User.new
58
+ # user.assign_attributes({ :name => 'Josh', :is_admin => true }, :as => :admin)
59
+ # user.name # => "Josh"
60
+ # user.is_admin? # => true
61
+ #
62
+ # user = User.new
63
+ # user.assign_attributes({ :name => 'Josh', :is_admin => true }, :without_protection => true)
64
+ # user.name # => "Josh"
65
+ # user.is_admin? # => true
66
+ def assign_attributes(new_attributes, options = {})
67
+ return unless new_attributes
68
+
69
+ attributes = new_attributes.stringify_keys
70
+ multi_parameter_attributes = []
71
+ nested_parameter_attributes = []
72
+ @mass_assignment_options = options
73
+
74
+ unless options[:without_protection]
75
+ attributes = sanitize_for_mass_assignment(attributes, mass_assignment_role)
76
+ end
77
+
78
+ attributes.each do |k, v|
79
+ if k.include?("(")
80
+ multi_parameter_attributes << [ k, v ]
81
+ elsif respond_to?("#{k}=")
82
+ if v.is_a?(Hash)
83
+ nested_parameter_attributes << [ k, v ]
84
+ else
85
+ send("#{k}=", v)
86
+ end
87
+ else
88
+ raise(UnknownAttributeError, "unknown attribute: #{k}")
89
+ end
90
+ end
91
+
92
+ # assign any deferred nested attributes after the base attributes have been set
93
+ nested_parameter_attributes.each do |k,v|
94
+ send("#{k}=", v)
95
+ end
96
+
97
+ @mass_assignment_options = nil
98
+ assign_multiparameter_attributes(multi_parameter_attributes)
99
+ end
100
+
101
+ protected
102
+
103
+ def mass_assignment_options
104
+ @mass_assignment_options ||= {}
105
+ end
106
+
107
+ def mass_assignment_role
108
+ mass_assignment_options[:as] || :default
109
+ end
110
+
111
+ private
112
+
113
+ # Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done
114
+ # by calling new on the column type or aggregation type (through composed_of) object with these parameters.
115
+ # So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate
116
+ # written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the
117
+ # parentheses to have the parameters typecasted before they're used in the constructor. Use i for Fixnum,
118
+ # f for Float, s for String, and a for Array. If all the values for a given attribute are empty, the
119
+ # attribute will be set to nil.
120
+ def assign_multiparameter_attributes(pairs)
121
+ execute_callstack_for_multiparameter_attributes(
122
+ extract_callstack_for_multiparameter_attributes(pairs)
123
+ )
124
+ end
125
+
126
+ def instantiate_time_object(name, values)
127
+ if self.class.send(:create_time_zone_conversion_attribute?, name, column_for_attribute(name))
128
+ Time.zone.local(*values)
129
+ else
130
+ Time.time_with_datetime_fallback(self.class.default_timezone, *values)
131
+ end
132
+ end
133
+
134
+ def execute_callstack_for_multiparameter_attributes(callstack)
135
+ errors = []
136
+ callstack.each do |name, values_with_empty_parameters|
137
+ begin
138
+ send(name + "=", read_value_from_parameter(name, values_with_empty_parameters))
139
+ rescue => ex
140
+ errors << AttributeAssignmentError.new("error on assignment #{values_with_empty_parameters.values.inspect} to #{name}", ex, name)
141
+ end
142
+ end
143
+ unless errors.empty?
144
+ raise MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes"
145
+ end
146
+ end
147
+
148
+ def read_value_from_parameter(name, values_hash_from_param)
149
+ klass = (self.class.reflect_on_aggregation(name.to_sym) || column_for_attribute(name)).klass
150
+ if values_hash_from_param.values.all?{|v|v.nil?}
151
+ nil
152
+ elsif klass == Time
153
+ read_time_parameter_value(name, values_hash_from_param)
154
+ elsif klass == Date
155
+ read_date_parameter_value(name, values_hash_from_param)
156
+ else
157
+ read_other_parameter_value(klass, name, values_hash_from_param)
158
+ end
159
+ end
160
+
161
+ def read_time_parameter_value(name, values_hash_from_param)
162
+ # If Date bits were not provided, error
163
+ raise "Missing Parameter" if [1,2,3].any?{|position| !values_hash_from_param.has_key?(position)}
164
+ max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param, 6)
165
+ # If Date bits were provided but blank, then return nil
166
+ return nil if (1..3).any? {|position| values_hash_from_param[position].blank?}
167
+
168
+ set_values = (1..max_position).collect{|position| values_hash_from_param[position] }
169
+ # If Time bits are not there, then default to 0
170
+ (3..5).each {|i| set_values[i] = set_values[i].blank? ? 0 : set_values[i]}
171
+ instantiate_time_object(name, set_values)
172
+ end
173
+
174
+ def read_date_parameter_value(name, values_hash_from_param)
175
+ return nil if (1..3).any? {|position| values_hash_from_param[position].blank?}
176
+ set_values = [values_hash_from_param[1], values_hash_from_param[2], values_hash_from_param[3]]
177
+ begin
178
+ Date.new(*set_values)
179
+ rescue ArgumentError # if Date.new raises an exception on an invalid date
180
+ instantiate_time_object(name, set_values).to_date # we instantiate Time object and convert it back to a date thus using Time's logic in handling invalid dates
181
+ end
182
+ end
183
+
184
+ def read_other_parameter_value(klass, name, values_hash_from_param)
185
+ max_position = extract_max_param_for_multiparameter_attributes(values_hash_from_param)
186
+ values = (1..max_position).collect do |position|
187
+ raise "Missing Parameter" if !values_hash_from_param.has_key?(position)
188
+ values_hash_from_param[position]
189
+ end
190
+ klass.new(*values)
191
+ end
192
+
193
+ def extract_max_param_for_multiparameter_attributes(values_hash_from_param, upper_cap = 100)
194
+ [values_hash_from_param.keys.max,upper_cap].min
195
+ end
196
+
197
+ def extract_callstack_for_multiparameter_attributes(pairs)
198
+ attributes = { }
199
+
200
+ pairs.each do |pair|
201
+ multiparameter_name, value = pair
202
+ attribute_name = multiparameter_name.split("(").first
203
+ attributes[attribute_name] = {} unless attributes.include?(attribute_name)
204
+
205
+ parameter_value = value.empty? ? nil : type_cast_attribute_value(multiparameter_name, value)
206
+ attributes[attribute_name][find_parameter_position(multiparameter_name)] ||= parameter_value
207
+ end
208
+
209
+ attributes
210
+ end
211
+
212
+ def type_cast_attribute_value(multiparameter_name, value)
213
+ multiparameter_name =~ /\([0-9]*([if])\)/ ? value.send("to_" + $1) : value
214
+ end
215
+
216
+ def find_parameter_position(multiparameter_name)
217
+ multiparameter_name.scan(/\(([0-9]*).*\)/).first.first.to_i
218
+ end
219
+
220
+ end
221
+ end
@@ -0,0 +1,32 @@
1
+ require 'active_support/concern'
2
+ require 'active_support/deprecation'
3
+
4
+ module ActiveRecord
5
+ module AttributeMethods
6
+ module DeprecatedUnderscoreRead
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ attribute_method_prefix "_"
11
+ end
12
+
13
+ module ClassMethods
14
+ protected
15
+
16
+ def define_method__attribute(attr_name)
17
+ # Do nothing, let it hit method missing instead.
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def _attribute(attr_name)
24
+ ActiveSupport::Deprecation.warn(
25
+ "You have called '_#{attr_name}'. This is deprecated. Please use " \
26
+ "either '#{attr_name}' or read_attribute('#{attr_name}')."
27
+ )
28
+ read_attribute(attr_name)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -34,9 +34,9 @@ module ActiveRecord
34
34
  @previously_changed = changes
35
35
  @changed_attributes.clear
36
36
  end
37
- rescue
38
- IdentityMap.remove(self) if IdentityMap.enabled?
39
- raise
37
+ rescue
38
+ IdentityMap.remove(self) if IdentityMap.enabled?
39
+ raise
40
40
  end
41
41
 
42
42
  # <tt>reload</tt> the record and clears changed attributes.
@@ -5,11 +5,44 @@ module ActiveRecord
5
5
 
6
6
  # Returns this record's primary key value wrapped in an Array if one is available
7
7
  def to_key
8
- key = send(self.class.primary_key)
8
+ key = self.id
9
9
  [key] if key
10
10
  end
11
11
 
12
+ # Returns the primary key value
13
+ def id
14
+ read_attribute(self.class.primary_key)
15
+ end
16
+
17
+ # Sets the primary key value
18
+ def id=(value)
19
+ write_attribute(self.class.primary_key, value)
20
+ end
21
+
22
+ # Queries the primary key value
23
+ def id?
24
+ query_attribute(self.class.primary_key)
25
+ end
26
+
12
27
  module ClassMethods
28
+ def define_method_attribute(attr_name)
29
+ super
30
+
31
+ if attr_name == primary_key && attr_name != 'id'
32
+ generated_attribute_methods.send(:alias_method, :id, primary_key)
33
+ generated_external_attribute_methods.module_eval <<-CODE, __FILE__, __LINE__
34
+ def id(v, attributes, attributes_cache, attr_name)
35
+ attr_name = '#{primary_key}'
36
+ send(attr_name, attributes[attr_name], attributes, attributes_cache, attr_name)
37
+ end
38
+ CODE
39
+ end
40
+ end
41
+
42
+ def dangerous_attribute_method?(method_name)
43
+ super && !['id', 'id=', 'id?'].include?(method_name)
44
+ end
45
+
13
46
  # Defines the primary key field -- can be overridden in subclasses. Overwriting will negate any effect of the
14
47
  # primary_key_prefix_type setting, though.
15
48
  def primary_key
@@ -23,11 +56,11 @@ module ActiveRecord
23
56
  end
24
57
 
25
58
  def reset_primary_key #:nodoc:
26
- key = self == base_class ? get_primary_key(base_class.name) :
27
- base_class.primary_key
28
-
29
- set_primary_key(key)
30
- key
59
+ if self == base_class
60
+ self.primary_key = get_primary_key(base_class.name)
61
+ else
62
+ self.primary_key = base_class.primary_key
63
+ end
31
64
  end
32
65
 
33
66
  def get_primary_key(base_name) #:nodoc:
@@ -39,37 +72,41 @@ module ActiveRecord
39
72
  when :table_name_with_underscore
40
73
  base_name.foreign_key
41
74
  else
42
- if ActiveRecord::Base != self && connection.table_exists?(table_name)
43
- connection.primary_key(table_name)
75
+ if ActiveRecord::Base != self && table_exists?
76
+ connection.schema_cache.primary_keys[table_name]
44
77
  else
45
78
  'id'
46
79
  end
47
80
  end
48
81
  end
49
82
 
50
- attr_accessor :original_primary_key
51
-
52
- # Attribute writer for the primary key column
53
- def primary_key=(value)
54
- @quoted_primary_key = nil
55
- @primary_key = value
56
-
57
- connection_pool.primary_keys[table_name] = @primary_key if connected?
83
+ def original_primary_key #:nodoc:
84
+ deprecated_original_property_getter :primary_key
58
85
  end
59
86
 
60
- # Sets the name of the primary key column to use to the given value,
61
- # or (if the value is nil or false) to the value returned by the given
62
- # block.
87
+ # Sets the name of the primary key column.
63
88
  #
64
89
  # class Project < ActiveRecord::Base
65
- # set_primary_key "sysid"
90
+ # self.primary_key = "sysid"
66
91
  # end
67
- def set_primary_key(value = nil, &block)
92
+ #
93
+ # You can also define the primary_key method yourself:
94
+ #
95
+ # class Project < ActiveRecord::Base
96
+ # def self.primary_key
97
+ # "foo_" + super
98
+ # end
99
+ # end
100
+ # Project.primary_key # => "foo_id"
101
+ def primary_key=(value)
102
+ @original_primary_key = @primary_key if defined?(@primary_key)
103
+ @primary_key = value && value.to_s
104
+ @quoted_primary_key = nil
105
+ end
106
+
107
+ def set_primary_key(value = nil, &block) #:nodoc:
108
+ deprecated_property_setter :primary_key, value, block
68
109
  @quoted_primary_key = nil
69
- @primary_key ||= ''
70
- self.original_primary_key = @primary_key
71
- value &&= value.to_s
72
- self.primary_key = block_given? ? instance_eval(&block) : value
73
110
  end
74
111
  end
75
112
  end
@@ -6,13 +6,8 @@ module ActiveRecord
6
6
  ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
7
7
 
8
8
  included do
9
- attribute_method_suffix ""
10
-
11
9
  cattr_accessor :attribute_types_cached_by_default, :instance_writer => false
12
10
  self.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
13
-
14
- # Undefine id so it can be used as an attribute name
15
- undef_method(:id) if method_defined?(:id)
16
11
  end
17
12
 
18
13
  module ClassMethods
@@ -34,107 +29,101 @@ module ActiveRecord
34
29
  cached_attributes.include?(attr_name)
35
30
  end
36
31
 
32
+ def undefine_attribute_methods
33
+ generated_external_attribute_methods.module_eval do
34
+ instance_methods.each { |m| undef_method(m) }
35
+ end
36
+
37
+ super
38
+ end
39
+
40
+ def type_cast_attribute(attr_name, attributes, cache = {}) #:nodoc:
41
+ return unless attr_name
42
+ attr_name = attr_name.to_s
43
+
44
+ if generated_external_attribute_methods.method_defined?(attr_name)
45
+ if attributes.has_key?(attr_name) || attr_name == 'id'
46
+ generated_external_attribute_methods.send(attr_name, attributes[attr_name], attributes, cache, attr_name)
47
+ end
48
+ elsif !attribute_methods_generated?
49
+ # If we haven't generated the caster methods yet, do that and
50
+ # then try again
51
+ define_attribute_methods
52
+ type_cast_attribute(attr_name, attributes, cache)
53
+ else
54
+ # If we get here, the attribute has no associated DB column, so
55
+ # just return it verbatim.
56
+ attributes[attr_name]
57
+ end
58
+ end
59
+
37
60
  protected
61
+ # We want to generate the methods via module_eval rather than define_method,
62
+ # because define_method is slower on dispatch and uses more memory (because it
63
+ # creates a closure).
64
+ #
65
+ # But sometimes the database might return columns with characters that are not
66
+ # allowed in normal method names (like 'my_column(omg)'. So to work around this
67
+ # we first define with the __temp__ identifier, and then use alias method to
68
+ # rename it to what we want.
38
69
  def define_method_attribute(attr_name)
39
- if serialized_attributes.include?(attr_name)
40
- define_read_method_for_serialized_attribute(attr_name)
41
- else
42
- define_read_method(attr_name, attr_name, columns_hash[attr_name])
43
- end
70
+ cast_code = attribute_cast_code(attr_name)
44
71
 
45
- if attr_name == primary_key && attr_name != "id"
46
- define_read_method('id', attr_name, columns_hash[attr_name])
47
- end
72
+ generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
73
+ def __temp__
74
+ #{internal_attribute_access_code(attr_name, cast_code)}
75
+ end
76
+ alias_method '#{attr_name}', :__temp__
77
+ undef_method :__temp__
78
+ STR
79
+
80
+ generated_external_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
81
+ def __temp__(v, attributes, attributes_cache, attr_name)
82
+ #{external_attribute_access_code(attr_name, cast_code)}
83
+ end
84
+ alias_method '#{attr_name}', :__temp__
85
+ undef_method :__temp__
86
+ STR
48
87
  end
49
88
 
50
89
  private
51
90
  def cacheable_column?(column)
52
- serialized_attributes.include?(column.name) || attribute_types_cached_by_default.include?(column.type)
53
- end
54
-
55
- # Define read method for serialized attribute.
56
- def define_read_method_for_serialized_attribute(attr_name)
57
- access_code = "@attributes_cache['#{attr_name}'] ||= @attributes['#{attr_name}']"
58
- generated_attribute_methods.module_eval("def _#{attr_name}; #{access_code}; end; alias #{attr_name} _#{attr_name}", __FILE__, __LINE__)
91
+ attribute_types_cached_by_default.include?(column.type)
59
92
  end
60
93
 
61
- # Define an attribute reader method. Cope with nil column.
62
- # method_name is the same as attr_name except when a non-standard primary key is used,
63
- # we still define #id as an accessor for the key
64
- def define_read_method(method_name, attr_name, column)
65
- cast_code = column.type_cast_code('v')
66
- access_code = "(v=@attributes['#{attr_name}']) && #{cast_code}"
94
+ def internal_attribute_access_code(attr_name, cast_code)
95
+ access_code = "(v=@attributes[attr_name]) && #{cast_code}"
67
96
 
68
- unless attr_name.to_s == self.primary_key.to_s
69
- access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
97
+ unless attr_name == primary_key
98
+ access_code.insert(0, "missing_attribute(attr_name, caller) unless @attributes.has_key?(attr_name); ")
70
99
  end
71
100
 
72
101
  if cache_attribute?(attr_name)
73
- access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
102
+ access_code = "@attributes_cache[attr_name] ||= (#{access_code})"
74
103
  end
75
104
 
76
- # Where possible, generate the method by evalling a string, as this will result in
77
- # faster accesses because it avoids the block eval and then string eval incurred
78
- # by the second branch.
79
- #
80
- # The second, slower, branch is necessary to support instances where the database
81
- # returns columns with extra stuff in (like 'my_column(omg)').
82
- if method_name =~ ActiveModel::AttributeMethods::COMPILABLE_REGEXP
83
- generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__
84
- def _#{method_name}
85
- #{access_code}
86
- end
87
-
88
- alias #{method_name} _#{method_name}
89
- STR
90
- else
91
- generated_attribute_methods.module_eval do
92
- define_method("_#{method_name}") { eval(access_code) }
93
- alias_method(method_name, "_#{method_name}")
94
- end
95
- end
105
+ "attr_name = '#{attr_name}'; #{access_code}"
96
106
  end
97
- end
98
107
 
99
- # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
100
- # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
101
- def read_attribute(attr_name)
102
- method = "_#{attr_name}"
103
- if respond_to? method
104
- send method if @attributes.has_key?(attr_name.to_s)
105
- else
106
- _read_attribute attr_name
107
- end
108
- end
108
+ def external_attribute_access_code(attr_name, cast_code)
109
+ access_code = "v && #{cast_code}"
109
110
 
110
- def _read_attribute(attr_name)
111
- attr_name = attr_name.to_s
112
- attr_name = self.class.primary_key if attr_name == 'id'
113
- value = @attributes[attr_name]
114
- unless value.nil?
115
- if column = column_for_attribute(attr_name)
116
- if unserializable_attribute?(attr_name, column)
117
- unserialize_attribute(attr_name)
118
- else
119
- column.type_cast(value)
111
+ if cache_attribute?(attr_name)
112
+ access_code = "attributes_cache[attr_name] ||= (#{access_code})"
120
113
  end
121
- else
122
- value
114
+
115
+ access_code
123
116
  end
124
- end
125
- end
126
117
 
127
- # Returns true if the attribute is of a text column and marked for serialization.
128
- def unserializable_attribute?(attr_name, column)
129
- column.text? && self.class.serialized_attributes.include?(attr_name)
118
+ def attribute_cast_code(attr_name)
119
+ columns_hash[attr_name].type_cast_code('v')
120
+ end
130
121
  end
131
122
 
132
- # Returns the unserialized object of the attribute.
133
- def unserialize_attribute(attr_name)
134
- coder = self.class.serialized_attributes[attr_name]
135
- unserialized_object = coder.load(@attributes[attr_name])
136
-
137
- @attributes.frozen? ? unserialized_object : @attributes[attr_name] = unserialized_object
123
+ # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
124
+ # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
125
+ def read_attribute(attr_name)
126
+ self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
138
127
  end
139
128
 
140
129
  private
@@ -0,0 +1,93 @@
1
+ module ActiveRecord
2
+ module AttributeMethods
3
+ module Serialization
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ # Returns a hash of all the attributes that have been specified for serialization as
8
+ # keys and their class restriction as values.
9
+ class_attribute :serialized_attributes
10
+ self.serialized_attributes = {}
11
+ end
12
+
13
+ class Attribute < Struct.new(:coder, :value, :state)
14
+ def unserialized_value
15
+ state == :serialized ? unserialize : value
16
+ end
17
+
18
+ def serialized_value
19
+ state == :unserialized ? serialize : value
20
+ end
21
+
22
+ def unserialize
23
+ self.state = :unserialized
24
+ self.value = coder.load(value)
25
+ end
26
+
27
+ def serialize
28
+ self.state = :serialized
29
+ self.value = coder.dump(value)
30
+ end
31
+ end
32
+
33
+ module ClassMethods
34
+ # If you have an attribute that needs to be saved to the database as an object, and retrieved as the same object,
35
+ # then specify the name of that attribute using this method and it will be handled automatically.
36
+ # The serialization is done through YAML. If +class_name+ is specified, the serialized object must be of that
37
+ # class on retrieval or SerializationTypeMismatch will be raised.
38
+ #
39
+ # ==== Parameters
40
+ #
41
+ # * +attr_name+ - The field name that should be serialized.
42
+ # * +class_name+ - Optional, class name that the object type should be equal to.
43
+ #
44
+ # ==== Example
45
+ # # Serialize a preferences attribute
46
+ # class User < ActiveRecord::Base
47
+ # serialize :preferences
48
+ # end
49
+ def serialize(attr_name, class_name = Object)
50
+ coder = if [:load, :dump].all? { |x| class_name.respond_to?(x) }
51
+ class_name
52
+ else
53
+ Coders::YAMLColumn.new(class_name)
54
+ end
55
+
56
+ # merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy
57
+ # has its own hash of own serialized attributes
58
+ self.serialized_attributes = serialized_attributes.merge(attr_name.to_s => coder)
59
+ end
60
+
61
+ def initialize_attributes(attributes) #:nodoc:
62
+ super
63
+
64
+ serialized_attributes.each do |key, coder|
65
+ if attributes.key?(key)
66
+ attributes[key] = Attribute.new(coder, attributes[key], :serialized)
67
+ end
68
+ end
69
+
70
+ attributes
71
+ end
72
+
73
+ private
74
+
75
+ def attribute_cast_code(attr_name)
76
+ if serialized_attributes.include?(attr_name)
77
+ "v.unserialized_value"
78
+ else
79
+ super
80
+ end
81
+ end
82
+ end
83
+
84
+ def type_cast_attribute_for_write(column, value)
85
+ if column && coder = self.class.serialized_attributes[column.name]
86
+ Attribute.new(coder, value, :unserialized)
87
+ else
88
+ super
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end