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

Refactor body_does_not_contain_lead_image validator #8448

Merged
merged 1 commit into from
Nov 3, 2023

Conversation

ollietreend
Copy link
Contributor

This commit refactors the body_does_not_contain_lead_image custom validation method to reduce its coupling to Govspeak.

Right now it uses regular expressions to check if the lead image has been embedded into the document body. This relies upon the validator knowing the specific Govspeak syntax that would be used to embed the image (of which there are two different variants).

This refactor takes a more 'black box' approach by rendering the Govspeak as HTML, and then inspecting the output to see if it contains the lead image. This way the validator doesn't need to parse the Govspeak syntax directly – making it more robust against future changes to the syntax.

I imagine this new approach is slightly less performant. It now renders and inspects the resultant HTML, rather than simply matching a regex against the raw Govspeak. However I expect this will have negligible overall impact on performance, so would be a net benefit given the improvements to code readability and cleanliness.


⚠️ This repo is Continuously Deployed: make sure you follow the guidance ⚠️

Follow these steps if you are doing a Rails upgrade.

@ollietreend ollietreend force-pushed the refactor-lead-image-validator branch from 18021bc to a3d5bb3 Compare November 2, 2023 17:22
This commit refactors the `body_does_not_contain_lead_image` custom
validation method to reduce its coupling to Govspeak.

Right now it uses regular expressions to check if the lead image has
been embedded into the document body. This relies upon the validator
knowing the specific Govspeak syntax that would be used to embed the
image (of which there are two different variants).

This refactor takes a more 'black box' approach by rendering the
Govspeak as HTML, and then inspecting the output to see if it contains
the lead image. This way the validator doesn't need to parse the
Govspeak syntax directly – making it more robust against future changes
to the syntax.

I imagine this new approach is slightly less performant. It now renders
and inspects the resultant HTML, rather than simply matching a regex
against the raw Govspeak. However I expect this will have negligible
overall impact on performance, so would be a net benefit given the
improvements to code readability and cleanliness.
@ollietreend ollietreend force-pushed the refactor-lead-image-validator branch from a3d5bb3 to 1a7a425 Compare November 2, 2023 17:32
@ollietreend ollietreend marked this pull request as ready for review November 2, 2023 17:41
def stubbed_image(name)
build(:image).tap do |image|
image.stubs(:filename).returns(name)
image.stubs(:url).returns("http://example.com/#{name}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to stub this out? Can we not just create an image with the filename we want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried avoiding a stub in the first place, but it looks like the Image and ImageData factories have been changed with the recent work done by the Assets team.

From what I can tell, the factory can only create an SVG image or a JPEG image. And the filename isn't configurable – it seems like the JPEG is hardcoded as minister-of-funk.960x640.jpg and the SVG is test-svg.svg.

Really I want to create two 'normal' images (e.g. not SVGs, because we know they sometimes get treated differently) that have different filenames.

So I figured this stub was an easy way to avoid changing how that factory works. (Perhaps that's a bit lazy of me!?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea i've seen quite a lot of stuff that's not used the factories which probably isn't ideal (if you search for build(:image_data) you'll see what i mean. This seems fine to me though.

Copy link
Contributor

@davidgisbey davidgisbey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me. Seems like a better approach to test the output itself to me 👍

@ollietreend ollietreend merged commit 92014a0 into main Nov 3, 2023
15 checks passed
@ollietreend ollietreend deleted the refactor-lead-image-validator branch November 3, 2023 09:27
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.

2 participants