Skip to content

Commit

Permalink
we only need to support asc and ASC. No need for mixed cases. #14263
Browse files Browse the repository at this point in the history


This is a result of the discussion at https://github.com/rails/rails/pull/14263/files#r10291489
  • Loading branch information
senny committed Mar 5, 2014
1 parent 8d486c6 commit f6aeb8b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 34 deletions.
27 changes: 14 additions & 13 deletions activerecord/lib/active_record/relation/query_methods.rb
Expand Up @@ -275,15 +275,6 @@ def group!(*args) # :nodoc:


# Allows to specify an order attribute: # Allows to specify an order attribute:
# #
# User.order('name')
# => SELECT "users".* FROM "users" ORDER BY name
#
# User.order('name DESC')
# => SELECT "users".* FROM "users" ORDER BY name DESC
#
# User.order('name DESC, email')
# => SELECT "users".* FROM "users" ORDER BY name DESC, email
#
# User.order(:name) # User.order(:name)
# => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC # => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC
# #
Expand All @@ -292,6 +283,15 @@ def group!(*args) # :nodoc:
# #
# User.order(:name, email: :desc) # User.order(:name, email: :desc)
# => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC, "users"."email" DESC # => SELECT "users".* FROM "users" ORDER BY "users"."name" ASC, "users"."email" DESC
#
# User.order('name')
# => SELECT "users".* FROM "users" ORDER BY name
#
# User.order('name DESC')
# => SELECT "users".* FROM "users" ORDER BY name DESC
#
# User.order('name DESC, email')
# => SELECT "users".* FROM "users" ORDER BY name DESC, email
def order(*args) def order(*args)
check_if_method_has_arguments!(:order, args) check_if_method_has_arguments!(:order, args)
spawn.order!(*args) spawn.order!(*args)
Expand Down Expand Up @@ -1030,13 +1030,14 @@ def build_order(arel)
arel.order(*orders) unless orders.empty? arel.order(*orders) unless orders.empty?
end end


VALID_DIRECTIONS = [:asc, :desc, 'asc', 'desc'] # :nodoc: VALID_DIRECTIONS = [:asc, :desc, :ASC, :DESC,
'asc', 'desc', 'ASC', 'DESC'] # :nodoc:


def validate_order_args(args) def validate_order_args(args)
args.grep(Hash) do |h| args.grep(Hash) do |h|

This comment has been minimized.

Copy link
@bogdan

bogdan Mar 5, 2014

Contributor

One more intermediate Array here.
While Aaron is putting hard work to make it adequate ... we are keeping our inadequate staff in master.

args.each do |arg|
  next if arg.is_a?(Hash)
  ....
end

This comment has been minimized.

Copy link
@senny

senny Mar 5, 2014

Author Member

probably next unless ... otherwise that change is pretty inadequate.

This comment has been minimized.

Copy link
@bogdan

bogdan Mar 5, 2014

Contributor

y, sure.

h.values.map(&:downcase).each do |value| h.values.each do |value|

This comment has been minimized.

Copy link
@bogdan

bogdan Mar 5, 2014

Contributor

!!

h.each do |_, value|
  raise ... unless VALID_DIRECTIONS.incude?(value)
end

This comment has been minimized.

Copy link
@senny

senny Mar 5, 2014

Author Member

😓 good catch thanks.

This comment has been minimized.

Copy link
@carlosantoniodasilva

carlosantoniodasilva Mar 5, 2014

Member

Could also use each_value.

raise ArgumentError, "Direction '#{value}' is invalid. Valid " \ raise ArgumentError, "Direction \"#{value}\" is invalid. Valid " \
"directions are asc and desc." unless VALID_DIRECTIONS.include?(value) "directions are: #{VALID_DIRECTIONS.inspect}" unless VALID_DIRECTIONS.include?(value)
end end
end end
end end
Expand Down
32 changes: 11 additions & 21 deletions activerecord/test/cases/relations_test.rb
Expand Up @@ -205,31 +205,21 @@ def test_finding_with_asc_order_with_string
assert_equal [topics(:first), topics(:second), topics(:third), topics(:fourth), topics(:fifth)], topics.to_a assert_equal [topics(:first), topics(:second), topics(:third), topics(:fourth), topics(:fifth)], topics.to_a
end end


def test_nothing_raises_on_upcase_desc_arg def test_support_upper_and_lower_case_directions
Topic.order(id: "DESC") assert_includes Topic.order(id: "ASC").to_sql, "ASC"
end assert_includes Topic.order(id: "asc").to_sql, "ASC"

assert_includes Topic.order(id: :ASC).to_sql, "ASC"
def test_nothing_raises_on_downcase_desc_arg assert_includes Topic.order(id: :asc).to_sql, "ASC"
Topic.order(id: "desc")
end

def test_nothing_raises_on_upcase_asc_arg
Topic.order(id: "ASC")
end

def test_nothing_raises_on_downcase_asc_arg
Topic.order(id: "asc")
end


def test_nothing_raises_on_case_insensitive_args assert_includes Topic.order(id: "DESC").to_sql, "DESC"
Topic.order(id: "DeSc") assert_includes Topic.order(id: "desc").to_sql, "DESC"
Topic.order(id: :DeSc) assert_includes Topic.order(id: :DESC).to_sql, "DESC"
Topic.order(id: "aSc") assert_includes Topic.order(id: :desc).to_sql,"DESC"
Topic.order(id: :aSc)
end end


def test_raising_exception_on_invalid_hash_params def test_raising_exception_on_invalid_hash_params
assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) } e = assert_raise(ArgumentError) { Topic.order(:name, "id DESC", id: :asfsdf) }
assert_equal 'Direction "asfsdf" is invalid. Valid directions are: [:asc, :desc, :ASC, :DESC, "asc", "desc", "ASC", "DESC"]', e.message
end end


def test_finding_last_with_arel_order def test_finding_last_with_arel_order
Expand Down

1 comment on commit f6aeb8b

@senny
Copy link
Member Author

@senny senny commented on f6aeb8b Mar 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.