Commit a7b2bd7694dc3fbcc2004dbffacddb9a44ad3e3e
Committed by
Rodrigo Souto
1 parent
b8374af3
Exists in
master
and in
29 other branches
api: accept order parameter
Showing
1 changed file
with
9 additions
and
5 deletions
Show diff stats
lib/noosfero/api/helpers.rb
@@ -67,15 +67,19 @@ module Noosfero | @@ -67,15 +67,19 @@ module Noosfero | ||
67 | 67 | ||
68 | conditions | 68 | conditions |
69 | end | 69 | end |
70 | - | ||
71 | - | 70 | + |
71 | + def make_order_with_parameters(params) | ||
72 | + params[:order] || "created_at DESC" | ||
73 | + end | ||
74 | + | ||
72 | def select_filtered_collection_of(object, method, params) | 75 | def select_filtered_collection_of(object, method, params) |
73 | conditions = make_conditions_with_parameter(params) | 76 | conditions = make_conditions_with_parameter(params) |
74 | - | 77 | + order = make_order_with_parameters(params) |
78 | + | ||
75 | if params[:reference_id] | 79 | if params[:reference_id] |
76 | - objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).where(conditions).limit(limit).order("created_at DESC") | 80 | + objects = object.send(method).send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).where(conditions).limit(limit).order(order) |
77 | else | 81 | else |
78 | - objects = object.send(method).where(conditions).limit(limit).order("created_at DESC") | 82 | + objects = object.send(method).where(conditions).limit(limit).order(order) |
79 | end | 83 | end |
80 | objects | 84 | objects |
81 | end | 85 | end |