Skip to content

Commit

Permalink
Merge pull request #56 from AlchemyCMS/fix-error-for-empty-results
Browse files Browse the repository at this point in the history
Do not throw error if filter returns empty result
  • Loading branch information
tvdeyen authored Oct 28, 2021
2 parents bf84118 + 3ca25f9 commit 503c058
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index

jsonapi_filter(page_scope, allowed) do |filtered_pages|
@pages = filtered_pages.result
if stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key).cache_key)
if stale?(last_modified: @pages.maximum(:published_at), etag: @pages.max_by(&:cache_key)&.cache_key)
# Only load pages with all includes when browser cache is stale
jsonapi_filter(page_scope_with_includes, allowed) do |filtered|
# decorate with our page model that has a eager loaded elements collection
Expand Down
7 changes: 7 additions & 0 deletions spec/requests/alchemy/json_api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@
expect(document["data"]).to include(have_id(news_page2.id.to_s))
end

context "if no pages returned for filter params" do
it "does not throw error" do
get alchemy_json_api.pages_path(filter: { page_layout_eq: "not-found" })
expect(response).to be_successful
end
end

it "sets cache headers of latest matching page" do
get alchemy_json_api.pages_path(filter: { page_layout_eq: "news" })
expect(response.headers["Last-Modified"]).to eq(news_page2.published_at.utc.httpdate)
Expand Down

0 comments on commit 503c058

Please sign in to comment.