-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local content item support #4366
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d49f2d
Document Local Content Item support
KludgeKML b78bb9d
Add ContentItemLoader class and tests
KludgeKML 28e39bd
Add a helper to all tests that resets the ContentItemLoader cache
KludgeKML 80c3e74
Update routing constraints to use ContentItemLoader
KludgeKML 50e0ee1
Update controllers to use ContentItemLoader
KludgeKML 41e22f2
Add ALLOW_LOCAL_CONTENT_ITEM_OVERRIDE=true to heroku config and start…
KludgeKML File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,7 @@ def result | |
|
||
private | ||
|
||
def content_item_slug | ||
def content_item_path | ||
BASE_PATH | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,4 @@ def sign_in | |
}, | ||
] | ||
end | ||
|
||
private | ||
|
||
def content_item_slug | ||
request.path | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Local Content Items | ||
|
||
For local development and preview apps, it is sometimes desirable to have the ability to load content items that are specified locally rather than pointing to production/integration or having the content store running locally. | ||
|
||
To support this, set: | ||
|
||
`ALLOW_LOCAL_CONTENT_ITEM_OVERRIDE=true` | ||
leenagupte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
...in your environment. | ||
|
||
With that environment variable set, the ContentItemLoader will add an additional check when loading a content item from the content store. Before making the GdsApi content store call, it will look in config/local-content-items/ for a JSON or YAML file matching the path of the content item (for instance, if you're looking for /find-licences/my-licence, it will look for config/local-content-items/find-licences/my-licence.json, then config/local-content-items/find-licences/my-licence.yml) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
class ApiErrorRoutingConstraint | ||
def matches?(request) | ||
request.env[:content_item_error].present? | ||
ContentItemLoader.load(request.path).is_a?(StandardError) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
require "ostruct" | ||
|
||
class ContentItemLoader | ||
LOCAL_ITEMS_PATH = "lib/data/local-content-items".freeze | ||
|
||
@cache = {} | ||
|
||
class << self | ||
attr_reader :cache | ||
|
||
def load(base_path) | ||
cache[base_path] ||= if use_local_file? && File.exist?(yaml_filename(base_path)) | ||
Rails.logger.debug("Loading content item #{base_path} from #{yaml_filename(base_path)}") | ||
load_yaml_file(base_path) | ||
elsif use_local_file? && File.exist?(json_filename(base_path)) | ||
Rails.logger.debug("Loading content item #{base_path} from #{json_filename(base_path)}") | ||
load_json_file(base_path) | ||
else | ||
begin | ||
GdsApi.content_store.content_item(base_path) | ||
rescue GdsApi::HTTPErrorResponse, GdsApi::InvalidUrl => e | ||
e | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def use_local_file? | ||
ENV["ALLOW_LOCAL_CONTENT_ITEM_OVERRIDE"] == "true" | ||
end | ||
|
||
def yaml_filename(base_path) | ||
Rails.root.join("#{LOCAL_ITEMS_PATH}#{base_path}.yml") | ||
end | ||
|
||
def load_yaml_file(base_path) | ||
GdsApi::Response.new(OpenStruct.new(code: 200, body: YAML.load(File.read(yaml_filename(base_path))).to_json, headers:)) | ||
end | ||
|
||
def json_filename(base_path) | ||
Rails.root.join("#{LOCAL_ITEMS_PATH}#{base_path}.json") | ||
end | ||
|
||
def load_json_file(base_path) | ||
GdsApi::Response.new(OpenStruct.new(code: 200, body: File.read(json_filename(base_path)), headers:)) | ||
end | ||
|
||
def headers | ||
{ cache_control: "max-age=0, public", expires: "" } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"schema_name": "json_page" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
schema_name: yaml_page |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module ContentItemLoaderHelpers | ||
def clear_content_item_loader_cache | ||
ContentItemLoader.cache.each_key { |key| ContentItemLoader.cache.delete(key) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,18 @@ | ||
require "api_error_routing_constraint" | ||
|
||
RSpec.describe ApiErrorRoutingConstraint do | ||
include ContentStoreHelpers | ||
|
||
let(:subject) { described_class.new } | ||
let(:request) { double(path: "/slug") } | ||
|
||
it "returns true if there's a cached error" do | ||
request = double(env: { content_item_error: StandardError.new }) | ||
stub_content_store_does_not_have_item("/slug") | ||
|
||
expect(subject.matches?(request)).to be true | ||
end | ||
|
||
it "returns false if there was no error in API calls" do | ||
request = double(env: {}) | ||
stub_content_store_has_item("/slug") | ||
|
||
expect(subject.matches?(request)).to be false | ||
end | ||
|
||
private | ||
|
||
def subject | ||
ApiErrorRoutingConstraint.new | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's great that you've fixed this. I think it would be good to add a test for this scaffolding too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The good news is that the existing request tests for landing_page already cover the old scaffolding (tests written... checks blame ... one @leenagupte)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can't have been thorough enough because nothing caught the attempted call to
ContentItemLoaderGdsApi.content_store.content_item(request.path).to_h
which doesn't exist!