Skip to content

Commit

Permalink
Update controllers to use ContentItemLoader
Browse files Browse the repository at this point in the history
- Now that ContentItemLoader handles caching we can use that rather than the stuff that the format constraints were putting into the request env.
- LandingPageController's scaffolding is still needed for the moment, but we can remove it soon (since part of this project is about replacing that scaffolding with a more generally useful one).
  • Loading branch information
KludgeKML committed Nov 4, 2024
1 parent 9da8933 commit 8c2506a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 1 addition & 5 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def publication_class
end

def content_item
@content_item ||= ContentItemFactory.build(request.env[:content_item] || request_content_item(content_item_slug || "/#{params[:slug]}"))
@content_item ||= ContentItemFactory.build(ContentItemLoader.load(content_item_slug || "/#{params[:slug]}"))
end

def content_item_slug
Expand All @@ -33,10 +33,6 @@ def content_item_hash
@content_item_hash ||= content_item.to_h
end

def request_content_item(base_path = "/#{params[:slug]}")
GdsApi.content_store.content_item(base_path)
end

# NOTE: Frontend honours the max-age directive provided
# in Content Store's Cache-Control response header.
def set_expiry(expiry = nil)
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/error_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class ErrorController < ApplicationController
def handler
# defer any errors to be handled in ApplicationController
raise request.env[:content_item_error]
# We know at this point that the ContentItemLoader has stored
# an exception to deal with, so just retrieve it and raise it
# to be handled in ApplicationController
raise ContentItemLoader.load(request.path)
end
end
10 changes: 8 additions & 2 deletions app/controllers/landing_page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ class LandingPageController < ContentItemsController

# SCAFFOLDING: can be removed when basic content items are available
# from content-store
def request_content_item(_base_path)
GdsApi.content_store.content_item(request.path).to_h
def content_item
@content_item ||= ContentItemFactory.build(old_scaffolding_content_item)
end

# SCAFFOLDING: can be removed when basic content items are available
# from content-store
def old_scaffolding_content_item
ContentItemLoaderGdsApi.content_store.content_item(request.path).to_h
rescue StandardError
fake_data
end
Expand Down

0 comments on commit 8c2506a

Please sign in to comment.