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

Make Contacts feature work and be more user friendly #130

Merged
merged 13 commits into from
Nov 8, 2018

Conversation

kevindew
Copy link
Member

@kevindew kevindew commented Nov 2, 2018

Trello: https://trello.com/c/gEAMmD0g/386-ability-to-embed-a-contact-via-markdown

This PR is to fix issues encountered by trying to use the Contacts feature added to Govspeak in 2016. Prior to this this feature was not used in any apps (evidenced by this feature not actually working properly) thus I don't consider any changes to this to be backwards compatibility breaks.

@kevindew kevindew force-pushed the extract-contacts branch 2 times, most recently from f88d496 to 77c036f Compare November 2, 2018 18:32
@kevindew kevindew changed the title Make Contacts feature work and be more user friendly [WIP] Make Contacts feature work and be more user friendly Nov 5, 2018
@kevindew kevindew changed the title [WIP] Make Contacts feature work and be more user friendly Make Contacts feature work and be more user friendly Nov 6, 2018
kevindew added a commit to alphagov/content-publisher that referenced this pull request Nov 6, 2018
kevindew added a commit to alphagov/content-publisher that referenced this pull request Nov 6, 2018
kevindew added a commit to alphagov/content-publisher that referenced this pull request Nov 6, 2018
@kevindew kevindew requested a review from benthorner November 7, 2018 09:43
Copy link
Contributor

@benthorner benthorner left a comment

Choose a reason for hiding this comment

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

Nice work 👍. It's great to see all the effort you put in has paid off :-). The only comment I'd really like your thoughts on is the UUID extractor in the Document class.

# Not showing United Kingdom country is a "feature" lifted and shifted
# from Whitehall:
# https://github.com/alphagov/whitehall/blob/c67d53d80f9856549c2da1941a10dbb9170be494/lib/address_formatter/formatter.rb#L17
value = "" if key == "world_location" && value.strip == "United Kingdom"
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be written as Hash[address]["world_location"].strip == "United Kingdom"? The current code with the assignment and reject is a bit tricky to follow.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've reworked this now. Hopefully it's easier now - didn't quite understand how your suggestion could work I'm afraid.

lib/govspeak.rb Show resolved Hide resolved
test/govspeak_contacts_test.rb Show resolved Hide resolved
This allows us to determine which contacts have been linked to in a
document so that we can work out what data to insert when rendering
govspeak.

As there is legacy markdown for contacts ([Contact: 123]) this
determines that the contacts are only ones that match a UUID format -
this also saves client apps having to work out whether an id is valid
before sending it to the Publishing API.
Prior to this PR the data for contacts inside was based on the needs of
the HTML that would be rendered. This makes it rather incompatible with
the contact format that is used [1].

This changes the expected data for contacts to be based on the
content schema - so that a Content Item returned from Publishing API get
contact (or from Content Store) can be accepted.

It also drops a worldwide office link which is a tricky to implement
feature that Whitehall does not seem to actually use as part of govspeak
rendering:

Whitehall uses this code to render Contacts: https://github.com/alphagov/whitehall/blob/58374b3ba0de07be9dfef269028e1663f4fda0f8/app/helpers/govspeak_helper.rb#L144-L148
The link is only rendered if the Contact is a WorldwideOffice: https://github.com/alphagov/whitehall/blob/235a26c29c18b081a23a075d76f0f2ef34c4e360/app/views/contacts/_contact.html.erb#L47-L49
Worldwide office can pretend to be a Contact:
https://github.com/alphagov/whitehall/blob/235a26c29c18b081a23a075d76f0f2ef34c4e360/app/models/worldwide_office.rb#L18-L22
but will never be loaded from a Contact.find(x) method as per the first
point.

[1]: https://github.com/alphagov/govuk-content-schemas/blob/4e976c696ddd6ebfd1c4e1da52d6d5af54c79ba4/formats/contact.jsonnet
The previous method did not work when this gem was used in a ruby app as
the load method was relative to the current directory. This changes this
to load the files relative to the file.

This also loads the locale files in when the gem starts rather than at
app runtime. This is so the gem does not surprise apps by adding locale
information later that can not be overriden by apps.
This is to stop them getting mixed into the translations of an
application that uses locales
This creates a method on the Govspeak module that will return the root
path of the Gem. This is to make it simpler to include files this gem
requires.

The relative loading of files prior to this meant files were only loaded
when running the gems tests and not when the gem is used in an
application.
This re-applies a quirk of Whitehall where United Kingdom as a country
name is not shown in any contact addresses that are shown to users as
it's considered implicit. It feels a bit weird to do this here and
potentially frustrating in some contexts but it stops us introducing a
change.

This also adds code to deal with a common problem that contacts have an
array of empty data for postal_addresses in the Publishing API

As an example (from Publishing API):

Document.find_by(content_id: "fb6777d8-1e19-427f-8dae-0ef50f0ff359").live

```
details:
  {"description"=>"",
   "contact_type"=>"General contact",
   "title"=>"Visa Enquiries",
   "contact_form_links"=>[{"title"=>"Visa Enquiries", "link"=>"", "description"=>""}],
   "post_addresses"=>
    [{"title"=>"",
      "street_address"=>"",
      "locality"=>"",
      "region"=>"",
      "postal_code"=>"",
      "world_location"=>""}],
   "email_addresses"=>
    [{"title"=>"",
      "email"=>"All enquiries via the UK Visas website at www.gov.uk/visas-immigration"}],
   "phone_numbers"=>[{"title"=>"Visa Enquiries", "number"=>"00 44 203 481 1736"}]},
```

(No comment on that email address)
This feature didn't work when I came to use this in an application. It
used the following methods:

h - Rails html_escape helper, not available in a Gem
format_with_html_line_breaks - Whitehall helper method, now lifted and
shifted here, already does a html_escape so that is no longer needed
auto_link - An old Rails helper removed in 3.1, now provided by a Gem. I
chose rinku as per Whitehall
This seems to be covered with the contact is not present in options test
defined above.
This is to stop this gem being subtly broken.
This is a bit of sad commit as I didn't really want to add this stuff
in. This adds more protection against empty data from the Publishing API
where it returns fields with empty strings.

I'm hoping to fix most of the data Whitehall has currently but I don't
think I'll fix it all so this protection will probably be needed
ongoing.
@kevindew
Copy link
Member Author

kevindew commented Nov 8, 2018

@benthorner All comments addressed now, hopefully this is good to go.

Copy link
Contributor

@benthorner benthorner left a comment

Choose a reason for hiding this comment

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

Happy to have a look with you on the post address filtering bit - I like this kind of data flow puzzle :-).

@kevindew
Copy link
Member Author

kevindew commented Nov 8, 2018

Thanks @benthorner - sure if we have a mo tomorrow lets chat.

@kevindew kevindew merged commit 17608c7 into master Nov 8, 2018
@kevindew kevindew deleted the extract-contacts branch November 8, 2018 15:29
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