Skip to content

Commit

Permalink
Merge pull request #251 from alphagov/save-and-transition-in-one-go
Browse files Browse the repository at this point in the history
Save and transition editions in one go
  • Loading branch information
alext committed Sep 5, 2014
2 parents d943d6c + 4864e45 commit f20d95f
Show file tree
Hide file tree
Showing 34 changed files with 300 additions and 290 deletions.
53 changes: 2 additions & 51 deletions app/assets/javascripts/publications.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
// Javascript that may be used on every publication show/edit page

$(function () {
/*
Pre-submit a form, invoking a callback if the submission succeeds.
This is mainly used for the action buttons other than "Save", where it
makes sense to save the edition and perform the requested action if there
aren't any errors.
*/
var submit_form = function(form, success) {
var jq = $.post(
form.attr('action') + ".json",
form.serialize(),
success
).error( function(data) {
var errors = $.parseJSON(data.responseText);
var messages = "There were problems saving this edition: ";
errors = $.map(errors, function(v,k) {
return k + " " + v.join(", ");
});
messages = messages + errors.join("; ") + ".";
$("<p class=\"flash-alert\">" + messages + "</p>").insertBefore("section.container-fluid:first");
});
}

/* Apparently a lock variable to prevent multiple form submissions */
var edition_form_saved = false;
$('#save-edition').submit(function (e) {
e.preventDefault();

var edition_form = $('#edition-form');
if (! edition_form_saved) {
edition_form_saved = true;
edition_form.trigger('submit');
}
});

if (! 'autofocus' in document.createElement('input')) {
$('*[autofocus]').focus();
}

/* Apparently a lock variable to prevent multiple form submissions */
var edition_form_submitted = false;

/*
Mark the edition form as dirty to prevent accidental navigation away from
the edition form (such as by clicking the "Edit in Panopticon" link)
Expand All @@ -51,28 +13,17 @@ $(function () {

$('#edition-form').change(function () {
edition_form_dirty = true;
edition_form_submitted = false;
});

$('#edition-form').submit(function() {
edition_form_dirty = false;
/* prevent multiple form submissions */
$("#save-edition").attr('disabled', true);
});

$(window).bind('beforeunload', function() {
if (edition_form_dirty) {
return 'You have unsaved changes to this edition.';
}
});

$('.also_save_edition').submit(function () {
var edition_form = $('#edition-form');
var this_form = $(this);

if (edition_form_dirty && ! edition_form_submitted) {
submit_form(edition_form, function () {
edition_form_dirty = false;
edition_form_submitted = true;
});
}
});
});
17 changes: 13 additions & 4 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@ def update
# it at the wrong time.
assign_to = new_assignee

activity_attempted = Edition::BASIC_EVENTS.invert[params[:commit]]
activity_params = params[:edition]["activity_#{activity_attempted}_attributes"]

update! do |success, failure|
success.html {
progress_edition(resource, activity_params) if activity_attempted
update_assignment resource, assign_to
return_to = params[:return_to] || edition_path(resource)
redirect_to return_to
Expand All @@ -70,6 +74,7 @@ def update
render :template => "show"
}
success.json {
progress_edition(resource, activity_params) if activity_attempted
update_assignment resource, assign_to
render :json => resource
}
Expand All @@ -91,11 +96,10 @@ def destroy
end

def progress
command = EditionProgressor.new(resource, current_user)
if command.progress(squash_multiparameter_datetime_attributes(params[:activity]))
redirect_to edition_path(resource), notice: command.status_message
if progress_edition(resource, params[:edition][:activity])
redirect_to edition_path(resource), notice: @command.status_message
else
redirect_to edition_path(resource), alert: command.status_message
redirect_to edition_path(resource), alert: @command.status_message
end
end

Expand Down Expand Up @@ -144,6 +148,11 @@ def format_failure_message(resource)
"We had some problems saving. Please check the form below."
end

def progress_edition(edition, activity_params)
@command = EditionProgressor.new(resource, current_user)
@command.progress(squash_multiparameter_datetime_attributes(activity_params))
end

def report_state_counts
Publisher::Application.edition_state_count_reporter.report
end
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ def friendly_date(date)
end

include PathsHelper
include ProgressFormsHelper
include EditionActivityHelper
end
56 changes: 56 additions & 0 deletions app/helpers/edition_activity_buttons_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module EditionActivityButtonsHelper

def build_review_button(edition, activity, title)
check_method = "can_#{activity}?".to_sym
enabled = edition.send(check_method)

link_to title, "##{activity}_form", data: { toggle: 'modal'},
class: "btn btn-info #{"disabled" if !enabled} add-top-margin"
end

def review_buttons(edition)
[
["Needs more work", "request_amendments"],
["OK for publication", "approve_review"]
].map{ |title, activity|
build_review_button(edition, activity, title)
}.join("\n").html_safe
end

def fact_check_buttons(edition)
[
["Needs major changes", "request_amendments"],
["Minor or no changes required", "approve_fact_check"]
].map{ |title, activity|
build_review_button(edition, activity, title)
}.join("\n").html_safe
end

def progress_buttons(edition, options = {})
[
["Fact check", "send_fact_check"],
["2nd pair of eyes", "request_review"],
*scheduled_publishing_buttons(edition),
["Publish", "publish"],
].map { |title, activity, button_color = 'primary'|
enabled = edition.send("can_#{activity}?")
show_disabled = options.fetch(:show_disabled, true)
next unless show_disabled || enabled

link_to title, "##{activity}_form", data: { toggle: 'modal'},
class: "btn btn-large btn-#{button_color} #{"disabled" if !enabled}"
}.join("\n").html_safe
end

def scheduled_publishing_buttons(edition)
buttons = []
buttons << ["Schedule", "schedule_for_publishing", 'warning'] if edition.can_schedule_for_publishing?
buttons << ["Cancel scheduled publishing", "cancel_scheduled_publishing", 'danger'] if edition.can_cancel_scheduled_publishing?
buttons
end

def preview_button(edition)
link_to('Preview', preview_edition_path(edition), class: 'btn btn-primary btn-large')
end

end
45 changes: 45 additions & 0 deletions app/helpers/edition_activity_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module EditionActivityHelper
def edition_activities_fields(f, edition)
activities_fields = Edition::BASIC_EVENTS.map do |activity, title|
content_tag(:div, modal_attributes.merge(id: "#{activity}_form")) do
edition_activity_fields(edition, title, activity, f, inline: true)
end
end

activities_fields.join("\n").html_safe
end

def edition_activities_forms(edition, activities)
activities_forms = activities.map do |activity, title|
semantic_form_for(:edition, url: progress_edition_path(edition),
html: modal_attributes.merge(id: "#{activity}_form")) do |f|
edition_activity_fields(edition, title, activity, f, inline: false)
end
end

activities_forms.join("\n").html_safe
end

def edition_activity_fields(edition, title, activity, form_builder, options)
render(
:partial => 'shared/edition_activity_fields',
:locals => {
:form_builder => form_builder, :title => title, :activity => activity,
:inline => options[:inline], :disabled => !edition.send("can_#{activity}?".to_sym)
}
)
end

def review_forms(edition)
edition_activities_forms(edition, Edition::REVIEW_EVENTS)
end

def fact_check_forms(edition)
edition_activities_forms(edition, Edition::FACT_CHECK_EVENTS)
end

def modal_attributes
{ :role => 'dialog', :class => 'modal', :tabindex => -1, 'aria-hidden' => true }
end

end
101 changes: 0 additions & 101 deletions app/helpers/progress_forms_helper.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/views/answers/_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<%= semantic_form_for @resource, :url => edition_path(@resource), :as => :edition, :html => { :id => 'edition-form' } do |f| %>
<%= f.inputs do %>
<%= render :partial => 'shared/title_etc', :locals => {:f => f} %>
<%= render :partial => 'shared/common_edition_attributes', :locals => {:f => f} %>
<div class="row">
<div class="col-md-10">
<%= f.input :body, :as => :text, :input_html => { :disabled => @resource.locked_for_edits? } %>
</div>
</div>
<% end %>

<%= render partial: 'shared/workflow_buttons', locals: { f: f } %>
<% end %>
3 changes: 2 additions & 1 deletion app/views/business_supports/_edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="row">
<div class="col-md-8">
<%= f.inputs do %>
<%= render :partial => 'shared/title_etc', :locals => {:f => f} %>
<%= render :partial => 'shared/common_edition_attributes', :locals => {:f => f} %>
<%= render :partial => 'business_supports/fields', :locals => { :f => f, :locked_for_edits => @resource.locked_for_edits? } %>
<% end %>
</div>
Expand All @@ -15,4 +15,5 @@
</div>
</div>

<%= render partial: 'shared/workflow_buttons', locals: { f: f } %>
<% end %>
4 changes: 3 additions & 1 deletion app/views/campaigns/_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= semantic_form_for @resource, :url => edition_path(@resource), :as => :edition, :html => { :id => 'edition-form' } do |f| %>
<%= f.inputs do %>
<%= render :partial => 'shared/title_etc', :locals => {:f => f} %>
<%= render :partial => 'shared/common_edition_attributes', :locals => {:f => f} %>

<%= f.inputs "Campaign images" do %>
<table class="table table-bordered image-upload remove-bottom-margin">
Expand Down Expand Up @@ -50,4 +50,6 @@
</div>
</div>
<% end %>

<%= render partial: 'shared/workflow_buttons', locals: { f: f } %>
<% end %>
4 changes: 3 additions & 1 deletion app/views/completed_transactions/_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= semantic_form_for @resource, :url => edition_path(@resource), :as => :edition, :html => { :id => 'edition-form' } do |f| %>
<%= f.inputs do %>
<%= render :partial => 'shared/title_etc', :locals => {:f => f} %>
<%= render :partial => 'shared/common_edition_attributes', :locals => {:f => f} %>
<div class="row">
<div class="col-md-10">
<%= f.input :body,
Expand All @@ -10,4 +10,6 @@
</div>
</div>
<% end %>

<%= render partial: 'shared/workflow_buttons', locals: { f: f } %>
<% end %>
Loading

0 comments on commit f20d95f

Please sign in to comment.