Skip to content

Commit

Permalink
Merge pull request #883 from d-m-u/add_aliases_asc_desc
Browse files Browse the repository at this point in the history
allow specification of sort_order with abbreviation or full form
  • Loading branch information
abellotti authored Aug 25, 2020
2 parents 68dc9a2 + 41f8998 commit f13a3b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
5 changes: 2 additions & 3 deletions app/controllers/api/base_controller/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class BaseController
module Parameters
module ResultsController
def sort_order
params['sort_order'] == 'desc' ? :descending : :ascending
%w[desc descending].include?(params['sort_order']) ? :descending : :ascending
end

def param_result_set?
Expand Down Expand Up @@ -93,8 +93,7 @@ def sort_directive(klass, attr, order, options)
arel = klass.arel_attribute(attr)
if order
arel = arel.lower if options.map(&:downcase).include?("ignore_case")
arel = arel.desc if order.downcase == "desc"
arel = arel.asc if order.downcase == "asc"
arel = %w[desc descending].include?(order.downcase) ? arel.desc : arel.asc
else
arel = arel.asc
end
Expand Down
24 changes: 14 additions & 10 deletions spec/requests/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,22 +245,26 @@ def create_vms_by_name(names)
describe "Sorting vms by attribute" do
before { api_basic_authorize collection_action_identifier(:vms, :read, :get) }

it "supports ascending order" do
create_vms_by_name %w(cc aa bb)
%w[asc ascending].each do |sort_order|
it "orders with sort_order of #{sort_order}" do
create_vms_by_name %w[cc aa bb]

get api_vms_url, :params => { :sort_by => "name", :sort_order => "asc", :expand => "resources" }
get api_vms_url, :params => {:sort_by => "name", :sort_order => sort_order, :expand => "resources"}

expect_query_result(:vms, 3, 3)
expect_result_resources_to_match_hash([{"name" => "aa"}, {"name" => "bb"}, {"name" => "cc"}])
expect_query_result(:vms, 3, 3)
expect_result_resources_to_match_hash([{"name" => "aa"}, {"name" => "bb"}, {"name" => "cc"}])
end
end

it "supports decending order" do
create_vms_by_name %w(cc aa bb)
%w[desc descending].each do |sort_order|
it "orders with sort_order of #{sort_order}" do
create_vms_by_name %w[cc aa bb]

get api_vms_url, :params => { :sort_by => "name", :sort_order => "desc", :expand => "resources" }
get api_vms_url, :params => {:sort_by => "name", :sort_order => sort_order, :expand => "resources"}

expect_query_result(:vms, 3, 3)
expect_result_resources_to_match_hash([{"name" => "cc"}, {"name" => "bb"}, {"name" => "aa"}])
expect_query_result(:vms, 3, 3)
expect_result_resources_to_match_hash([{"name" => "cc"}, {"name" => "bb"}, {"name" => "aa"}])
end
end

it "supports case insensitive ordering" do
Expand Down

0 comments on commit f13a3b3

Please sign in to comment.