From 0e8932f18823ca60dc97cc39936905d72c04b39f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Inge=20J=C3=B8rgensen?= Date: Mon, 24 Apr 2023 14:57:55 +0200 Subject: [PATCH] Fix RSS specs --- .../concerns/pages_core/rss_controller.rb | 17 +++++++++++++++++ .../pages_core/frontend/pages_controller.rb | 19 +------------------ .../frontend/pages_controller_spec.rb | 9 ++++++++- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/controllers/concerns/pages_core/rss_controller.rb b/app/controllers/concerns/pages_core/rss_controller.rb index 46588da5..6e0ba684 100644 --- a/app/controllers/concerns/pages_core/rss_controller.rb +++ b/app/controllers/concerns/pages_core/rss_controller.rb @@ -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 diff --git a/app/controllers/pages_core/frontend/pages_controller.rb b/app/controllers/pages_core/frontend/pages_controller.rb index 10c1b952..8416adc1 100644 --- a/app/controllers/pages_core/frontend/pages_controller.rb +++ b/app/controllers/pages_core/frontend/pages_controller.rb @@ -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 @@ -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 @@ -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] diff --git a/spec/controllers/pages_core/frontend/pages_controller_spec.rb b/spec/controllers/pages_core/frontend/pages_controller_spec.rb index 291e514a..ee90265b 100644 --- a/spec/controllers/pages_core/frontend/pages_controller_spec.rb +++ b/spec/controllers/pages_core/frontend/pages_controller_spec.rb @@ -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 }