Skip to content

Commit

Permalink
API: Eager load page associations
Browse files Browse the repository at this point in the history
In order to prevent N+1 queries in the pages API we
eager load all necessary associations
  • Loading branch information
tvdeyen committed Nov 15, 2019
1 parent feaa50f commit 5b2f6dd
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions app/controllers/alchemy/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def index
else
@pages = Page.accessible_by(current_ability, :index)
end
@pages = @pages.includes(*page_includes)
if params[:page_layout].present?
Alchemy::Deprecation.warn <<~WARN
Passing page_layout parameter to Alchemy::Api::PagesController#index is deprecated.
Expand Down Expand Up @@ -91,5 +92,23 @@ def per_page_value
def page_value
params[:page] ? params[:page].to_i : nil
end

def page_includes
[
:tags,
{
elements: [
{
nested_elements: [
:contents,
:tags
]
},
:contents,
:tags
]
}
]
end
end
end

0 comments on commit 5b2f6dd

Please sign in to comment.