Skip to content
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

Share view context with presenters to access helpers #1752

Merged
merged 3 commits into from
May 1, 2020

Conversation

kevindew
Copy link
Member

This changes the way presenters work in this application so that they
have the equivalent of a view object available to call helpers on. This
removes the need to import specific helpers into individual presenters.

The reason for adding this in is to fix a number of subtle issues that
can occur when it comes to generating URLs without any request
context. This problem affected me when I went to looking at adjusting
the asset host in order to make progress towards RFC-115 1.

I found that we'd experience different behaviours on asset url methods
depending on the GOVUK_ASSET_ROOT env var. When this env var is not set,
as per 2, any calls for an asset_url would return a path rather than
URL like we requested; when a GOVUK_ASSET_ROOT env var is set as a
hostname without protocol, as per 3, any calls for asset urls would
raise a "undefined method protocol for nil:NilClass" exception at 4.
These problems don't affect production since GOVUK_ASSET_ROOT is set as
a full URL.

Anyway, I came to the conclusion that these presenters are effectively
just extensions of the view - evidenced by the high proliferation of
view helpers embedded - and I felt that it'd be easier to have a
view_context object available to call any view helpers we might want.
This helps us out when it comes to rendering asset_urls as this view
context object has the request context available and can determine the
URL based on the request.

Were we to use Rails route helpers for navigating the app we'd also be
able to drop the various usages of Plek.current.website_root and instead
rely on view context to generate URLs.

@bevanloon bevanloon temporarily deployed to government-f-use-contex-m7yr44 April 29, 2020 22:00 Inactive
kevindew added 3 commits May 1, 2020 11:24
This adds a new method in, create_presenter, which has default test
arguments set for presenters. This then updates all of the presenter
tests so that they all use this helper.

The reason for this change is that I intend to add a third argument soon to
presenter classes which would mean an increase in test verbosity. I also
think this improves consistency of these tests.
This shortens this variable name and I believe it still signifies the
same intent as the previous name. The optional nature of it is also
removed since every time this class is initialised in app this attribute
is set, I imagine this was set as optional for test verbosity but this
is no longer an issue.

This also fixes a couple of ivars that weren't necessary due to methods
existing.
This changes the way presenters work in this application so that they
have the equivalent of a view object available to call helpers on. This
removes the need to import specific helpers into individual presenters.

The reason for adding this in is to fix a number of subtle issues that
can occur when it comes to generating URLs without any request
context. This problem affected me when I went to looking at adjusting
the asset host in order to make progress towards RFC-115 [1].

I found that we'd experience different behaviours on asset url methods
depending on the GOVUK_ASSET_ROOT env var. When this env var is not set,
as per [2], any calls for an asset_url would return a path rather than
URL like we requested; when a GOVUK_ASSET_ROOT env var is set as a
hostname without protocol, as per [3], any calls for asset urls would
raise a "undefined method `protocol` for nil:NilClass" exception at [4].
These problems don't affect production since GOVUK_ASSET_ROOT is set as
a full URL.

Anyway, I came to the conclusion that these presenters are effectively
just extensions of the view - evidenced by the high proliferation of
view helpers embedded - and I felt that it'd be easier to have a
view_context object available to call any view helpers we might want.
This helps us out when it comes to rendering asset_urls as this view
context object has the request context available and can determine the
URL based on the request.

Were we to use Rails route helpers for navigating the app we'd also be
able to drop the various usages of Plek.current.website_root and instead
rely on view context to generate URLs.

[1]: https://github.com/alphagov/govuk-rfcs/blob/master/rfc-115-enabling-http2-on-govuk.md
[2]: https://github.com/alphagov/govuk-docker/blob/4b40e479799ea304709276efb29c49898a8b8c9f/projects/government-frontend/docker-compose.yml#L48-L57
[3]: https://github.com/alphagov/govuk-docker/blob/4b40e479799ea304709276efb29c49898a8b8c9f/projects/government-frontend/docker-compose.yml#L20-L30
[4]: https://github.com/rails/rails/blob/92d03850f3bb4c44103d0b06b43e14d6e270e646/actionview/lib/action_view/helpers/asset_url_helper.rb#L303
@kevindew kevindew force-pushed the use-contextual-helpers branch from a75d7c4 to c57012c Compare May 1, 2020 10:27
@bevanloon bevanloon temporarily deployed to government-f-use-contex-m7yr44 May 1, 2020 10:27 Inactive
@kevindew kevindew merged commit 2a09d4d into master May 1, 2020
@kevindew kevindew deleted the use-contextual-helpers branch May 1, 2020 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants