Skip to content

Commit

Permalink
Merge pull request #9061 from alphagov/retrospective-political
Browse files Browse the repository at this point in the history
Rake task: Mark documents as political for an organisation
  • Loading branch information
ryanb-gds authored May 24, 2024
2 parents 0a3f91d + 37494b2 commit bb030a1
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/tasks/election.rake
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,34 @@ namespace :election do
puts e.message
end
end

desc "
Mark all documents of a given organisation as political
Usage:
rake election:mark_documents_as_political_for[organisation_slug, '30-12-2024']
"
task :mark_documents_as_political_for, %i[slug date] => :environment do |_t, args|
date = Date.parse(args[:date])
org = Organisation.find_by!(slug: args[:slug])
puts "Marking all documents as political for #{org.name}..."

editions = org.editions
published = editions
.published
.where("first_published_at >= ?", date)

puts "Marking #{published.size} published editions as political..."
published.update_all(political: true)

pre_published_editions = Edition.in_pre_publication_state.where(document_id: published.map(&:document_id))

puts "Updating #{pre_published_editions.size} pre-publication editions as political..."
pre_published_editions.update_all(political: true)

puts "Done"
rescue Date::Error => _e
puts "The date is not on the right format [\"#{args[:date]}\"]"
rescue ActiveRecord::RecordNotFound => _e
puts "There is no Organisation with slug [\"#{args[:slug]}\"]"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "test_helper"
require "rake"

class MarkDocumentsAsPoliticalFor < ActiveSupport::TestCase
extend Minitest::Spec::DSL

teardown { task.reenable }

describe "#mark_as_political" do
let(:task) { Rake::Task["election:mark_documents_as_political_for"] }
let(:organisation) { create(:organisation) }
let(:document) { create(:document) }
let(:published_edition) { create(:edition, :published, document:, first_published_at: "01-01-2023") }
let(:draft_edition) { create(:edition, :draft, document:) }
let(:date) { "31-12-2022" }

setup do
organisation.editions = [published_edition, draft_edition]
organisation.save!
end

it "raises an error if organisation does not exist" do
out, _err = capture_io { task.invoke("non-existent-slug", date) }
assert_equal 'There is no Organisation with slug ["non-existent-slug"]', out.strip
end

it "raises an error if date is not right format" do
out, _err = capture_io { task.invoke(organisation.slug, "not a date") }
assert_equal 'The date is not on the right format ["not a date"]', out.strip
end

it "marks published editions of documents first published after the specified date and any associated drafts as political" do
capture_io { task.invoke(organisation.slug, date) }
assert published_edition.reload.political
assert draft_edition.reload.political
end

it "does not mark editions unrelated to documents first published before the specified date as political" do
non_political_document = create(:document)
create(:edition, :published, document: non_political_document, first_published_at: "30-12-2022")
create(:edition, :draft, document: non_political_document)
capture_io { task.invoke(organisation.slug, date) }
assert_not non_political_document.reload.live_edition.political
assert_not non_political_document.reload.latest_edition.political
end
end
end

0 comments on commit bb030a1

Please sign in to comment.