From 8c36499e247f5708cdc2c8b1180fabe21ca74628 Mon Sep 17 00:00:00 2001 From: Gareth Rees Date: Tue, 30 Aug 2022 09:39:30 +0100 Subject: [PATCH] Automatically tag bodies missing a request address Automatically apply and remove the `missing_email` tag to bodies that have a missing email so that users can find them via a public search and help to source the addresses. The changelog suggests a one-shot script to mass-apply the tag to existing bodies that are missing an email. Fixes https://github.com/mysociety/alaveteli/issues/3477. --- app/models/public_body.rb | 16 ++++++++++++- app/views/admin_public_body/_form.html.erb | 1 + doc/CHANGES.md | 8 +++++++ lib/has_tag_string/has_tag_string.rb | 2 ++ spec/models/public_body_spec.rb | 28 ++++++++++++++++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) diff --git a/app/models/public_body.rb b/app/models/public_body.rb index 339608122e..106bae6cea 100644 --- a/app/models/public_body.rb +++ b/app/models/public_body.rb @@ -113,8 +113,10 @@ def self.admin_title validate :request_email_if_requestable before_save :set_api_key!, :unless => :api_key - after_update :reindex_requested_from + after_save :update_missing_email_tag + + after_update :reindex_requested_from # Every public body except for the internal admin one is visible scope :visible, -> { where("public_bodies.id <> #{ PublicBody.internal_admin_body.id }") } @@ -975,4 +977,16 @@ def self.get_public_body_list_translated_condition(table, has_first_letter=false end result end + + def update_missing_email_tag + if missing_email? + add_tag_if_not_already_present('missing_email') + else + remove_tag('missing_email') + end + end + + def missing_email? + !has_request_email? + end end diff --git a/app/views/admin_public_body/_form.html.erb b/app/views/admin_public_body/_form.html.erb index b521372cae..6d11cf394b 100644 --- a/app/views/admin_public_body/_form.html.erb +++ b/app/views/admin_public_body/_form.html.erb @@ -62,6 +62,7 @@
  • defunct if the authority no longer exists
  • charity:NUMBER if a registered charity
  • important_notes if the notes have major implications on making a request to this authority
  • +
  • missing_email is automatically applied (and removed) so that users can help source missing request addresses via a <%= link_to 'public search', list_public_bodies_by_tag_path('missing_email') %>.
  • diff --git a/doc/CHANGES.md b/doc/CHANGES.md index 1de90c3920..82f1c50a9b 100644 --- a/doc/CHANGES.md +++ b/doc/CHANGES.md @@ -2,6 +2,8 @@ ## Highlighted Features +* Automatically apply `missing_email` tag to bodies who are missing a request + email so that they can be found in a public list (Gareth Rees) * Improve linking from outgoing & incoming message admin pages (Gareth Rees) * Allow admins to destroy user post redirects (Gareth Rees) * Use correct mime type for cached CSV attachments @@ -11,6 +13,12 @@ ## Upgrade Notes +* _Optional:_ Bodies missing a request email will automatically get tagged + `missing_email` as they are updated. If you want to automatically tag them all + in one go, run the following from the app root directory: + + bin/rails runner "PublicBody.without_request_email.each(&:save)" + ### Changed Templates # 0.41.1.0 diff --git a/lib/has_tag_string/has_tag_string.rb b/lib/has_tag_string/has_tag_string.rb index 475e687315..ab57393a0b 100644 --- a/lib/has_tag_string/has_tag_string.rb +++ b/lib/has_tag_string/has_tag_string.rb @@ -111,6 +111,8 @@ def has_tag?(tag_as_string) # rubocop:disable Naming::PredicateName false end + alias tagged? has_tag? + class TagNotFound < StandardError end diff --git a/spec/models/public_body_spec.rb b/spec/models/public_body_spec.rb index 5e8a44fda2..e2cb141630 100644 --- a/spec/models/public_body_spec.rb +++ b/spec/models/public_body_spec.rb @@ -192,6 +192,34 @@ end + describe '#save' do + subject { public_body.save } + + context 'when a request email is added' do + let!(:public_body) do + FactoryBot.create(:blank_email_public_body, tag_string: 'missing_email') + end + + before { public_body.request_email = 'added@example.com' } + + it 'removes the missing email tag' do + subject + expect(public_body).not_to be_tagged('missing_email') + end + end + + context 'when a request email is removed' do + let!(:public_body) { FactoryBot.create(:public_body) } + + before { public_body.request_email = '' } + + it 'adds the missing email tag' do + subject + expect(public_body).to be_tagged('missing_email') + end + end + end + describe '#name' do it 'is invalid when nil' do