Skip to content

Commit

Permalink
Merge pull request #2169 from adnotam/filter_current_site_pages
Browse files Browse the repository at this point in the history
Return only pages from current site in api
  • Loading branch information
tvdeyen authored Aug 20, 2021
2 parents 53ef8f0 + 95f5c3e commit 504c5bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/controllers/alchemy/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class Api::PagesController < Api::BaseController
#
def index
# Fix for cancancan not able to merge multiple AR scopes for logged in users
if can? :edit_content, Alchemy::Page
@pages = Page.all
if cannot? :edit_content, Alchemy::Page
@pages = Alchemy::Page.accessible_by(current_ability, :index)
@pages = @pages.where(language: Language.current)
else
@pages = Page.accessible_by(current_ability, :index)
@pages = Language.current&.pages.presence || Alchemy::Page.none
end
@pages = @pages.includes(*page_includes)
@pages = @pages.ransack(params[:q]).result
Expand Down
12 changes: 12 additions & 0 deletions spec/controllers/alchemy/api/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ module Alchemy
expect(result["pages"].size).to eq(1)
end
end

context "with multiple sites" do
let(:site_2) { create(:alchemy_site) }
let(:language_2) { create(:alchemy_language, site: site_2) }
let!(:site_2_page) { create(:alchemy_page, :public, language: language_2) }

it "only returns pages for current site" do
get :index, format: :json

expect(result["pages"].map { |r| r["id"] }).to_not include(site_2_page.id)
end
end
end

describe "#nested" do
Expand Down

0 comments on commit 504c5bf

Please sign in to comment.