Skip to content

Commit

Permalink
Merge pull request #4221 from alphagov/content-item-model
Browse files Browse the repository at this point in the history
Convert content_item from simple response to Model
  • Loading branch information
KludgeKML authored Sep 5, 2024
2 parents dc4049c + c8c03ab commit 332a1e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 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 ||= request.env[:content_item] || request_content_item(content_item_slug || "/#{params[:slug]}")
@content_item ||= ContentItemFactory.build(request.env[:content_item] || request_content_item(content_item_slug || "/#{params[:slug]}"))
end

def content_item_slug
Expand Down Expand Up @@ -53,8 +53,8 @@ def set_expiry(expiry = nil)
end

def set_locale
I18n.locale = if content_item["locale"] && I18n.available_locales.map(&:to_s).include?(content_item["locale"])
content_item["locale"]
I18n.locale = if content_item.locale && I18n.available_locales.map(&:to_s).include?(content_item.locale)
content_item.locale
else
I18n.default_locale
end
Expand Down
17 changes: 17 additions & 0 deletions app/models/content_item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ContentItem
attr_reader :content_store_response, :body, :image, :description, :document_type, :title, :base_path, :locale

def initialize(content_store_response)
@content_store_response = content_store_response
@body = content_store_response.dig("details", "body")
@image = content_store_response.dig("details", "image")
@description = content_store_response["description"]
@document_type = content_store_response["document_type"]
@title = content_store_response["title"]
@base_path = content_store_response["base_path"]
@locale = content_store_response["locale"]
end

delegate :to_h, to: :content_store_response
delegate :cache_control, to: :content_store_response
end
9 changes: 9 additions & 0 deletions app/models/content_item_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ContentItemFactory
def self.build(content_hash)
content_item_class(content_hash).new(content_hash)
end

def self.content_item_class(_content_hash)
ContentItem
end
end

0 comments on commit 332a1e4

Please sign in to comment.