Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Prevent AR::Relation#order! from breaking KeyPaths

Fixes #248
  • Loading branch information...
commit c04fc129cd17ea95ee6203840a7ca66a1de0cc13 1 parent b566cab
@ernie ernie authored
View
3  CHANGELOG.md
@@ -1,7 +1,10 @@
## 1.1.0 (unreleased)
+* Support for Active Record 4.0.0!
* Deprecated core extensions. In Squeel 2.0, the DSL will be the way to
construct queries, and Symbol/Hash extensions will go away.
+* Prefix generated sifter methods with `sifter_` so as not to interfere with
+ similarly-named scopes.
## 1.0.18 (2013-03-07)
View
22 lib/squeel/adapters/active_record/4.0/relation_extensions.rb
@@ -84,6 +84,28 @@ def build_from
end
end
+ # This is copied directly from 4.0.0's implementation, but adds an extra
+ # exclusion for Squeel::Nodes::Node to fix #248. Can be removed if/when
+ # rails/rails#11439 is merged.
+ def order!(*args)
+ args.flatten!
+ validate_order_args args
+
+ references = args.reject { |arg|
+ Arel::Node === arg || Squeel::Nodes::Node === arg
+ }
+ references.map! { |arg| arg =~ /^([a-zA-Z]\w*)\.(\w+)/ && $1 }.compact!
+ references!(references) if references.any?
+
+ # if a symbol is given we prepend the quoted table name
+ args = args.map { |arg|
+ arg.is_a?(Symbol) ? "#{quoted_table_name}.#{arg} ASC" : arg
+ }
+
+ self.order_values = args + self.order_values
+ self
+ end
+
private
def dehashified_order_values
View
5 spec/squeel/adapters/active_record/relation_extensions_spec.rb
@@ -603,6 +603,11 @@ module ActiveRecord
end
end
+ it 'allows ordering by an attributes of a joined table' do
+ relation = Article.joins(:person).order { person.id.asc }
+ relation.to_sql.should match /ORDER BY "people"\."id" ASC/
+ end
+
end
describe '#reorder' do
Please sign in to comment.
Something went wrong with that request. Please try again.