Skip to content

Commit

Permalink
Eager load essence associations in controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Nov 19, 2019
1 parent a703bd9 commit 41fdfcf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
8 changes: 6 additions & 2 deletions app/controllers/alchemy/admin/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,17 @@ def fold
def element_includes
[
{
contents: :essence
contents: {
essence: :ingredient_association
}
},
:tags,
{
all_nested_elements: [
{
contents: :essence
contents: {
essence: :ingredient_association
}
},
:tags
]
Expand Down
14 changes: 12 additions & 2 deletions app/controllers/alchemy/api/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def index
if params[:element_id].present?
@contents = @contents.where(element_id: params[:element_id])
end
@contents = @contents.includes(:essence)
@contents = @contents.includes(*content_includes)

render json: @contents, adapter: :json, root: 'contents'
end
Expand All @@ -37,10 +37,20 @@ def show
@content = Content.where(
element_id: params[:element_id],
name: params[:name]
).includes(:essence).first || raise(ActiveRecord::RecordNotFound)
).includes(*content_includes).first || raise(ActiveRecord::RecordNotFound)
end
authorize! :show, @content
respond_with @content
end

private

def content_includes
[
{
essence: :ingredient_association
}
]
end
end
end
8 changes: 6 additions & 2 deletions app/controllers/alchemy/api/elements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ def element_includes
{
nested_elements: [
{
contents: :essence
contents: {
essence: :ingredient_association
}
},
:tags
]
},
{
contents: :essence
contents: {
essence: :ingredient_association
}
},
:tags
]
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/alchemy/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ def page_includes
{
nested_elements: [
{
contents: :essence
contents: {
essence: :ingredient_association
}
},
:tags
]
},
{
contents: :essence
contents: {
essence: :ingredient_association
}
},
:tags
]
Expand Down

0 comments on commit 41fdfcf

Please sign in to comment.