Permalink
Browse files

Update select_for_count to support AR 4.0.1

  • Loading branch information...
1 parent 616ec03 commit dfbdd68d10bf6f3750d860f9ed41f0cc16858ac1 @ernie ernie committed Jul 15, 2013
View
@@ -1,3 +1,8 @@
+## 1.1.1 (unreleased)
+
+* Update relation extensions to support new count behavior in Active Record
+ 4.0.1 (see rails/rails@da9b5d4a)
+
## 1.1.0 (2013-07-14)
* Support for Active Record 4.0.0!
@@ -118,6 +118,39 @@ def dehashified_order_values
}
end
+ # So, building a select for a count query in Active Record is
+ # pretty heavily dependent on select_values containing strings.
+ # I'd initially expected that I could just hack together a fix
+ # to select_for_count and everything would fall in line, but
+ # unfortunately, pretty much everything from that point on
+ # in ActiveRecord::Calculations#perform_calculation expects
+ # the column to be a string, or at worst, a symbol.
+ #
+ # In the long term, I would like to refactor the code in
+ # Rails core, but for now, I'm going to settle for this hack
+ # that tries really hard to coerce things to a string.
+ def select_for_count
+ visited_values = select_visit(select_values.uniq)
+ if visited_values.any?
+ string_values = visited_values.map { |value|
+ case value
+ when String
+ value
+ when Symbol
+ value.to_s
+ when Arel::Attributes::Attribute
+ join_name = value.relation.table_alias || value.relation.name
+ "#{connection.quote_table_name join_name}.#{connection.quote_column_name value.name}"
+ else
+ value.respond_to?(:to_sql) ? value.to_sql : value
+ end
+ }
+ string_values.join(', ')
+ else
+ :all
+ end
+ end
+
end
end
end
@@ -421,11 +421,7 @@ module ActiveRecord
describe '#count' do
it 'works with non-strings in select' do
- if activerecord_version_at_least '4.0.0'
- pending 'broken on current 4-0-stable'
- else
- Article.select{distinct(title)}.count.should eq 51
- end
+ Article.select{distinct(title)}.count.should eq 51
end
it 'works with non-strings in wheres' do

0 comments on commit dfbdd68

Please sign in to comment.