Skip to content

Commit

Permalink
Merge pull request #546 from alphagov/tidy-admin
Browse files Browse the repository at this point in the history
Tidy up administration area
  • Loading branch information
alanth authored Oct 11, 2016
2 parents be6bfaa + ed181f6 commit 9f92255
Show file tree
Hide file tree
Showing 52 changed files with 1,253 additions and 199 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/admin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//= require jquery
//= require jquery_ujs
//= require auto_logout
//= require autosubmit_selects
44 changes: 44 additions & 0 deletions app/assets/javascripts/auto_logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
(function ($) {
'use strict';

$.fn.autoLogout = function() {
var $html = this;
var $continue = $html.find('#logout-warning-continue');
var $logout = $html.find('#logout-warning-logout');
var INTERVAL = 10000;
var WARNING_TIME = 120;

var AutoLogout = {
continueClicked: function(e) {
$.getJSON('/admin/continue.json', AutoLogout.processContinue);
},

logoutClicked: function(e) {
window.location = '/admin/logout';
},

processContinue: function(data) {
$html.hide();
},

processStatus: function(data) {
if (data.time_remaining == 0) {
window.location = '/admin/logout';
} else if (data.time_remaining <= WARNING_TIME) {
$html.show();
} else {
$html.hide();
}
},

checkStatus: function() {
$.getJSON('/admin/status.json', AutoLogout.processStatus);
}
};

$continue.on('click', AutoLogout.continueClicked);
$logout.on('click', AutoLogout.logoutClicked);

window.setInterval(AutoLogout.checkStatus, INTERVAL);
}
})(jQuery);
30 changes: 30 additions & 0 deletions app/assets/stylesheets/petitions/admin/_logout.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#logout-warning {
width: 100%;
height: 100%;
position: fixed;
top: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
display: none;

.logout-warning-modal {
background-color: #FFF;
margin: 10% auto 0;
width: 40%;
padding: 24px;
box-shadow: 0px 5px 20px 10px rgba(0, 0, 0, 0.3);
}

.logout-warning-message {
margin: 0 0 24px 0;
}

.logout-warning-actions {
margin: 0;

button {
font-size: 16px;
margin-bottom: 2px;
}
}
}
10 changes: 10 additions & 0 deletions app/assets/stylesheets/petitions/admin/views/_shared.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
margin-top: $gutter;
}

.mandatory {
color: $red-50;
font-weight: bold;
font-size: 24px;
}

.form-label-inline {
display: inline-block;
}

.button_to {
display: inline-block;

Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/site-admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
@import "petitions/admin/meta";
@import "petitions/admin/actions";
@import "petitions/admin/list";
@import "petitions/admin/logout";
@import "petitions/admin/tables";

// Petition admin views
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/admin/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
class Admin::AdminController < ApplicationController
include Authentication
include Authentication, FlashI18n, FlashRender

before_action :require_admin_and_check_for_password_change
before_action :do_not_cache

layout 'admin'

def index
end

end
44 changes: 24 additions & 20 deletions app/controllers/admin/admin_users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,58 @@
class Admin::AdminUsersController < Admin::AdminController
before_filter :require_sysadmin
before_filter :find_user, only: %i[edit update destroy]

rescue_from AdminUser::CannotDeleteCurrentUser do
redirect_to admin_admin_users_url, alert: :user_is_current_user
end

rescue_from AdminUser::MustBeAtLeastOneAdminUser do
redirect_to admin_admin_users_url, alert: :user_count_is_too_low
end

def index
@users = AdminUser.by_name.paginate(:page => params[:page], :per_page => 50)
@users = AdminUser.by_name.paginate(page: params[:page], per_page: 50)
end

def new
@user = AdminUser.new
end

def create
@user = AdminUser.create(admin_user_params)
@user = AdminUser.new(admin_user_params)

if @user.save
flash[:notice] = "User was successfully created"
redirect_to admin_admin_users_url
redirect_to admin_admin_users_url, notice: :user_created
else
render :action => 'new'
render :new
end
end

def edit
@user = AdminUser.find(params[:id])
end

def update
@user = AdminUser.find(params[:id])
if @user.update_attributes(admin_user_params)
flash[:notice] = "User was successfully updated"
redirect_to admin_admin_users_url
if @user.update(admin_user_params)
redirect_to admin_admin_users_url, notice: :user_updated
else
render :action => 'edit'
render :edit
end
end

def destroy
@user = AdminUser.find(params[:id])

# only destroy if user is not the logged in user and there are at least 2 users
if @user == current_user
flash[:error] = "You are not allowed to delete yourself!"
elsif AdminUser.count < 2
flash[:error] = "There needs to be at least 1 admin user"
if @user.destroy(current_user: current_user)
redirect_to admin_admin_users_url, notice: :user_deleted
else
@user.destroy
redirect_to admin_admin_users_url, alert: :user_not_deleted
end
redirect_to admin_admin_users_url
end

protected

def find_user
@user = AdminUser.find(params[:id])
end

def admin_user_params
params.
require(:admin_user).
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/debate_outcomes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def update
if @debate_outcome.update(debate_outcome_params)
if send_email_to_petitioners?
EmailDebateOutcomesJob.run_later_tonight(petition: @petition)
message = 'Email will be sent overnight'
message = :email_sent_overnight
else
message = 'Updated debate outcome successfully'
message = :debate_outcome_updated
end

redirect_to [:admin, @petition], notice: message
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/government_response_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def update
if @government_response.update(government_response_params)
if send_email_to_petitioners?
EmailThresholdResponseJob.run_later_tonight(petition: @petition)
message = 'Email will be sent overnight'
message = :email_sent_overnight
else
message = 'Updated government response successfully'
message = :government_response_updated
end

redirect_to [:admin, @petition], notice: message
Expand Down
32 changes: 16 additions & 16 deletions app/controllers/admin/invalidations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def new

def create
if @invalidation.save
redirect_to admin_invalidations_url, notice: "Invalidation created successfully"
redirect_to admin_invalidations_url, notice: :invalidation_created
else
respond_to do |format|
format.html { render :new }
Expand All @@ -32,69 +32,69 @@ def edit
format.html
end
else
redirect_to_index_url notice: "Can't edit invalidations that aren't pending"
redirect_to_index_url notice: :invalidation_cant_be_edited
end
end

def update
if @invalidation.pending?
if @invalidation.update(invalidation_params)
redirect_to admin_invalidations_url, notice: "Invalidation updated successfully"
redirect_to admin_invalidations_url, notice: :invalidation_updated
else
respond_to do |format|
format.html { render :edit }
end
end
else
redirect_to_index_url notice: "Can't edit invalidations that aren't pending"
redirect_to_index_url notice: :invalidation_cant_be_edited
end
end

def destroy
if @invalidation.started?
redirect_to_index_url notice: "Can't remove invalidations that have started"
redirect_to_index_url notice: :invalidation_cant_be_removed
else
if @invalidation.destroy
redirect_to_index_url notice: "Invalidation removed successfully"
redirect_to_index_url notice: :invalidation_removed
else
redirect_to_index_url alert: "Invalidation could not be removed - please contact support"
redirect_to_index_url alert: :invalidation_not_removed
end
end
end

def cancel
if @invalidation.completed?
redirect_to_index_url notice: "Can't cancel invalidations that have completed"
redirect_to_index_url notice: :invalidation_cant_be_cancelled
else
if @invalidation.cancel!
redirect_to_index_url notice: "Invalidation cancelled successfully"
redirect_to_index_url notice: :invalidation_cancelled
else
redirect_to_index_url alert: "Invalidation could not be cancelled - please contact support"
redirect_to_index_url alert: :invalidation_not_cancelled
end
end
end

def count
if @invalidation.pending?
if @invalidation.count!
redirect_to_index_url notice: "Counted the matching signatures for invalidation #{@invalidation.summary.inspect}"
redirect_to_index_url notice: [:invalidation_counted, summary: @invalidation.summary.inspect]
else
redirect_to_index_url alert: "Invalidation could not be counted - please contact support"
redirect_to_index_url alert: :invalidation_not_counted
end
else
redirect_to_index_url notice: "Can't count invalidations that aren't pending"
redirect_to_index_url notice: :invalidation_cant_be_counted
end
end

def start
if @invalidation.pending?
if @invalidation.start!
redirect_to_index_url notice: "Enqueued the invalidation #{@invalidation.summary.inspect}"
redirect_to_index_url notice: [:invalidation_started, summary: @invalidation.summary.inspect]
else
redirect_to_index_url alert: "Invalidation could not be enqueued - please contact support"
redirect_to_index_url alert: :invalidation_not_started
end
else
redirect_to_index_url notice: "Can't start invalidations that aren't pending"
redirect_to_index_url notice: :invalidation_cant_be_started
end
end

Expand Down
3 changes: 1 addition & 2 deletions app/controllers/admin/petition_details_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ def show

def update
if @petition.update_attributes(petition_params)
flash[:notice] = 'Petition has been successfully updated'
redirect_to [:admin, @petition]
redirect_to [:admin, @petition], notice: :petition_updated
else
render :show
end
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/admin/petition_emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def create
if @email.update(email_params)
if send_email_to_petitioners?
schedule_email_petitioners_job
message = 'Email will be sent overnight'
message = :email_sent_overnight
else
message = 'Created other parliamentary business successfully'
message = :petition_email_created
end

redirect_to [:admin, @petition], notice: message
Expand All @@ -30,9 +30,9 @@ def update
if @email.update(email_params)
if send_email_to_petitioners?
schedule_email_petitioners_job
message = 'Email will be sent overnight'
message = :email_sent_overnight
else
message = 'Updated other parliamentary business successfully'
message = :petition_email_updated
end

redirect_to [:admin, @petition], notice: message
Expand All @@ -43,9 +43,9 @@ def update

def destroy
if @email.destroy
message = 'Deleted other parliamentary business successfully'
message = :petition_email_deleted
else
message = 'Unable to delete other parliamentary business - please contact support'
message = :petition_email_not_deleted
end

redirect_to [:admin, @petition], notice: message
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/petitions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def update_scheduled_debate_date
fetch_petition_for_scheduled_debate_date
if @petition.update(update_scheduled_debate_date_params)
EmailDebateScheduledJob.run_later_tonight(petition: @petition)
redirect_to admin_petition_url(@petition), notice: "Email will be sent overnight"
redirect_to admin_petition_url(@petition), notice: :email_sent_overnight
else
render :edit_scheduled_debate_date
end
Expand Down
Loading

0 comments on commit 9f92255

Please sign in to comment.