From 0f9780d6060deb833d6db092aaad3f625acf80ed Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 14 Sep 2020 17:50:39 +0200 Subject: [PATCH 1/5] Add a bin/rspec binstub Dev convenience --- bin/rspec | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 bin/rspec diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 0000000..a6c7852 --- /dev/null +++ b/bin/rspec @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rspec' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rspec-core", "rspec") From 63d74421c4074cc23d91f5b14611a7202836afbf Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 14 Sep 2020 17:51:21 +0200 Subject: [PATCH 2/5] Ignore public folder of dummy app --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index ad06c5c..2ec588f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,5 +10,6 @@ spec/dummy/db/development.sqlite3 spec/dummy/db/schema.rb spec/dummy/db/migrate spec/dummy/db/*.sqlite3 +spec/dummy/public Gemfile.lock .ruby-version From 24df6fda23f2c6ddac7b781bff28d0f1410826f3 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 14 Sep 2020 17:51:44 +0200 Subject: [PATCH 3/5] Fix example description of pages request spec --- spec/requests/alchemy/json_api/layout_pages_spec.rb | 2 +- spec/requests/alchemy/json_api/pages_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/requests/alchemy/json_api/layout_pages_spec.rb b/spec/requests/alchemy/json_api/layout_pages_spec.rb index f43d7cf..f2dbb45 100644 --- a/spec/requests/alchemy/json_api/layout_pages_spec.rb +++ b/spec/requests/alchemy/json_api/layout_pages_spec.rb @@ -60,7 +60,7 @@ let!(:non_public_layout_page) { FactoryBot.create(:alchemy_page, :layoutpage) } let!(:public_page) { FactoryBot.create(:alchemy_page, :public) } - it "displays the layoutpage and the public page" do + it "returns only public layout pages" do get alchemy_json_api.layout_pages_path document = JSON.parse(response.body) expect(document["data"]).to include(have_id(layoutpage.id.to_s)) diff --git a/spec/requests/alchemy/json_api/pages_spec.rb b/spec/requests/alchemy/json_api/pages_spec.rb index dc79de1..4929319 100644 --- a/spec/requests/alchemy/json_api/pages_spec.rb +++ b/spec/requests/alchemy/json_api/pages_spec.rb @@ -63,7 +63,7 @@ let!(:non_public_page) { FactoryBot.create(:alchemy_page) } let!(:public_page) { FactoryBot.create(:alchemy_page, :public) } - it "displays the layoutpage and the public page" do + it "returns public content pages only" do get alchemy_json_api.pages_path document = JSON.parse(response.body) expect(document["data"]).not_to include(have_id(layoutpage.id.to_s)) From db00f42b2573e11be99c4a928fcd16a0f19fa71e Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 14 Sep 2020 18:02:15 +0200 Subject: [PATCH 4/5] Add pagination to pages endpoint There might be lots of pages. The default page size is 30 --- .../alchemy/json_api/base_controller.rb | 1 + .../alchemy/json_api/pages_controller.rb | 10 ++++- .../alchemy/json_api/layout_pages_spec.rb | 41 +++++++++++++++---- spec/requests/alchemy/json_api/pages_spec.rb | 41 +++++++++++++++---- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/app/controllers/alchemy/json_api/base_controller.rb b/app/controllers/alchemy/json_api/base_controller.rb index e4955b3..c89399a 100644 --- a/app/controllers/alchemy/json_api/base_controller.rb +++ b/app/controllers/alchemy/json_api/base_controller.rb @@ -6,6 +6,7 @@ class BaseController < ::ApplicationController include JSONAPI::Fetching include JSONAPI::Errors include JSONAPI::Filtering + include JSONAPI::Pagination end end end diff --git a/app/controllers/alchemy/json_api/pages_controller.rb b/app/controllers/alchemy/json_api/pages_controller.rb index 1a0a7c5..42c71fa 100644 --- a/app/controllers/alchemy/json_api/pages_controller.rb +++ b/app/controllers/alchemy/json_api/pages_controller.rb @@ -8,7 +8,9 @@ def index allowed = [:page_layout] jsonapi_filter(page_scope, allowed) do |filtered| - render jsonapi: filtered.result + jsonapi_paginate(filtered.result) do |paginated| + render jsonapi: paginated + end end end @@ -18,6 +20,12 @@ def show private + def jsonapi_meta(pages) + pagination = jsonapi_pagination_meta(pages) + + { pagination: pagination } if pagination.present? + end + def load_page @page = load_page_by_id || load_page_by_urlname || raise(ActiveRecord::RecordNotFound) end diff --git a/spec/requests/alchemy/json_api/layout_pages_spec.rb b/spec/requests/alchemy/json_api/layout_pages_spec.rb index f2dbb45..8169b44 100644 --- a/spec/requests/alchemy/json_api/layout_pages_spec.rb +++ b/spec/requests/alchemy/json_api/layout_pages_spec.rb @@ -56,16 +56,39 @@ end describe "GET /alchemy/json_api/pages" do - let!(:layoutpage) { FactoryBot.create(:alchemy_page, :layoutpage, :public) } - let!(:non_public_layout_page) { FactoryBot.create(:alchemy_page, :layoutpage) } - let!(:public_page) { FactoryBot.create(:alchemy_page, :public) } + context "with contentpages and unpublished layout pages" do + let!(:layoutpage) { FactoryBot.create(:alchemy_page, :layoutpage, :public) } + let!(:non_public_layout_page) { FactoryBot.create(:alchemy_page, :layoutpage) } + let!(:public_page) { FactoryBot.create(:alchemy_page, :public) } - it "returns only public layout pages" do - get alchemy_json_api.layout_pages_path - document = JSON.parse(response.body) - expect(document["data"]).to include(have_id(layoutpage.id.to_s)) - expect(document["data"]).not_to include(have_id(non_public_layout_page.id.to_s)) - expect(document["data"]).not_to include(have_id(public_page.id.to_s)) + it "returns only public layout pages" do + get alchemy_json_api.layout_pages_path + document = JSON.parse(response.body) + expect(document["data"]).to include(have_id(layoutpage.id.to_s)) + expect(document["data"]).not_to include(have_id(non_public_layout_page.id.to_s)) + expect(document["data"]).not_to include(have_id(public_page.id.to_s)) + end + end + + context "with pagination params" do + before do + FactoryBot.create_list(:alchemy_page, 3, :layoutpage, :public) + end + + it "returns paginated result" do + get alchemy_json_api.layout_pages_path(page: { number: 2, size: 1 }) + document = JSON.parse(response.body) + expect(document["data"].length).to eq(1) + expect(document["meta"]).to eq({ + "pagination" => { + "current" => 2, + "first" => 1, + "last" => 3, + "next" => 3, + "prev" => 1, + }, + }) + end end end end diff --git a/spec/requests/alchemy/json_api/pages_spec.rb b/spec/requests/alchemy/json_api/pages_spec.rb index 4929319..46fc370 100644 --- a/spec/requests/alchemy/json_api/pages_spec.rb +++ b/spec/requests/alchemy/json_api/pages_spec.rb @@ -59,16 +59,39 @@ end describe "GET /alchemy/json_api/pages" do - let!(:layoutpage) { FactoryBot.create(:alchemy_page, :layoutpage, :public) } - let!(:non_public_page) { FactoryBot.create(:alchemy_page) } - let!(:public_page) { FactoryBot.create(:alchemy_page, :public) } + context "with layoutpages and unpublished pages" do + let!(:layoutpage) { FactoryBot.create(:alchemy_page, :layoutpage, :public) } + let!(:non_public_page) { FactoryBot.create(:alchemy_page) } + let!(:public_page) { FactoryBot.create(:alchemy_page, :public) } - it "returns public content pages only" do - get alchemy_json_api.pages_path - document = JSON.parse(response.body) - expect(document["data"]).not_to include(have_id(layoutpage.id.to_s)) - expect(document["data"]).not_to include(have_id(non_public_page.id.to_s)) - expect(document["data"]).to include(have_id(public_page.id.to_s)) + it "returns public content pages only" do + get alchemy_json_api.pages_path + document = JSON.parse(response.body) + expect(document["data"]).not_to include(have_id(layoutpage.id.to_s)) + expect(document["data"]).not_to include(have_id(non_public_page.id.to_s)) + expect(document["data"]).to include(have_id(public_page.id.to_s)) + end + end + + context "with pagination params" do + before do + FactoryBot.create_list(:alchemy_page, 3, :public) + end + + it "returns paginated result" do + get alchemy_json_api.pages_path(page: { number: 2, size: 1 }) + document = JSON.parse(response.body) + expect(document["data"].length).to eq(1) + expect(document["meta"]).to eq({ + "pagination" => { + "current" => 2, + "first" => 1, + "last" => 4, + "next" => 3, + "prev" => 1, + }, + }) + end end end end From d2fd7666aca8ebce645d6880ea21fcb19015f720 Mon Sep 17 00:00:00 2001 From: Thomas von Deyen Date: Mon, 14 Sep 2020 18:25:22 +0200 Subject: [PATCH 5/5] Add total to meta of pages result --- app/controllers/alchemy/json_api/pages_controller.rb | 5 ++++- spec/requests/alchemy/json_api/layout_pages_spec.rb | 1 + spec/requests/alchemy/json_api/pages_spec.rb | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/alchemy/json_api/pages_controller.rb b/app/controllers/alchemy/json_api/pages_controller.rb index 42c71fa..ffbda0f 100644 --- a/app/controllers/alchemy/json_api/pages_controller.rb +++ b/app/controllers/alchemy/json_api/pages_controller.rb @@ -23,7 +23,10 @@ def show def jsonapi_meta(pages) pagination = jsonapi_pagination_meta(pages) - { pagination: pagination } if pagination.present? + { + pagination: pagination.presence, + total: page_scope.count + }.compact end def load_page diff --git a/spec/requests/alchemy/json_api/layout_pages_spec.rb b/spec/requests/alchemy/json_api/layout_pages_spec.rb index 8169b44..c48b0fc 100644 --- a/spec/requests/alchemy/json_api/layout_pages_spec.rb +++ b/spec/requests/alchemy/json_api/layout_pages_spec.rb @@ -87,6 +87,7 @@ "next" => 3, "prev" => 1, }, + "total" => 3, }) end end diff --git a/spec/requests/alchemy/json_api/pages_spec.rb b/spec/requests/alchemy/json_api/pages_spec.rb index 46fc370..b2856e9 100644 --- a/spec/requests/alchemy/json_api/pages_spec.rb +++ b/spec/requests/alchemy/json_api/pages_spec.rb @@ -90,6 +90,7 @@ "next" => 3, "prev" => 1, }, + "total" => 4, }) end end