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

Improve the closing and reopening of the petitions website #939

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 75 additions & 30 deletions app/controllers/admin/parliaments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,20 @@ def show
end

def update
if @parliament.update(parliament_params)
if send_emails?
@parliament.send_emails!
redirect_to admin_parliament_url(tab: params[:tab]), notice: :creators_emailed
elsif schedule_closure?
@parliament.schedule_closure!
redirect_to admin_parliament_url(tab: params[:tab]), notice: :closure_scheduled
elsif archive_petitions?
@parliament.start_archiving!
redirect_to admin_parliament_url(tab: params[:tab]), notice: :petitions_archiving
elsif anonymize_petitions?
@parliament.start_anonymizing!
redirect_to admin_parliament_url(tab: params[:tab]), notice: :petitions_anonymizing
elsif archive_parliament?
@parliament.archive!
redirect_to admin_parliament_url(tab: params[:tab]), notice: :parliament_archived
else
redirect_to admin_parliament_url(tab: params[:tab]), notice: :parliament_updated
respond_to do |format|
format.html do
@parliament.assign_attributes(parliament_params)

if @parliament.save(context: parliament_context)
if perform_parliament_action!
redirect_to admin_parliament_url(tab: params[:tab]), notice: parliament_action_notice
else
redirect_to admin_parliament_url(tab: params[:tab]), alert: parliament_action_alert
end
else
render :show, alert: :parliament_not_updated
end
end
else
render :show, alert: :parliament_not_updated
end
end

Expand All @@ -53,23 +46,75 @@ def parliament_params
)
end

def send_emails?
params.key?(:send_emails) && @parliament.dissolution_at?
def perform_parliament_action!
case button_param
when "send_emails"
@parliament.send_emails!
when "schedule_closure"
@parliament.schedule_closure!
when "archive_petitions"
@parliament.start_archiving!
when "anonymize_petitions"
@parliament.start_anonymizing!
when "archive_parliament"
@parliament.archive!
else
true
end
end

def schedule_closure?
params.key?(:schedule_closure) && @parliament.dissolution_announced?
def parliament_context
case button_param
when "send_emails"
:send_emails
when "schedule_closure"
:schedule_closure
when "archive_petitions"
:archive_petitions
when "archive_parliament"
:archive_parliament
when "anonymize_petitions"
:anonymize_petitions
else
nil
end
end

def archive_petitions?
params.key?(:archive_petitions) && @parliament.can_archive_petitions?
def parliament_action_notice
case button_param
when "send_emails" then
:creators_emailed
when "schedule_closure"
:closure_scheduled
when "archive_petitions"
:petitions_archiving
when "archive_parliament"
:parliament_archived
when "anonymize_petitions"
:petitions_anonymizing
else
:parliament_updated
end
end

def anonymize_petitions?
params.key?(:anonymize_petitions) && @parliament.can_anonymize_petitions?
def parliament_action_alert
case button_param
when "send_emails" then
:creators_not_emailed
when "schedule_closure"
:closure_not_scheduled
when "archive_petitions"
:petitions_not_archiving
when "archive_parliament"
:parliament_not_archived
when "anonymize_petitions"
:petitions_not_anonymizing
else
:parliament_not_updated
end
end

def archive_parliament?
params.key?(:archive_parliament) && @parliament.can_archive?
def button_param
params.fetch(:button, "save")
end
end
22 changes: 22 additions & 0 deletions app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,28 @@ def short_rejection_reason(rejection)
t(rejection.code, scope: :"rejection.reasons.short", default: rejection.code.titleize)
end

def parliament_tab
if (errors = @parliament.errors.attribute_names).present?
if (errors & %i[government opening_at]).present?
"details"
elsif (errors & %i[dissolution_at dissolution_faq_url show_dissolution_notification dissolution_heading dissolution_message]).present?
"dissolution"
elsif (errors & %i[notification_cutoff_at dissolved_heading dissolved_message]).present?
"dissolved"
elsif (errors & %i[election_date registration_closed_at]).present?
"election"
elsif (errors & %i[government_response_heading government_response_description government_response_status]).present?
"response"
elsif (errors & %i[parliamentary_debate_heading parliamentary_debate_description parliamentary_debate_status]).present?
"debate"
else
"details"
end
else
params.fetch(:tab, "details")
end
end

private

def admin_petition_facets
Expand Down
56 changes: 54 additions & 2 deletions app/models/parliament.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,23 @@ def reload
Thread.current[:__parliament__] = nil
end

def reset!(attributes = defaults)
destroy_all and reload
create!(attributes)
end

def current_or_create
current.first_or_create(government: "TBC", opening_at: 2.weeks.ago)
current.first_or_create(defaults)
end

def update!(attributes)
instance.update!(attributes)
end

def defaults
I18n.t(:defaults, scope: :parliament).transform_values do |value|
value.respond_to?(:call) ? value.call : value
end
end
end

Expand All @@ -148,6 +163,43 @@ def current_or_create
validates_numericality_of :petition_duration, greater_than_or_equal_to: 1, allow_blank: true
validates_numericality_of :petition_duration, less_than_or_equal_to: 12, allow_blank: true

validate on: :send_emails do
errors.add(:dissolution_at, :blank) unless dissolution_at?
errors.add(:dissolution_faq_url, :blank) unless dissolution_faq_url?
errors.add(:show_dissolution_notification, :not_visible) unless show_dissolution_notification?
errors.add(:registration_closed_at, :blank) unless registration_closed_at?
errors.add(:election_date, :blank) unless election_date?
end

validate on: :schedule_closure do
errors.add(:notification_cutoff_at, :blank) unless notification_cutoff_at?
end

validate on: :archive_petitions do
errors.add(:dissolution_at, :blank) unless dissolution_at?

if dissolution_at?
errors.add(:dissolution_at, :too_soon) unless dissolution_at < 2.days.ago
end
end

validate on: :archive_parliament do
errors.add(:dissolution_at, :blank) unless dissolution_at?

if dissolution_at?
errors.add(:dissolution_at, :too_soon) unless dissolution_at < 2.days.ago
errors.add(:dissolution_at, :still_archiving) if dissolved? && archiving?
end
end

validate on: :anonymize_petitions do
errors.add(:opening_at, :blank) unless opening_at?

if opening_at?
errors.add(:opening_at, :too_soon) unless opening_at < 6.months.ago
end
end

after_save { Site.touch }

def name
Expand Down Expand Up @@ -224,7 +276,7 @@ def start_archiving!(now = Time.current)
end

def start_anonymizing!(now = Time.current)
unless dissolving? || dissolved? || archiving?
if can_anonymize_petitions?
Archived::AnonymizePetitionsJob.set(wait_until: midnight).perform_later(midnight.iso8601)
end
end
Expand Down
9 changes: 9 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ def reload
Thread.current[:__site__] = nil
end

def reset!(attributes = defaults)
destroy_all and reload
create!(attributes)
end

def update!(attributes)
instance.update!(attributes)
end

def touch(*names)
if instance.persisted?
instance.touch(*names)
Expand Down
14 changes: 7 additions & 7 deletions app/views/admin/parliaments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<%= form_for @parliament, url: admin_parliament_url do |form| %>
<%= render tab, form: form %>
<%= form.submit 'Save', class: 'button' %>
<%= render parliament_tab, form: form %>
<%= form.button 'Save', name: 'button', value: 'save', class: 'button' %>
<% if @parliament.dissolved? %>
<% if @parliament.can_archive_petitions? %>
<%= form.submit 'Archive petitions', name: 'archive_petitions', class: 'button-secondary', data: { confirm: 'Copy current petitions to archive?' } %>
<%= form.button 'Archive petitions', name: 'button', value: 'archive_petitions', class: 'button-secondary', data: { confirm: 'Copy current petitions to archive?' } %>
<% elsif @parliament.can_archive? %>
<%= form.submit 'Archive parliament', name: 'archive_parliament', class: 'button-secondary', data: { confirm: 'Archive this parliament and create a new one?' } %>
<%= form.button 'Archive parliament', name: 'button', value: 'archive_parliament', class: 'button-secondary', data: { confirm: 'Archive this parliament and create a new one?' } %>
<% end %>
<% elsif @parliament.dissolving? %>
<% if [email protected]_emails_sent? %>
<%= form.submit 'Send dissolution emails', name: 'send_emails', class: 'button-secondary', data: { confirm: 'Email everyone about dissolution?' } %>
<%= form.button 'Send dissolution emails', name: 'button', value: 'send_emails', class: 'button-secondary', data: { confirm: 'Email everyone about dissolution?' } %>
<% elsif [email protected]_scheduled? %>
<%= form.submit 'Schedule closure', name: 'schedule_closure', class: 'button-secondary', data: { confirm: 'Schedule early closure of petitions?' } %>
<%= form.button 'Schedule closure', name: 'button', value: 'schedule_closure', class: 'button-secondary', data: { confirm: 'Schedule early closure of petitions?' } %>
<% end %>
<% end %>
<% if @parliament.can_anonymize_petitions? %>
<%= form.submit 'Anonymize petitions', name: 'anonymize_petitions', class: 'button-secondary', data: { confirm: 'Anonymize all archived petitions?' } %>
<%= form.button 'Anonymize petitions', name: 'button', value: 'anonymize_petitions', class: 'button-secondary', data: { confirm: 'Anonymize all archived petitions?' } %>
<% end %>
<%= link_to 'Cancel', admin_root_path, class: 'button-secondary' %>
<% end %>
Expand Down
14 changes: 1 addition & 13 deletions app/views/admin/parliaments/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@
</div>

<div class="column-two-thirds extra-gutter">
<% if params[:tab] == "dissolution" %>
<%= render "form", tab: "dissolution" %>
<% elsif params[:tab] == "election" %>
<%= render "form", tab: "election" %>
<% elsif params[:tab] == "dissolved" %>
<%= render "form", tab: "dissolved" %>
<% elsif params[:tab] == "response" %>
<%= render "form", tab: "response" %>
<% elsif params[:tab] == "debate" %>
<%= render "form", tab: "debate" %>
<% else %>
<%= render "form", tab: "details" %>
<% end %>
<%= render "form" %>
</div>
</div>
10 changes: 5 additions & 5 deletions app/views/admin/shared/_parliament_tabs.html.erb
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<p class="tabs">
<% if params[:tab] == "dissolution" %>
<% if parliament_tab == "dissolution" %>
<%= link_to "Details", admin_parliament_path %> |
Dissolution |
<%= link_to "Election", admin_parliament_path(tab: 'election') %> |
<%= link_to "Dissolved", admin_parliament_path(tab: 'dissolved') %> |
<%= link_to "Government Response", admin_parliament_path(tab: 'response') %> |
<%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %>
<% elsif params[:tab] == "election" %>
<% elsif parliament_tab == "election" %>
<%= link_to "Details", admin_parliament_path %> |
<%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> |
Election |
<%= link_to "Dissolved", admin_parliament_path(tab: 'dissolved') %> |
<%= link_to "Government Response", admin_parliament_path(tab: 'response') %> |
<%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %>
<% elsif params[:tab] == "dissolved" %>
<% elsif parliament_tab == "dissolved" %>
<%= link_to "Details", admin_parliament_path %> |
<%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> |
<%= link_to "Election", admin_parliament_path(tab: 'election') %> |
Dissolved |
<%= link_to "Government Response", admin_parliament_path(tab: 'response') %> |
<%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %>
<% elsif params[:tab] == "response" %>
<% elsif parliament_tab == "response" %>
<%= link_to "Details", admin_parliament_path %> |
<%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> |
<%= link_to "Election", admin_parliament_path(tab: 'election') %> |
<%= link_to "Dissolved", admin_parliament_path(tab: 'dissolved') %> |
Government Response |
<%= link_to "Parliamentary Debate", admin_parliament_path(tab: 'debate') %>
<% elsif params[:tab] == "debate" %>
<% elsif parliament_tab == "debate" %>
<%= link_to "Details", admin_parliament_path %> |
<%= link_to "Dissolution", admin_parliament_path(tab: 'dissolution') %> |
<%= link_to "Election", admin_parliament_path(tab: 'election') %> |
Expand Down
19 changes: 19 additions & 0 deletions config/locales/activerecord.en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ en-GB:
aliased_domain:
not_found: "The aliased domain was not found - please check the spelling and try again"

parliament:
attributes:
dissolution_at:
blank: Please enter the date and time when parliament dissolves
too_soon: Please wait until two days after dissolution to begin archiving
dissolution_faq_url:
blank: Please enter a URL for the Dissolution FAQ
election_date:
blank: Please enter the election date
notification_cutoff_at:
blank: Please enter a notification cut-off
opening_at:
blank: Please enter the date and time when parliament will reopen
too_soon: Please wait until six months after reopening before anonymizing petitions
registration_closed_at:
blank: Please enter the date and time at which voter registration closes
show_dissolution_notification:
not_visible: Please make the dissolution notification visible

petition:
attributes:
moderation:
Expand Down
7 changes: 6 additions & 1 deletion config/locales/admin.en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ en-GB:
admin_required: "You must be logged in as an administrator to view this page"
change_password: "Please change your password before continuing"
closure_scheduled: "Petitions have been scheduled to close early"
closure_not_scheduled: "Petitions could not be scheduled to close early - please contact support"
creators_emailed: "Everyone will be notified of the early closing of their petitions"
creators_not_emailed: "Could not notify everyone of the early closing of their petitions - please contact support"
debate_date_updated: "Updated the scheduled debate date successfully"
debate_outcome_updated: "Updated debate outcome successfully"
department_created: "Department created successfully"
Expand Down Expand Up @@ -82,10 +84,13 @@ en-GB:
missing_tasks: "Please select one or more tasks to execute"
password_updated: "Password was successfully updated"
petitions_archiving: "Archiving of petitions was successfully started"
petitions_not_archiving: "Archiving of petitions could not be started - please contact support"
petitions_anonymizing: "Anonymizing of petitions was successfully started"
petitions_not_anonymizing: "Anonymizing of petitions could not be started - please contact support"
parliament_archived: "Parliament archived successfully"
parliament_not_updated: "Parliament could not be updated - please check the form for errors"
parliament_not_archived: "Parliament could not be archived - please contact support"
parliament_updated: "Parliament updated successfully"
parliament_not_updated: "Parliament could not be updated - please check the form for errors"
petition_email_created: "Created related activity successfully"
petition_email_updated: "Updated related activity successfully"
petition_email_deleted: "Deleted related activity successfully"
Expand Down
Loading