Skip to content

Commit

Permalink
Merge pull request #2593 from alphagov/refactor_button_code
Browse files Browse the repository at this point in the history
Refactor single page notification button logic prior to adding new functionality
  • Loading branch information
hannako authored Nov 7, 2022
2 parents ce06e29 + 4228bb8 commit 38faada
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 20 deletions.
5 changes: 2 additions & 3 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ def render_template
return
end

# use these and `@content_item.base_path` in the template
@notification_button_visible = @content_item.has_single_page_notifications?
@include_single_page_notification_button_js = account_session_header.present?
# use this and `@content_item.base_path` in the template
@has_govuk_account = account_session_header.present?

request.variant = :print if params[:variant] == "print"

Expand Down
1 change: 1 addition & 0 deletions app/presenters/consultation_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ConsultationPresenter < ContentItemPresenter
include ContentItem::Shareable
include ContentItem::TitleAndContext
include ContentItem::Attachments
include ContentItem::SinglePageNotificationButton

def opening_date_time
content_item["details"]["opening_date"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ module SinglePageNotificationButton
5f9c6c15-7631-11e4-a3cb-005056011aef
].freeze

ALLOWED_DOCUMENT_TYPES = %w[
publication
detailed_guide
consultation
].freeze

def has_single_page_notifications?
(!EXEMPTION_LIST.include? content_item["content_id"]) && (ALLOWED_DOCUMENT_TYPES.include? content_item["schema_name"])
!EXEMPTION_LIST.include? content_item["content_id"]
end
end
end
5 changes: 4 additions & 1 deletion app/presenters/content_item_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class ContentItemPresenter
include ContentItem::Withdrawable
include ContentItem::SinglePageNotificationButton
attr_reader :content_item,
:requested_path,
:view_context,
Expand Down Expand Up @@ -47,6 +46,10 @@ def requesting_a_service_sign_in_page?
false
end

def has_single_page_notifications?
false
end

def available_translations
translations = content_item["links"]["available_translations"] || []

Expand Down
1 change: 1 addition & 0 deletions app/presenters/detailed_guide_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class DetailedGuidePresenter < ContentItemPresenter
include ContentItem::NationalApplicability
include ContentItem::Political
include ContentItem::TitleAndContext
include ContentItem::SinglePageNotificationButton

def title_and_context
super.tap do |t|
Expand Down
1 change: 1 addition & 0 deletions app/presenters/publication_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class PublicationPresenter < ContentItemPresenter
include ContentItem::NationalStatisticsLogo
include ContentItem::Political
include ContentItem::Attachments
include ContentItem::SinglePageNotificationButton

def details
content_item["details"]["body"]
Expand Down
1 change: 1 addition & 0 deletions app/views/content_items/consultation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</div>

<%= render 'shared/publisher_metadata_with_logo', content_item: @content_item %>
<%= render 'shared/single_page_notification_button', content_item: @content_item %>
<%= render 'shared/history_notice', content_item: @content_item %>
<%= render 'govuk_publishing_components/components/notice', @content_item.withdrawal_notice_component %>

Expand Down
1 change: 1 addition & 0 deletions app/views/content_items/detailed_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
</div>

<%= render 'shared/publisher_metadata_with_logo' %>
<%= render 'shared/single_page_notification_button', content_item: @content_item %>

<%= render 'shared/history_notice', content_item: @content_item %>
<%= render 'govuk_publishing_components/components/notice', @content_item.withdrawal_notice_component %>
Expand Down
1 change: 1 addition & 0 deletions app/views/content_items/publication.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
</div>

<%= render 'shared/publisher_metadata_with_logo' %>
<%= render 'shared/single_page_notification_button', content_item: @content_item %>

<%= render 'shared/history_notice', content_item: @content_item %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
} %>
<%= render 'govuk_publishing_components/components/single_page_notification_button', {
base_path: @content_item.base_path,
js_enhancement: @include_single_page_notification_button_js,
js_enhancement: @has_govuk_account,
button_location: "bottom",
margin_bottom: 0,
} if @notification_button_visible %>
} if @content_item.has_single_page_notifications? %>
7 changes: 0 additions & 7 deletions app/views/shared/_publisher_metadata_with_logo.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@
</div>
</div>
</div>

<%= render 'govuk_publishing_components/components/single_page_notification_button', {
base_path: @content_item.base_path,
js_enhancement: @include_single_page_notification_button_js,
margin_bottom: 6,
button_location: "top",
} if @notification_button_visible %>
6 changes: 6 additions & 0 deletions app/views/shared/_single_page_notification_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= render 'govuk_publishing_components/components/single_page_notification_button', {
base_path: @content_item.base_path,
js_enhancement: @has_govuk_account,
margin_bottom: 6,
button_location: "top",
} if @content_item.has_single_page_notifications? %>
7 changes: 7 additions & 0 deletions test/presenters/consultation_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,12 @@ def schema_name
assert_equal "https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fwww.test.gov.uk%2Fgovernment%2Fconsultations%2Fpostgraduate-doctoral-loans", presented_item("open_consultation").share_links[0][:href]
assert_equal "https://twitter.com/share?url=https%3A%2F%2Fwww.test.gov.uk%2Fgovernment%2Fconsultations%2Fpostgraduate-doctoral-loans&text=Postgraduate%20doctoral%20loans", presented_item("open_consultation").share_links[1][:href]
end

test "presents the single page notification button" do
schema = schema_item("open_consultation")
presented = presented_item("open_consultation", schema)

assert presented.has_single_page_notifications?
end
end
end
5 changes: 5 additions & 0 deletions test/presenters/detailed_guide_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ def schema_name
}
assert_equal presented.logo, expected
end

test "presents the single page notification button" do
presented = presented_item("national_applicability_detailed_guide")
assert presented.has_single_page_notifications?
end
end
5 changes: 5 additions & 0 deletions test/presenters/publication_presenter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ def schema_name
assert_equal(presented.national_applicability[:wales][:applicable], false)
assert_equal(presented.national_applicability[:wales][:alternative_url], "http://wales.gov.uk/topics/statistics/headlines/housing2012/121025/?lang=en")
end

test "presents the single page notification button" do
presented = presented_item("statistics_publication")
assert presented.has_single_page_notifications?
end
end

0 comments on commit 38faada

Please sign in to comment.