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

Admin controller fixes #46

Merged
merged 3 commits into from
Mar 18, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
module Alchemy
module JsonApi
module Admin
class LayoutPagesController < PagesController
class LayoutPagesController < JsonApi::Admin::PagesController
private

def page_scope
page_scope_with_includes(page_version: :draft_version).layoutpages
page_scope_with_includes.layoutpages
end
end
end
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/alchemy/json_api/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@ module Admin
class PagesController < JsonApi::PagesController
prepend_before_action { authorize! :edit_content, Alchemy::Page }

def show
render jsonapi: Alchemy::JsonApi::Page.new(@page, page_version: :draft_version)
end

private

def page_scope
page_scope_with_includes(page_version: :draft_version).contentpages
def page_version
:draft_version
end
end
end
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/alchemy/json_api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ def index

jsonapi_filter(page_scope, allowed) do |filtered|
# decorate with our page model that has a eager loaded elements collection
pages = filtered.result.map { |p| Alchemy::JsonApi::Page.new(p) }
pages = filtered.result.map { |page| api_page(page) }
jsonapi_paginate(pages) do |paginated|
render jsonapi: paginated
end
end
end

def show
render jsonapi: Alchemy::JsonApi::Page.new(@page)
render jsonapi: api_page(@page)
end

private
Expand Down Expand Up @@ -49,7 +49,7 @@ def page_scope
page_scope_with_includes.contentpages
end

def page_scope_with_includes(page_version: :public_version)
def page_scope_with_includes
base_page_scope.
where(language: Language.current).
includes(
Expand All @@ -68,6 +68,14 @@ def page_scope_with_includes(page_version: :public_version)
)
end

def page_version
:public_version
end

def api_page(page)
Alchemy::JsonApi::Page.new(page, page_version: page_version)
end

def base_page_scope
# cancancan is not able to merge our complex AR scopes for logged in users
if can?(:edit_content, ::Alchemy::Page)
Expand Down