Skip to content

Commit

Permalink
Fix page search
Browse files Browse the repository at this point in the history
We overwrote ransackable attributes to only include page_layout
and urlname, but Alchemy needs name as well. Fixed by adding
page_layout to already defined ransackable attributes.
  • Loading branch information
tvdeyen committed Jul 18, 2023
1 parent f851a37 commit 7635b71
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
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

0 comments on commit 7635b71

Please sign in to comment.