Skip to content

Commit 235cea4

Browse files
authored
Merge pull request #9796 from alphagov/reorder-social-media-worldwide-org
Reorder social media worldwide org
2 parents 826b54f + fccbb2c commit 235cea4

File tree

8 files changed

+93
-9
lines changed

8 files changed

+93
-9
lines changed

app/controllers/admin/editionable_social_media_accounts_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ def update
4646
end
4747
end
4848

49+
def reorder
50+
@reorderable_social_media_accounts = @edition.social_media_accounts
51+
end
52+
53+
def order
54+
@edition.social_media_accounts.reorder_without_callbacks!(params.require(:ordering))
55+
redirect_to admin_edition_social_media_accounts_path(@edition), notice: "Social media accounts reordered"
56+
end
57+
4958
private
5059

5160
def find_edition

app/models/concerns/edition/social_media_accounts.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def process_associations_after_save(edition)
1616
end
1717

1818
included do
19-
has_many :social_media_accounts, as: :socialable, dependent: :destroy, autosave: true
19+
has_many :social_media_accounts, -> { extending(UserOrderableExtension).order(:ordering) }, as: :socialable, dependent: :destroy, autosave: true
2020

2121
add_trait Trait
2222
end

app/views/admin/editionable_social_media_accounts/index.html.erb

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@
1818
<%= link_to "Add new social media account", new_admin_edition_social_media_account_path(@edition), class: "govuk-link govuk-link--no-visited-state" %>
1919
</p>
2020

21-
<%= render "govuk_publishing_components/components/heading", {
22-
text: "Social media accounts",
23-
font_size: "l",
24-
margin_bottom: 3,
25-
} %>
21+
<div class="govuk-grid-row">
22+
<div class="govuk-grid-column-two-thirds">
23+
<%= render "govuk_publishing_components/components/heading", {
24+
text: "Social media accounts",
25+
font_size: "l",
26+
margin_bottom: 3,
27+
} %>
28+
</div>
29+
<% if @edition.social_media_accounts.many? %>
30+
<div class="govuk-grid-column-one-third">
31+
<p class="govuk-body govuk-!-text-align-right">
32+
<%= link_to("Reorder", reorder_admin_edition_social_media_accounts_path(@edition), class: "govuk-link govuk-link--no-visited-state") %>
33+
</p>
34+
</div>
35+
<% end %>
36+
</div>
2637

2738
<% if @edition.social_media_accounts.any? %>
2839
<% @editionable_social_media_accounts_index_presenter.social_media_accounts.each do |social_media_account| %>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<% content_for :page_title, "Reorder social media accounts" %>
2+
<% content_for :title, "Reorder social media accounts" %>
3+
<% content_for :title_margin_bottom, 6 %>
4+
5+
<div class="govuk-grid-row">
6+
<div class="govuk-grid-column-full">
7+
<%= form_with url: order_admin_edition_social_media_accounts_path(@edition), method: :put do %>
8+
<%= render "govuk_publishing_components/components/reorderable_list", {
9+
items: @reorderable_social_media_accounts.map do |account|
10+
{
11+
id: account.id,
12+
title: account.title,
13+
}
14+
end,
15+
} %>
16+
17+
<div class="govuk-button-group govuk-!-margin-bottom-6">
18+
<%= render "govuk_publishing_components/components/button", {
19+
text: "Update order",
20+
} %>
21+
22+
<%= link_to("Cancel", admin_edition_social_media_accounts_path(@edition), class: "govuk-link govuk-link--no-visited-state") %>
23+
</div>
24+
<% end %>
25+
</div>
26+
</div>

config/routes.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
250250
resources :lead_images, controller: "edition_lead_images", only: %i[update]
251251
resources :social_media_accounts, only: %i[create destroy edit index new update], controller: "editionable_social_media_accounts" do
252252
get :confirm_destroy, on: :member
253+
collection do
254+
get :reorder
255+
put :order
256+
end
253257
end
254258
end
255259

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddOrderingToSocialMediaAccounts < ActiveRecord::Migration[7.1]
2+
def change
3+
add_column :social_media_accounts, :ordering, :integer
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.1].define(version: 2025_01_06_160308) do
13+
ActiveRecord::Schema[7.1].define(version: 2025_01_08_102234) do
1414
create_table "assets", charset: "utf8mb3", force: :cascade do |t|
1515
t.string "asset_manager_id", null: false
1616
t.string "variant", null: false
@@ -1006,6 +1006,7 @@
10061006
t.datetime "created_at", precision: nil
10071007
t.datetime "updated_at", precision: nil
10081008
t.string "socialable_type"
1009+
t.integer "ordering"
10091010
t.index ["social_media_service_id"], name: "index_social_media_accounts_on_social_media_service_id"
10101011
t.index ["socialable_id"], name: "index_social_media_accounts_on_organisation_id"
10111012
end

test/functional/admin/editionable_social_media_accounts_controller_test.rb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te
110110
}
111111

112112
assert_response :redirect
113-
assert_equal "Account title", @edition.social_media_accounts.last.title
114-
assert_equal "https://www.social.gov.uk", @edition.social_media_accounts.last.url
113+
account = @edition.social_media_accounts.where(title: "Account title").first
114+
assert_equal "https://www.social.gov.uk", account.url
115115
end
116116

117117
view_test "POST :create with invalid data shows errors" do
@@ -146,4 +146,32 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te
146146
assert_response :redirect
147147
assert_empty @edition.social_media_accounts
148148
end
149+
150+
view_test "GET :reorder displays the reorderable social media accounts" do
151+
edition = create(:worldwide_organisation)
152+
social_media_accounts = create_list(:social_media_account, 2, socialable: edition)
153+
get :reorder, params: { edition_id: edition.id }
154+
155+
assert_response :success
156+
assert_select ".gem-c-reorderable-list__item", count: social_media_accounts.size
157+
assert_select "form" do
158+
assert_select "[action=?]", order_admin_edition_social_media_accounts_path(edition)
159+
end
160+
end
161+
162+
test "PUT :order updates the ordering of social media accounts" do
163+
edition = create(:worldwide_organisation)
164+
social_media_account1 = create(:social_media_account, socialable: edition, ordering: 1)
165+
social_media_account2 = create(:social_media_account, socialable: edition, ordering: 2)
166+
167+
put :order,
168+
params: { edition_id: edition.id,
169+
ordering: {
170+
social_media_account2.id.to_s => "1",
171+
social_media_account1.id.to_s => "2",
172+
} }
173+
174+
assert_response :redirect
175+
assert_equal [social_media_account2, social_media_account1], edition.reload.social_media_accounts
176+
end
149177
end

0 commit comments

Comments
 (0)