Skip to content

Commit

Permalink
RSS pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronaut committed Apr 24, 2023
1 parent c178272 commit 864cea2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
1 change: 0 additions & 1 deletion app/controllers/concerns/pages_core/rss_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def all_feed_items
Page.where(parent_page_id: feeds)
.order("published_at DESC")
.published
.limit(20)
.localized(locale)
end

Expand Down
23 changes: 19 additions & 4 deletions app/controllers/pages_core/frontend/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ 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

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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 1 addition & 3 deletions app/views/feeds/pages.rss.builder
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ xml << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\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"
Expand Down

0 comments on commit 864cea2

Please sign in to comment.