Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Combine similar helper methods to reduce duplication.

I'm going to want to add an extra field to the publishing form in both
the normal & force publish modes, so it will make things easier if I
only have to add it in one place.
  • Loading branch information...
commit 9c9e669a4fd4ba17e22ea54bd21043c90ad1fd69 1 parent ea50deb
@floehopper floehopper authored
View
40 app/helpers/admin/document_actions_helper.rb
@@ -15,30 +15,15 @@ def submit_document_button(document)
end
end
- def publish_document_button(document)
+ def publish_document_form(document, options = {})
+ url = admin_document_publishing_path(document, options.slice(:force))
+ button_text = options[:force] ? "Force Publish" : "Publish"
+ button_title = "Publish #{document.title}"
+ confirm = publish_document_alerts(document, options[:force])
capture do
- form_for [:admin, document], {as: :document, url: admin_document_publishing_path(document), method: :post} do |form|
+ form_for [:admin, document], {as: :document, url: url, method: :post} do |form|
concat(form.hidden_field :lock_version)
- submit_options = {title: "Publish #{document.title}"}
- if document.has_supporting_pages?
- submit_options[:confirm] = "Have you checked the #{document.supporting_pages.count} supporting pages?"
- end
- concat(form.submit "Publish", submit_options)
- end
- end
- end
-
- def force_publish_document_button(document)
- capture do
- form_for [:admin, document], {as: :document, url: admin_document_publishing_path(document, force: true), method: :post} do |form|
- concat(form.hidden_field :lock_version)
- submit_options = {title: "Publish #{document.title}"}
- if document.has_supporting_pages?
- submit_options[:confirm] = "Are you sure you want to force publish this document? Have you checked the #{document.supporting_pages.count} supporting pages?"
- else
- submit_options[:confirm] = "Are you sure you want to force publish this document?"
- end
- concat(form.submit "Force Publish", submit_options)
+ concat(form.submit button_text, title: button_title, confirm: confirm)
end
end
end
@@ -62,4 +47,15 @@ def add_consultation_response_button(consultation)
def show_consultation_response_button(consultation)
link_to 'Show response', admin_consultation_response_path(consultation.latest_consultation_response), title: "Show response", class: "button"
end
+
+ private
+
+ def publish_document_alerts(document, force)
+ alerts = []
+ alerts << "Are you sure you want to force publish this document?" if force
+ if document.has_supporting_pages?
+ alerts << "Have you checked the #{document.supporting_pages.count} supporting pages?"
+ end
+ alerts.join(" ")
+ end
end
View
4 app/views/admin/documents/_navigation.html.erb
@@ -8,9 +8,9 @@
<%= submit_document_button(document) if document.submittable? %>
<% if document.publishable_by?(current_user) %>
- <%= publish_document_button(document) %>
+ <%= publish_document_form(document) %>
<% elsif document.force_publishable_by?(current_user) %>
- <%= force_publish_document_button(document) %>
+ <%= publish_document_form(document, force: true) %>
<% end %>
<%= link_to "Reject", new_admin_document_editorial_remark_path(document), class: "button" if document.rejectable_by?(current_user) %>
<%= delete_document_button(document) if document.deletable? %>
View
17 test/unit/helpers/admin/document_actions_helper_test.rb
@@ -1,28 +1,27 @@
require 'test_helper'
class Admin::DocumentActionsHelperTest < ActionView::TestCase
- test "should generate publish button form for document" do
+ test "should generate publish form for document" do
document = create(:submitted_document, title: "document-title")
- html = publish_document_button(document)
+ html = publish_document_form(document)
fragment = Nokogiri::HTML.fragment(html)
assert_equal admin_document_publishing_path(document), (fragment/"form").first["action"]
refute_nil (fragment/"input[name='document[lock_version]'][type=hidden]").first
assert_equal "Publish", (fragment/"input[type=submit]").first["value"]
assert_equal "Publish document-title", (fragment/"input[type=submit]").first["title"]
- assert_nil (fragment/"input[type=submit]").first["data-confirm"]
+ assert (fragment/"input[type=submit]").first["data-confirm"].blank?
end
- test "should generate publish button form for document with supporting pages alert" do
+ test "should generate publish form for document with supporting pages alert" do
document = create(:submitted_policy, supporting_pages: [create(:supporting_page)])
- publishing_path = admin_document_publishing_path(document)
- html = publish_document_button(document)
+ html = publish_document_form(document)
fragment = Nokogiri::HTML.fragment(html)
assert_equal "Have you checked the 1 supporting pages?", (fragment/"input[type=submit]").first["data-confirm"]
end
- test "should generate force-publish button form" do
+ test "should generate force-publish form" do
document = create(:submitted_document, title: "document-title")
- html = force_publish_document_button(document)
+ html = publish_document_form(document, force: true)
fragment = Nokogiri::HTML.fragment(html)
assert_equal admin_document_publishing_path(document, force: true), (fragment/"form").first["action"]
refute_nil (fragment/"input[name='document[lock_version]'][type=hidden]").first
@@ -33,7 +32,7 @@ class Admin::DocumentActionsHelperTest < ActionView::TestCase
test "should generate force-publish button form with supporting pages alert" do
document = create(:submitted_policy, supporting_pages: [create(:supporting_page)])
- html = force_publish_document_button(document)
+ html = publish_document_form(document, force: true)
fragment = Nokogiri::HTML.fragment(html)
assert_equal "Are you sure you want to force publish this document? Have you checked the 1 supporting pages?", (fragment/"input[type=submit]").first["data-confirm"]
end
Please sign in to comment.
Something went wrong with that request. Please try again.