From a164dccc946b8ab3ad1f30241dbee7891f81ebdc Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 24 Sep 2016 13:55:52 +0800 Subject: [PATCH] fix errors!! --- app/controllers/api/v2/restful_controller.rb | 17 +++++++++++------ app/policies/map_policy.rb | 2 +- app/policies/topic_policy.rb | 2 +- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/v2/restful_controller.rb b/app/controllers/api/v2/restful_controller.rb index 74a8f472..f837957d 100644 --- a/app/controllers/api/v2/restful_controller.rb +++ b/app/controllers/api/v2/restful_controller.rb @@ -99,19 +99,24 @@ module Api end def pagination(collection) - @pagination_data ||= { - current_page: (params[:page] || 1).to_i, + return @pagination_data unless @pagination_data.nil? + + current_page = (params[:page] || 1).to_i + per = (params[:per] || 25).to_i + total_pages = (collection.total_count.to_f / per).ceil + @pagination_data = { + current_page: current_page, next_page: current_page < total_pages ? current_page + 1 : 0, prev_page: current_page > 1 ? current_page - 1 : 0, - total_pages: (collection.total_count.to_f / per).ceil, + total_pages: total_pages, total_count: collection.total_count, - per: (params[:per] || 25).to_i + per: per } end def pagination_link_headers!(data) base_url = request.base_url + request.path - old_query = request_query_parameters + old_query = request.query_parameters nxt = old_query.merge(page: data[:next_page]).map { |x| x.join('=') }.join('&') prev = old_query.merge(page: data[:prev_page]).map { |x| x.join('=') }.join('&') last = old_query.merge(page: data[:total_pages]).map { |x| x.join('=') }.join('&') @@ -123,7 +128,7 @@ module Api ].join(',') response.headers['X-Total-Pages'] = data[:total_pages].to_s response.headers['X-Total-Count'] = data[:total_count].to_s - response.headers['X-Per-Page'] = per.to_s + response.headers['X-Per-Page'] = data[:per].to_s end def instantiate_collection diff --git a/app/policies/map_policy.rb b/app/policies/map_policy.rb index b2a04cbe..4cb2db38 100644 --- a/app/policies/map_policy.rb +++ b/app/policies/map_policy.rb @@ -16,7 +16,7 @@ class MapPolicy < ApplicationPolicy end def show? - record.permission.in?('commons', 'public') || + record.permission.in?(['commons', 'public']) || record.collaborators.include?(user) || record.user == user end diff --git a/app/policies/topic_policy.rb b/app/policies/topic_policy.rb index cbde51d8..7bcf585c 100644 --- a/app/policies/topic_policy.rb +++ b/app/policies/topic_policy.rb @@ -23,7 +23,7 @@ class TopicPolicy < ApplicationPolicy if record.defer_to_map.present? map_policy.show? else - record.permission.in?('commons', 'public') || record.user == user + record.permission.in?(['commons', 'public']) || record.user == user end end