Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nodes filter #62

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(distinct: true)

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(distinct: true)) 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