Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix since_id/since_time sorting

refs #54
  • Loading branch information...
commit 772369e792748bf55c9e81d82fe789287eb99573 1 parent 2a4bd43
@jvatic jvatic authored
View
14 lib/tentd/model/permissible.rb
@@ -184,7 +184,11 @@ def fetch_all(params, &block)
if params.return_count
with_sql(query.join(' '), *query_bindings).all.first[:count]
else
- with_sql(query.join(' '), *query_bindings).all
+ res = with_sql(query.join(' '), *query_bindings).all
+ if sort_reversed?(params)
+ res.reverse!
+ end
+ res
end
end
@@ -226,7 +230,7 @@ def fetch_with_permissions(params, current_auth, &block)
with_sql(query.join(' '), *query_bindings).all.first[:count]
else
res = with_sql(query.join(' '), *query_bindings).all
- if sort_reversed?(params, sort_direction)
+ if sort_reversed?(params)
res.reverse!
end
res
@@ -236,12 +240,12 @@ def fetch_with_permissions(params, current_auth, &block)
private
- def sort_reversed?(params, sort_direction)
- !params.since_id.nil? && sort_direction.downcase != 'asc'
+ def sort_reversed?(params)
+ params.since_id && params.order.to_s.downcase != 'asc'
end
def get_sort_direction(params)
- if params['order'].to_s.downcase == 'asc' || sort_reversed?(params, 'desc')
+ if params['order'].to_s.downcase == 'asc' || sort_reversed?(params)
'ASC'
else
'DESC'
View
8 lib/tentd/model/permissible_post.rb
@@ -68,6 +68,8 @@ def fetch_all(params, current_auth)
query_conditions << "(#{table_name}.#{sort_column} >= (SELECT #{sort_column} FROM #{table_name} WHERE id = ?) AND #{table_name}.id != ?)"
query_bindings << params.since_id
query_bindings << params.since_id
+
+ _params.since_id = params.since_id
end
if params.before_id
@@ -133,6 +135,8 @@ def fetch_with_permissions(params, current_auth)
query << "AND (#{table_name}.#{sort_column} >= (SELECT #{sort_column} FROM #{table_name} WHERE id = ?) AND #{table_name}.id != ?)"
query_bindings << params.since_id
query_bindings << params.since_id
+
+ _params.since_id = params.since_id
end
if params.before_id
@@ -182,8 +186,8 @@ def fetch_with_permissions(params, current_auth)
end
end
- def sort_reversed?(params, sort_direction)
- super || (!params.since_time.nil? && sort_direction.downcase != 'asc')
+ def sort_reversed?(params)
+ super || (params.since_time && params.order.to_s.downcase != 'asc')
end
def get_sort_column(params)
View
15 spec/integration/api/posts_spec.rb
@@ -913,22 +913,23 @@ def authorize!(*scopes)
end
it "should filter by both since_id and before_id" do
- post1 = Fabricate(:post, :public => post_public?)
- post2 = Fabricate(:post, :public => post_public?)
- post3 = Fabricate(:post, :public => post_public?)
- post4 = Fabricate(:post, :public => post_public?)
+ post1 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 4))
+ post2 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 3))
+ post3 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 2))
+ post4 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 1))
+ post5 = Fabricate(:post, :public => post_public?, :received_at => Time.at(Time.now.to_i - 0))
- with_constants "TentD::API::MAX_PER_PAGE" => 1 do
+ with_constants "TentD::API::MAX_PER_PAGE" => 2 do
params = {
:since_id => post1.public_id,
- :before_id => post4.public_id
+ :before_id => post5.public_id
}
json_get "/posts", params, env
expect(last_response.status).to eql(200)
body = Yajl::Parser.parse(last_response.body)
ids = body.map { |i| i['id'] }
- expect(ids).to eql([post2.public_id])
+ expect(ids).to eql([post3.public_id, post2.public_id])
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.