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).
- Content items in controllers are now always loaded from `content_item_path`, which defaults to the request path, and is overridden if necessary for items with multiple paths on one content item.
  • Loading branch information
KludgeKML committed Nov 12, 2024
1 parent 9aefe46 commit e0d8c80
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 28 deletions.
4 changes: 4 additions & 0 deletions app/controllers/calendar_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def division

helper_method :calendar

def content_item_path
"/#{params[:slug]}"
end

def set_cors_headers
headers["Access-Control-Allow-Origin"] = "*"
end
Expand Down
8 changes: 2 additions & 6 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,17 @@ 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_path))
end

def content_item_slug
def content_item_path
request.path
end

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
2 changes: 1 addition & 1 deletion app/controllers/electoral_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def publication_class
LocalTransactionPresenter
end

def content_item_slug
def content_item_path
BASE_PATH_OF_EXISTING_CONTACT_LOCAL_ERO_SERVICE
end

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
2 changes: 1 addition & 1 deletion app/controllers/find_local_council_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def result

private

def content_item_slug
def content_item_path
BASE_PATH
end

Expand Down
6 changes: 0 additions & 6 deletions app/controllers/help_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,4 @@ def sign_in
},
]
end

private

def content_item_slug
request.path
end
end
7 changes: 0 additions & 7 deletions app/controllers/help_page_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
class HelpPageController < ContentItemsController
def show; end

private

def content_item_slug
request.path
end
end
13 changes: 10 additions & 3 deletions app/controllers/landing_page_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ 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
rescue StandardError
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
result = ContentItemLoader.load(request.path)
return result.to_h if result.is_a?(GdsApi::Response)

fake_data
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/licence_transaction_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def redirect_to_continuation_licence
end
end

def content_item_slug
def content_item_path
"/find-licences/#{params[:slug]}"
end

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/local_transaction_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def translated_slug
local_authority_slug
end

def content_item_path
"/#{params[:slug]}"
end

def publication_class
LocalTransactionPresenter
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/simple_smart_answers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def flow
:smart_answer_path_for_responses,
)

def content_item_path
"/#{params[:slug]}"
end

def publication_class
SimpleSmartAnswerPresenter
end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/transaction_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def show

private

def content_item_path
"/#{params[:slug]}"
end

def publication_class
TransactionPresenter
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/travel_advice_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def index
# These objects duplicate roles (see `presenter || @publication`) in views.
def publication; end

def content_item_slug
def content_item_path
"/#{FOREIGN_TRAVEL_ADVICE_SLUG}"
end
end

0 comments on commit e0d8c80

Please sign in to comment.