Skip to content

Commit

Permalink
wip: implement nodes filter
Browse files Browse the repository at this point in the history
The page.all_elements filter is not working yet.
  • Loading branch information
tvdeyen committed Sep 5, 2022
1 parent 95388c6 commit 1c91232
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
24 changes: 20 additions & 4 deletions app/controllers/alchemy/json_api/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,27 @@
module Alchemy
module JsonApi
class NodesController < JsonApi::BaseController
ALLOWED_FILTERS = %i[
page_page_layout
page_elements_name
page_all_elements_name
page_fixed_elements_name
]

def index
@nodes = node_scope.select(:id, :updated_at)
if stale?(last_modified: @nodes.maximum(:updated_at), etag: @nodes)
jsonapi_paginate(node_scope_with_includes) do |paginated|
render jsonapi: paginated
jsonapi_filter(node_scope, ALLOWED_FILTERS) do |filtered_nodes|
@nodes = filtered_nodes.result

puts "===========SQL============"
puts @nodes.to_sql
puts "===========SQL============"

if stale?(last_modified: @nodes.maximum(:updated_at), etag: @nodes)
jsonapi_filter(node_scope_with_includes, ALLOWED_FILTERS) do |nodes|
jsonapi_paginate(nodes.result) do |paginated|
render jsonapi: paginated
end
end
end
end

Expand Down
26 changes: 26 additions & 0 deletions spec/requests/alchemy/json_api/nodes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@
expect(document["included"]).to include(have_type("page"))
expect(document["included"]).to_not include(have_type("element"))
end

context "with 'page_page_layout' filter" do
let(:page2) { FactoryBot.create(:alchemy_page, page_layout: "news") }
let!(:node2) { FactoryBot.create(:alchemy_node, name: "News", page: page2) }

it "includes only matching pages" do
get alchemy_json_api.nodes_path(include: "page", filter: { page_page_layout_eq: "standard" })
document = JSON.parse(response.body)
expect(document["data"]).to include(have_id(node.id.to_s))
expect(document["included"]).to include(have_id(page.id.to_s))
expect(document["included"]).to_not include(have_id(page2.id.to_s))
end
end
end

context "with include param set to 'page.elements'" do
Expand All @@ -65,6 +78,19 @@
expect(document["included"]).to include(have_type("page"))
expect(document["included"]).to include(have_type("element"))
end

context "with 'page_all_elements_name_eq' filter" do
let(:element) { page.elements.first }
let(:element2) { page.elements.last }

it "includes only matching elements" do
get alchemy_json_api.nodes_path(include: "page.all_elements", filter: { page_all_elements_name_eq: "header" })
document = JSON.parse(response.body)
expect(document["data"]).to include(have_id(node.id.to_s))
expect(document["included"]).to include(have_id(element.id.to_s))
expect(document["included"]).to_not include(have_id(element2.id.to_s))
end
end
end

context "with include param set to 'page.all_elements.ingredients,page.all_elements.contents'" do
Expand Down

0 comments on commit 1c91232

Please sign in to comment.