activerecord 3.1.12 → 3.2.22.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +804 -338
- data/README.rdoc +3 -3
- data/examples/performance.rb +20 -1
- data/lib/active_record/aggregations.rb +1 -1
- data/lib/active_record/associations/alias_tracker.rb +3 -6
- data/lib/active_record/associations/association.rb +13 -45
- data/lib/active_record/associations/association_scope.rb +3 -15
- data/lib/active_record/associations/belongs_to_association.rb +1 -1
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +2 -1
- data/lib/active_record/associations/builder/association.rb +6 -4
- data/lib/active_record/associations/builder/belongs_to.rb +7 -4
- data/lib/active_record/associations/builder/collection_association.rb +2 -2
- data/lib/active_record/associations/builder/has_many.rb +4 -4
- data/lib/active_record/associations/builder/has_one.rb +5 -6
- data/lib/active_record/associations/builder/singular_association.rb +3 -16
- data/lib/active_record/associations/collection_association.rb +65 -32
- data/lib/active_record/associations/collection_proxy.rb +8 -41
- data/lib/active_record/associations/has_and_belongs_to_many_association.rb +1 -0
- data/lib/active_record/associations/has_many_association.rb +11 -7
- data/lib/active_record/associations/has_many_through_association.rb +19 -9
- data/lib/active_record/associations/has_one_association.rb +23 -13
- data/lib/active_record/associations/join_dependency/join_association.rb +6 -1
- data/lib/active_record/associations/join_dependency.rb +3 -3
- data/lib/active_record/associations/preloader/through_association.rb +3 -3
- data/lib/active_record/associations/preloader.rb +14 -10
- data/lib/active_record/associations/through_association.rb +8 -4
- data/lib/active_record/associations.rb +92 -76
- data/lib/active_record/attribute_assignment.rb +221 -0
- data/lib/active_record/attribute_methods/deprecated_underscore_read.rb +32 -0
- data/lib/active_record/attribute_methods/dirty.rb +21 -11
- data/lib/active_record/attribute_methods/primary_key.rb +62 -25
- data/lib/active_record/attribute_methods/read.rb +73 -83
- data/lib/active_record/attribute_methods/serialization.rb +120 -0
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +12 -14
- data/lib/active_record/attribute_methods/write.rb +32 -6
- data/lib/active_record/attribute_methods.rb +231 -30
- data/lib/active_record/autosave_association.rb +44 -26
- data/lib/active_record/base.rb +227 -1708
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +150 -148
- data/lib/active_record/connection_adapters/abstract/connection_specification.rb +85 -29
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +7 -34
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +10 -2
- data/lib/active_record/connection_adapters/abstract/quoting.rb +7 -4
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +39 -28
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +48 -19
- data/lib/active_record/connection_adapters/abstract_adapter.rb +77 -42
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +676 -0
- data/lib/active_record/connection_adapters/column.rb +37 -11
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +133 -581
- data/lib/active_record/connection_adapters/mysql_adapter.rb +136 -693
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +209 -97
- data/lib/active_record/connection_adapters/schema_cache.rb +69 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +2 -6
- data/lib/active_record/connection_adapters/sqlite_adapter.rb +62 -35
- data/lib/active_record/counter_cache.rb +9 -4
- data/lib/active_record/dynamic_finder_match.rb +12 -0
- data/lib/active_record/dynamic_matchers.rb +84 -0
- data/lib/active_record/errors.rb +11 -1
- data/lib/active_record/explain.rb +86 -0
- data/lib/active_record/explain_subscriber.rb +25 -0
- data/lib/active_record/fixtures/file.rb +65 -0
- data/lib/active_record/fixtures.rb +57 -86
- data/lib/active_record/identity_map.rb +3 -4
- data/lib/active_record/inheritance.rb +174 -0
- data/lib/active_record/integration.rb +60 -0
- data/lib/active_record/locking/optimistic.rb +33 -26
- data/lib/active_record/locking/pessimistic.rb +23 -1
- data/lib/active_record/log_subscriber.rb +8 -4
- data/lib/active_record/migration/command_recorder.rb +8 -8
- data/lib/active_record/migration.rb +68 -35
- data/lib/active_record/model_schema.rb +368 -0
- data/lib/active_record/nested_attributes.rb +60 -24
- data/lib/active_record/persistence.rb +57 -11
- data/lib/active_record/query_cache.rb +6 -6
- data/lib/active_record/querying.rb +58 -0
- data/lib/active_record/railtie.rb +37 -29
- data/lib/active_record/railties/controller_runtime.rb +3 -1
- data/lib/active_record/railties/databases.rake +213 -117
- data/lib/active_record/railties/jdbcmysql_error.rb +1 -1
- data/lib/active_record/readonly_attributes.rb +26 -0
- data/lib/active_record/reflection.rb +7 -15
- data/lib/active_record/relation/batches.rb +7 -4
- data/lib/active_record/relation/calculations.rb +55 -16
- data/lib/active_record/relation/delegation.rb +49 -0
- data/lib/active_record/relation/finder_methods.rb +16 -11
- data/lib/active_record/relation/predicate_builder.rb +8 -6
- data/lib/active_record/relation/query_methods.rb +75 -9
- data/lib/active_record/relation/spawn_methods.rb +48 -7
- data/lib/active_record/relation.rb +78 -32
- data/lib/active_record/result.rb +10 -4
- data/lib/active_record/sanitization.rb +194 -0
- data/lib/active_record/schema_dumper.rb +12 -5
- data/lib/active_record/scoping/default.rb +142 -0
- data/lib/active_record/scoping/named.rb +200 -0
- data/lib/active_record/scoping.rb +152 -0
- data/lib/active_record/serialization.rb +1 -43
- data/lib/active_record/serializers/xml_serializer.rb +4 -45
- data/lib/active_record/session_store.rb +18 -16
- data/lib/active_record/store.rb +52 -0
- data/lib/active_record/test_case.rb +11 -7
- data/lib/active_record/timestamp.rb +17 -3
- data/lib/active_record/transactions.rb +27 -6
- data/lib/active_record/translation.rb +22 -0
- data/lib/active_record/validations/associated.rb +5 -4
- data/lib/active_record/validations/uniqueness.rb +8 -8
- data/lib/active_record/validations.rb +1 -1
- data/lib/active_record/version.rb +3 -3
- data/lib/active_record.rb +38 -3
- data/lib/rails/generators/active_record/migration/migration_generator.rb +1 -1
- data/lib/rails/generators/active_record/migration/templates/migration.rb +12 -3
- data/lib/rails/generators/active_record/model/model_generator.rb +9 -1
- data/lib/rails/generators/active_record/model/templates/migration.rb +3 -5
- data/lib/rails/generators/active_record/model/templates/model.rb +5 -0
- data/lib/rails/generators/active_record/session_migration/templates/migration.rb +1 -5
- metadata +49 -28
- 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 if new_attributes.blank?
|
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
|
-
|
38
|
-
|
39
|
-
|
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.
|
@@ -55,12 +55,12 @@ module ActiveRecord
|
|
55
55
|
# The attribute already has an unsaved change.
|
56
56
|
if attribute_changed?(attr)
|
57
57
|
old = @changed_attributes[attr]
|
58
|
-
@changed_attributes.delete(attr) unless
|
58
|
+
@changed_attributes.delete(attr) unless _field_changed?(attr, old, value)
|
59
59
|
else
|
60
60
|
old = clone_attribute_value(:read_attribute, attr)
|
61
61
|
# Save Time objects as TimeWithZone if time_zone_aware_attributes == true
|
62
62
|
old = old.in_time_zone if clone_with_time_zone_conversion_attribute?(attr, old)
|
63
|
-
@changed_attributes[attr] = old if
|
63
|
+
@changed_attributes[attr] = old if _field_changed?(attr, old, value)
|
64
64
|
end
|
65
65
|
|
66
66
|
# Carry on.
|
@@ -77,13 +77,10 @@ module ActiveRecord
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
def
|
80
|
+
def _field_changed?(attr, old, value)
|
81
81
|
if column = column_for_attribute(attr)
|
82
|
-
if column.number? &&
|
83
|
-
|
84
|
-
# Hence we don't record it as a change if the value changes from nil to ''.
|
85
|
-
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
|
86
|
-
# be typecast back to 0 (''.to_i => 0)
|
82
|
+
if column.number? && (changes_from_nil_to_empty_string?(column, old, value) ||
|
83
|
+
changes_from_zero_to_string?(old, value))
|
87
84
|
value = nil
|
88
85
|
else
|
89
86
|
value = column.type_cast(value)
|
@@ -96,6 +93,19 @@ module ActiveRecord
|
|
96
93
|
def clone_with_time_zone_conversion_attribute?(attr, old)
|
97
94
|
old.class.name == "Time" && time_zone_aware_attributes && !self.skip_time_zone_conversion_for_attributes.include?(attr.to_sym)
|
98
95
|
end
|
96
|
+
|
97
|
+
def changes_from_nil_to_empty_string?(column, old, value)
|
98
|
+
# For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
|
99
|
+
# Hence we don't record it as a change if the value changes from nil to ''.
|
100
|
+
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
|
101
|
+
# be typecast back to 0 (''.to_i => 0)
|
102
|
+
column.null && (old.nil? || old == 0) && value.blank?
|
103
|
+
end
|
104
|
+
|
105
|
+
def changes_from_zero_to_string?(old, value)
|
106
|
+
# For columns with old 0 and value non-empty string
|
107
|
+
old == 0 && value.is_a?(String) && value.present? && value != '0'
|
108
|
+
end
|
99
109
|
end
|
100
110
|
end
|
101
111
|
end
|
@@ -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 =
|
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
|
-
|
27
|
-
base_class.
|
28
|
-
|
29
|
-
|
30
|
-
|
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 &&
|
43
|
-
connection.
|
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
|
-
|
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
|
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
|
-
#
|
90
|
+
# self.primary_key = "sysid"
|
66
91
|
# end
|
67
|
-
|
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,102 @@ module ActiveRecord
|
|
34
29
|
cached_attributes.include?(attr_name)
|
35
30
|
end
|
36
31
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
44
39
|
|
45
|
-
|
46
|
-
|
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
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
|
+
|
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.
|
69
|
+
def define_method_attribute(attr_name)
|
70
|
+
generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
|
71
|
+
def __temp__
|
72
|
+
#{internal_attribute_access_code(attr_name, attribute_cast_code(attr_name))}
|
73
|
+
end
|
74
|
+
alias_method '#{attr_name}', :__temp__
|
75
|
+
undef_method :__temp__
|
76
|
+
STR
|
48
77
|
end
|
49
78
|
|
50
79
|
private
|
51
|
-
|
52
|
-
|
80
|
+
|
81
|
+
def define_external_attribute_method(attr_name)
|
82
|
+
generated_external_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
|
83
|
+
def __temp__(v, attributes, attributes_cache, attr_name)
|
84
|
+
#{external_attribute_access_code(attr_name, attribute_cast_code(attr_name))}
|
85
|
+
end
|
86
|
+
alias_method '#{attr_name}', :__temp__
|
87
|
+
undef_method :__temp__
|
88
|
+
STR
|
53
89
|
end
|
54
90
|
|
55
|
-
|
56
|
-
|
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
|
+
def cacheable_column?(column)
|
92
|
+
attribute_types_cached_by_default.include?(column.type)
|
59
93
|
end
|
60
94
|
|
61
|
-
|
62
|
-
|
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}"
|
95
|
+
def internal_attribute_access_code(attr_name, cast_code)
|
96
|
+
access_code = "(v=@attributes[attr_name]) && #{cast_code}"
|
67
97
|
|
68
|
-
unless attr_name
|
69
|
-
access_code.insert(0, "missing_attribute(
|
98
|
+
unless attr_name == primary_key
|
99
|
+
access_code.insert(0, "missing_attribute(attr_name, caller) unless @attributes.has_key?(attr_name); ")
|
70
100
|
end
|
71
101
|
|
72
102
|
if cache_attribute?(attr_name)
|
73
|
-
access_code = "@attributes_cache[
|
103
|
+
access_code = "@attributes_cache[attr_name] ||= (#{access_code})"
|
74
104
|
end
|
75
105
|
|
76
|
-
|
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
|
106
|
+
"attr_name = '#{attr_name}'; #{access_code}"
|
96
107
|
end
|
97
|
-
end
|
98
108
|
|
99
|
-
|
100
|
-
|
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
|
109
|
+
def external_attribute_access_code(attr_name, cast_code)
|
110
|
+
access_code = "v && #{cast_code}"
|
109
111
|
|
110
|
-
|
111
|
-
|
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)
|
112
|
+
if cache_attribute?(attr_name)
|
113
|
+
access_code = "attributes_cache[attr_name] ||= (#{access_code})"
|
120
114
|
end
|
121
|
-
|
122
|
-
|
115
|
+
|
116
|
+
access_code
|
123
117
|
end
|
124
|
-
end
|
125
|
-
end
|
126
118
|
|
127
|
-
|
128
|
-
|
129
|
-
|
119
|
+
def attribute_cast_code(attr_name)
|
120
|
+
columns_hash[attr_name].type_cast_code('v')
|
121
|
+
end
|
130
122
|
end
|
131
123
|
|
132
|
-
# Returns the
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
@attributes.frozen? ? unserialized_object : @attributes[attr_name] = unserialized_object
|
124
|
+
# Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
|
125
|
+
# "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
|
126
|
+
def read_attribute(attr_name)
|
127
|
+
self.class.type_cast_attribute(attr_name, @attributes, @attributes_cache)
|
138
128
|
end
|
139
129
|
|
140
130
|
private
|