From 864cea2fe810c89c1ce792d2c56e8212f1d4e10d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Inge=20J=C3=B8rgensen?= Date: Mon, 24 Apr 2023 14:43:11 +0200 Subject: [PATCH] RSS pagination --- .../concerns/pages_core/rss_controller.rb | 1 - .../pages_core/frontend/pages_controller.rb | 23 +++++++++++++++---- app/views/feeds/pages.rss.builder | 4 +--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/controllers/concerns/pages_core/rss_controller.rb b/app/controllers/concerns/pages_core/rss_controller.rb index 474dfda5..46588da5 100644 --- a/app/controllers/concerns/pages_core/rss_controller.rb +++ b/app/controllers/concerns/pages_core/rss_controller.rb @@ -11,7 +11,6 @@ def all_feed_items Page.where(parent_page_id: feeds) .order("published_at DESC") .published - .limit(20) .localized(locale) end diff --git a/app/controllers/pages_core/frontend/pages_controller.rb b/app/controllers/pages_core/frontend/pages_controller.rb index cb42abbe..10c1b952 100644 --- a/app/controllers/pages_core/frontend/pages_controller.rb +++ b/app/controllers/pages_core/frontend/pages_controller.rb @@ -20,7 +20,10 @@ class PagesController < ::FrontendController def index respond_to do |format| format.html { render_published_page(root_pages.try(&:first)) } - format.rss { render_rss(all_feed_items) } + format.rss do + render_rss(all_feed_items.paginate(per_page: per_page_param, + page: page_param)) + end end end @@ -28,7 +31,7 @@ def show respond_to do |format| format.html { render_published_page(@page) } format.json { render json: PageResource.new(@page) } - format.rss { render_page_rss(@page) } + format.rss { render_page_rss(@page, page_param) } end end @@ -61,6 +64,16 @@ def redirect_to(*args) super end + def per_page_param(default = 20, max = 1000) + return default unless params[:per_page].is_a?(String) + + params[:per_page].to_i.clamp(1, max) + end + + def page_param + params[:page].is_a?(String) ? params[:page] : 1 + end + def page_template(page) if PagesCore::Templates.names.include?(page.template) page.template @@ -83,9 +96,11 @@ def render_page render template: "pages/templates/#{template}" end - def render_page_rss(page) + def render_page_rss(page, pagination_page = 1) if page.feed_enabled? - render_rss(page.pages.limit(20).includes(:image, :author), + render_rss(page.pages.paginate(per_page: per_page_param, + page: pagination_page) + .includes(:image, :author), title: page.name) else render_error 404 diff --git a/app/views/feeds/pages.rss.builder b/app/views/feeds/pages.rss.builder index 88e933c8..f65cf4d9 100644 --- a/app/views/feeds/pages.rss.builder +++ b/app/views/feeds/pages.rss.builder @@ -4,9 +4,7 @@ xml << "\n" xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do xml.channel do xml.title(@title || PagesCore.config(:site_name)) - xml.link( - url_for(controller: "pages", action: "index", only_path: false) - ) + xml.link(root_url) xml.description "Recent items" xml.language locale xml.generator "Pages"