aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-05-09 11:46:45 +0200
committerXavier Noria <fxn@hashref.com>2010-05-09 11:46:45 +0200
commite1a0d86fe0355ddff8c86db0f42f3824ffe14c02 (patch)
tree5833022ca41905f243c15c8a5ffdf864de69bfa6 /activerecord/lib/active_record/relation
parent1ff3d951e620ddeeb97e87e2024391470e886467 (diff)
parentdf508bd97062b871fe25eda8d1bb61cd43d79bc4 (diff)
downloadrails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.tar.gz
rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.tar.bz2
rails-e1a0d86fe0355ddff8c86db0f42f3824ffe14c02.zip
Merge remote branch 'rails/master'
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r--activerecord/lib/active_record/relation/calculations.rb16
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb22
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb8
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb7
4 files changed, 31 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb
index a5ea6e7e3a..858d298470 100644
--- a/activerecord/lib/active_record/relation/calculations.rb
+++ b/activerecord/lib/active_record/relation/calculations.rb
@@ -195,7 +195,7 @@ module ActiveRecord
select_statement << ", #{group_field} AS #{group_alias}"
- relation = select(select_statement).group(group)
+ relation = except(:group).select(select_statement).group(group)
calculated_data = @klass.connection.select_all(relation.to_sql)
@@ -239,11 +239,15 @@ module ActiveRecord
end
def type_cast_calculated_value(value, column, operation = nil)
- case operation
- when 'count' then value.to_i
- when 'sum' then type_cast_using_column(value || '0', column)
- when 'average' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d
- else type_cast_using_column(value, column)
+ if value.is_a?(String) || value.nil?
+ case operation
+ when 'count' then value.to_i
+ when 'sum' then type_cast_using_column(value || '0', column)
+ when 'average' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d
+ else type_cast_using_column(value, column)
+ end
+ else
+ value
end
end
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index d6144dc206..7a0c9dc612 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -234,20 +234,24 @@ module ActiveRecord
end
def find_or_instantiator_by_attributes(match, attributes, *args)
- guard_protected_attributes = false
-
- if args[0].is_a?(Hash)
- guard_protected_attributes = true
- attributes_for_create = args[0].with_indifferent_access
- conditions = attributes_for_create.slice(*attributes).symbolize_keys
- else
- attributes_for_create = conditions = attributes.inject({}) {|h, a| h[a] = args[attributes.index(a)]; h}
+ protected_attributes_for_create, unprotected_attributes_for_create = {}, {}
+ args.each_with_index do |arg, i|
+ if arg.is_a?(Hash)
+ protected_attributes_for_create = args[i].with_indifferent_access
+ else
+ unprotected_attributes_for_create[attributes[i]] = args[i]
+ end
end
+ conditions = (protected_attributes_for_create.merge(unprotected_attributes_for_create)).slice(*attributes).symbolize_keys
+
record = where(conditions).first
unless record
- record = @klass.new { |r| r.send(:attributes=, attributes_for_create, guard_protected_attributes) }
+ record = @klass.new do |r|
+ r.send(:attributes=, protected_attributes_for_create, true) unless protected_attributes_for_create.empty?
+ r.send(:attributes=, unprotected_attributes_for_create, false) unless unprotected_attributes_for_create.empty?
+ end
yield(record) if block_given?
record.save if match.instantiator == :create
end
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 7bca12d85e..8d8bb659e1 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -162,13 +162,9 @@ module ActiveRecord
arel = arel.take(@limit_value) if @limit_value.present?
arel = arel.skip(@offset_value) if @offset_value.present?
- @group_values.uniq.each do |g|
- arel = arel.group(g) if g.present?
- end
+ arel = arel.group(*@group_values.uniq.select{|g| g.present?})
- @order_values.uniq.each do |o|
- arel = arel.order(Arel::SqlLiteral.new(o.to_s)) if o.present?
- end
+ arel = arel.order(*@order_values.uniq.select{|o| o.present?}.map(&:to_s))
selects = @select_values.uniq
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 8fdd64afcc..bb1f138f5b 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -80,10 +80,15 @@ module ActiveRecord
options.assert_valid_keys(VALID_FIND_OPTIONS)
- [:joins, :select, :group, :having, :order, :limit, :offset, :from, :lock, :readonly].each do |finder|
+ [:joins, :select, :group, :having, :limit, :offset, :from, :lock, :readonly].each do |finder|
relation = relation.send(finder, options[finder]) if options.has_key?(finder)
end
+ # Give precedence to newly-applied orders and groups to play nicely with with_scope
+ [:group, :order].each do |finder|
+ relation.send("#{finder}_values=", Array.wrap(options[finder]) + relation.send("#{finder}_values")) if options.has_key?(finder)
+ end
+
relation = relation.where(options[:conditions]) if options.has_key?(:conditions)
relation = relation.includes(options[:include]) if options.has_key?(:include)
relation = relation.extending(options[:extend]) if options.has_key?(:extend)