aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/base.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/base.rb')
-rwxr-xr-xactiverecord/lib/active_record/base.rb76
1 files changed, 42 insertions, 34 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index cfc6a5693a..c2a8d3ec3d 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -372,7 +372,7 @@ module ActiveRecord #:nodoc:
def self.reset_subclasses #:nodoc:
nonreloadables = []
subclasses.each do |klass|
- unless Dependencies.autoloaded? klass
+ unless ActiveSupport::Dependencies.autoloaded? klass
nonreloadables << klass
next
end
@@ -1296,6 +1296,20 @@ module ActiveRecord #:nodoc:
store_full_sti_class ? name : name.demodulize
end
+ # Merges conditions so that the result is a valid +condition+
+ def merge_conditions(*conditions)
+ segments = []
+
+ conditions.each do |condition|
+ unless condition.blank?
+ sql = sanitize_sql(condition)
+ segments << sql unless sql.blank?
+ end
+ end
+
+ "(#{segments.join(') AND (')})" unless segments.empty?
+ end
+
private
def find_initial(options)
options.update(:limit => 1)
@@ -1464,7 +1478,7 @@ module ActiveRecord #:nodoc:
def construct_finder_sql(options)
scope = scope(:find)
- sql = "SELECT #{options[:select] || (scope && scope[:select]) || (options[:joins] && quoted_table_name + '.*') || '*'} "
+ sql = "SELECT #{options[:select] || (scope && scope[:select]) || ((options[:joins] || (scope && scope[:joins])) && quoted_table_name + '.*') || '*'} "
sql << "FROM #{(scope && scope[:from]) || options[:from] || quoted_table_name} "
add_joins!(sql, options, scope)
@@ -1483,20 +1497,6 @@ module ActiveRecord #:nodoc:
(safe_to_array(first) + safe_to_array(second)).uniq
end
- # Merges conditions so that the result is a valid +condition+
- def merge_conditions(*conditions)
- segments = []
-
- conditions.each do |condition|
- unless condition.blank?
- sql = sanitize_sql(condition)
- segments << sql unless sql.blank?
- end
- end
-
- "(#{segments.join(') AND (')})" unless segments.empty?
- end
-
# Object#to_a is deprecated, though it does have the desired behavior
def safe_to_array(o)
case o
@@ -1902,10 +1902,12 @@ module ActiveRecord #:nodoc:
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
def compute_type(type_name)
modularized_name = type_name_with_module(type_name)
- begin
- class_eval(modularized_name, __FILE__, __LINE__)
- rescue NameError
- class_eval(type_name, __FILE__, __LINE__)
+ silence_warnings do
+ begin
+ class_eval(modularized_name, __FILE__, __LINE__)
+ rescue NameError
+ class_eval(type_name, __FILE__, __LINE__)
+ end
end
end
@@ -2052,9 +2054,10 @@ module ActiveRecord #:nodoc:
end
def replace_named_bind_variables(statement, bind_vars) #:nodoc:
- statement.gsub(/:([a-zA-Z]\w*)/) do
- match = $1.to_sym
- if bind_vars.include?(match)
+ statement.gsub(/(:?):([a-zA-Z]\w*)/) do
+ if $1 == ':' # skip postgresql casts
+ $& # return the whole match
+ elsif bind_vars.include?(match = $2.to_sym)
quote_bound_value(bind_vars[match])
else
raise PreparedStatementInvalid, "missing value for :#{match} in #{statement}"
@@ -2063,13 +2066,18 @@ module ActiveRecord #:nodoc:
end
def expand_range_bind_variables(bind_vars) #:nodoc:
- bind_vars.sum do |var|
+ expanded = []
+
+ bind_vars.each do |var|
if var.is_a?(Range)
- [var.first, var.last]
+ expanded << var.first
+ expanded << var.last
else
- [var]
+ expanded << var
end
end
+
+ expanded
end
def quote_bound_value(value) #:nodoc:
@@ -2161,11 +2169,11 @@ module ActiveRecord #:nodoc:
def cache_key
case
when new_record?
- "#{self.class.name.tableize}/new"
- when self[:updated_at]
- "#{self.class.name.tableize}/#{id}-#{updated_at.to_s(:number)}"
+ "#{self.class.model_name.cache_key}/new"
+ when timestamp = self[:updated_at]
+ "#{self.class.model_name.cache_key}/#{id}-#{timestamp.to_s(:number)}"
else
- "#{self.class.name.tableize}/#{id}"
+ "#{self.class.model_name.cache_key}/#{id}"
end
end
@@ -2246,12 +2254,12 @@ module ActiveRecord #:nodoc:
end
end
- # Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records.
- # Note: This method is overwritten by the Validation module that'll make sure that updates made with this method
- # aren't subjected to validation checks. Hence, attributes can be updated even if the full object isn't valid.
+ # Updates a single attribute and saves the record without going through the normal validation procedure.
+ # This is especially useful for boolean flags on existing records. The regular +update_attribute+ method
+ # in Base is replaced with this when the validations module is mixed in, which it is by default.
def update_attribute(name, value)
send(name.to_s + '=', value)
- save
+ save(false)
end
# Updates all the attributes from the passed-in Hash and saves the record. If the object is invalid, the saving will