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

Fix page search #71

Merged
merged 1 commit into from
Jul 18, 2023
Merged
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
5 changes: 3 additions & 2 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class PagesController < JsonApi::BaseController
before_action :load_page_for_cache_key, only: :show

def index
allowed = [:page_layout, :urlname]
allowed = Alchemy::Page.ransackable_attributes

jsonapi_filter(page_scope, allowed) do |filtered_pages|
@pages = filtered_pages.result
Expand Down Expand Up @@ -79,6 +79,7 @@ def load_page

def load_page_by_id
return unless params[:path] =~ /\A\d+\z/

page_scope_with_includes.find_by(id: params[:path])
end

Expand All @@ -104,7 +105,7 @@ def page_scope_with_includes
],
},
},
]
],
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/json_api/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Alchemy
# With Ransack 4 we need to define the attributes
# that are allowed to be searched.
def Page.ransackable_attributes(_auth_object = nil)
%w[urlname page_layout]
super | %w[page_layout]
end

module JsonApi
Expand Down
12 changes: 10 additions & 2 deletions spec/requests/alchemy/json_api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,24 @@
context "with filters" do
let!(:standard_page) { FactoryBot.create(:alchemy_page, :public, published_at: 2.weeks.ago) }
let!(:news_page) { FactoryBot.create(:alchemy_page, :public, page_layout: "news", published_at: 1.week.ago) }
let!(:news_page2) { FactoryBot.create(:alchemy_page, :public, page_layout: "news", published_at: Date.yesterday) }
let!(:news_page2) { FactoryBot.create(:alchemy_page, :public, name: "News", page_layout: "news", published_at: Date.yesterday) }

it "returns only matching pages" do
it "returns only matching pages by page_layout" do
get alchemy_json_api.pages_path(filter: { page_layout_eq: "news" })
document = JSON.parse(response.body)
expect(document["data"]).not_to include(have_id(standard_page.id.to_s))
expect(document["data"]).to include(have_id(news_page.id.to_s))
expect(document["data"]).to include(have_id(news_page2.id.to_s))
end

it "returns only matching pages by name" do
get alchemy_json_api.pages_path(filter: { name_eq: "News" })
document = JSON.parse(response.body)
expect(document["data"]).not_to include(have_id(standard_page.id.to_s))
expect(document["data"]).not_to include(have_id(news_page.id.to_s))
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" })
Expand Down