diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rb index 403adb5d1aa..40ae4976dff 100644 --- a/app/controllers/list_controller.rb +++ b/app/controllers/list_controller.rb @@ -46,7 +46,8 @@ class ListController < ApplicationController end list = TopicQuery.new(user, list_opts).public_send("list_#{filter}") - list.more_topics_url = construct_url_with(list_opts) + list.more_topics_url = construct_next_url_with(list_opts) + list.prev_topics_url = construct_prev_url_with(list_opts) if Discourse.anonymous_filters.include?(filter) @description = SiteSetting.site_description @rss = filter @@ -92,8 +93,8 @@ class ListController < ApplicationController guardian.ensure_can_see_private_messages!(target_user.id) unless action == :topics_by list = generate_list_for(action.to_s, target_user, list_opts) url_prefix = "topics" unless action == :topics_by - url = construct_url_with(list_opts, url_prefix) - list.more_topics_url = url_for(url) + list.more_topics_url = url_for(construct_next_url_with(list_opts, url_prefix)) + list.prev_topics_url = url_for(construct_prev_url_with(list_opts, url_prefix)) respond(list) end end @@ -163,7 +164,8 @@ class ListController < ApplicationController top_options[:per_page] = SiteSetting.topics_per_period_in_top_page user = list_target_user list = TopicQuery.new(user, top_options).public_send("list_top_#{period}") - list.more_topics_url = construct_url_with(top_options) + list.more_topics_url = construct_next_url_with(top_options) + list.prev_topics_url = construct_prev_url_with(top_options) respond(list) end @@ -202,8 +204,19 @@ class ListController < ApplicationController end def next_page_params(opts = nil) + page_params(opts).merge(page: params[:page].to_i + 1) + end + + def prev_page_params(opts = nil) + page_params(opts).merge(page: params[:page].to_i > 1 ? (params[:page].to_i - 1) : 0) + end + + + private + + def page_params(opts = nil) opts ||= {} - route_params = { format: 'json', page: params[:page].to_i + 1 } + route_params = {format: 'json'} route_params[:category] = @category.slug if @category route_params[:parent_category] = @category.parent_category.slug if @category && @category.parent_category route_params[:sort_order] = opts[:sort_order] if opts[:sort_order].present? @@ -211,8 +224,6 @@ class ListController < ApplicationController route_params end - private - def set_category slug_or_id = params.fetch(:category) parent_slug_or_id = params[:parent_category] @@ -269,11 +280,16 @@ class ListController < ApplicationController TopicQuery.new(current_user, opts).send("list_#{action}", target_user) end - def construct_url_with(opts, url_prefix = nil) + def construct_next_url_with(opts, url_prefix = nil) method = url_prefix.blank? ? "#{action_name}_path" : "#{url_prefix}_#{action_name}_path" public_send(method, opts.merge(next_page_params(opts))) end + def construct_prev_url_with(opts, url_prefix = nil) + method = url_prefix.blank? ? "#{action_name}_path" : "#{url_prefix}_#{action_name}_path" + public_send(method, opts.merge(prev_page_params(opts))) + end + def generate_top_lists(options) top = TopLists.new diff --git a/app/models/topic_list.rb b/app/models/topic_list.rb index 715468b969f..54d847d3653 100644 --- a/app/models/topic_list.rb +++ b/app/models/topic_list.rb @@ -4,6 +4,7 @@ class TopicList include ActiveModel::Serialization attr_accessor :more_topics_url, + :prev_topics_url, :draft, :draft_key, :draft_sequence, diff --git a/app/views/list/list.erb b/app/views/list/list.erb index 5182baac4c2..d10f5cd9133 100644 --- a/app/views/list/list.erb +++ b/app/views/list/list.erb @@ -4,9 +4,15 @@ <% end %> -<% if @list.topics.length > 0 %> -
-<% end %> ++ <% if params[:page].to_i > 1 %> + <%= t 'prev_page'%> + <% end %> + + <% if @list.topics.length > 0 %> + <%= t 'next_page'%> + <% end %> +
<%= t 'powered_by_html' %>
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 0a602c86269..1d1bd18f482 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -76,6 +76,7 @@ en: invalid_characters: "contains invalid characters" is_invalid: "is invalid; try to be a little more descriptive" next_page: "next page →" + prev_page: "← previous page" page_num: "Page %{num}" by: "By" topics_in_category: "Topics in the '%{category}' category"