Skip to content

Commit

Permalink
Fix RSS specs
Browse files Browse the repository at this point in the history
  • Loading branch information
elektronaut committed Apr 24, 2023
1 parent 864cea2 commit 0e8932f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
17 changes: 17 additions & 0 deletions app/controllers/concerns/pages_core/rss_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ def all_feed_items
.localized(locale)
end

def per_page_rss_param(default = 20, max = 1000)
return default unless params[:per_page].is_a?(String)

params[:per_page].to_i.clamp(1, max)
end

def render_page_rss(page, pagination_page = 1)
if page.feed_enabled?
render_rss(page.pages.paginate(per_page: per_page_rss_param,
page: pagination_page)
.includes(:image, :author),
title: page.name)
else
render_error 404
end
end

def render_rss(items, title: nil)
@title = PagesCore.config.site_name
@title += ": #{title}" if title
Expand Down
19 changes: 1 addition & 18 deletions app/controllers/pages_core/frontend/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def index
respond_to do |format|
format.html { render_published_page(root_pages.try(&:first)) }
format.rss do
render_rss(all_feed_items.paginate(per_page: per_page_param,
render_rss(all_feed_items.paginate(per_page: per_page_rss_param,
page: page_param))
end
end
Expand Down Expand Up @@ -64,12 +64,6 @@ 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
Expand All @@ -96,17 +90,6 @@ def render_page
render template: "pages/templates/#{template}"
end

def render_page_rss(page, pagination_page = 1)
if page.feed_enabled?
render_rss(page.pages.paginate(per_page: per_page_param,
page: pagination_page)
.includes(:image, :author),
title: page.name)
else
render_error 404
end
end

def find_page_by_path
return unless params[:path]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@
it { is_expected.to render_template("pages/templates/index") }
end

describe "RSS rendering" do
describe "RSS rendering when page is feed enabled" do
let(:page) { create(:page, name: "Home", feed_enabled: true) }
let(:params) { { path: page.path_segment, format: :rss } }

it { is_expected.to render_template("feeds/pages") }
end

describe "RSS rendering when page isn't feed enabled" do
let(:params) { { path: page.path_segment, format: :rss } }

specify { expect(response).to have_http_status(:not_found) }
end

describe "JSON rendering" do
let(:params) { { path: page.path_segment, format: :json } }
let(:json) { response.parsed_body }
Expand Down

0 comments on commit 0e8932f

Please sign in to comment.