Skip to content

Commit

Permalink
Merge pull request #9796 from alphagov/reorder-social-media-worldwide…
Browse files Browse the repository at this point in the history
…-org

Reorder social media worldwide org
  • Loading branch information
ryanb-gds authored Jan 23, 2025
2 parents 826b54f + fccbb2c commit 235cea4
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def update
end
end

def reorder
@reorderable_social_media_accounts = @edition.social_media_accounts
end

def order
@edition.social_media_accounts.reorder_without_callbacks!(params.require(:ordering))
redirect_to admin_edition_social_media_accounts_path(@edition), notice: "Social media accounts reordered"
end

private

def find_edition
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/edition/social_media_accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def process_associations_after_save(edition)
end

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

add_trait Trait
end
Expand Down
21 changes: 16 additions & 5 deletions app/views/admin/editionable_social_media_accounts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@
<%= link_to "Add new social media account", new_admin_edition_social_media_account_path(@edition), class: "govuk-link govuk-link--no-visited-state" %>
</p>

<%= render "govuk_publishing_components/components/heading", {
text: "Social media accounts",
font_size: "l",
margin_bottom: 3,
} %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/heading", {
text: "Social media accounts",
font_size: "l",
margin_bottom: 3,
} %>
</div>
<% if @edition.social_media_accounts.many? %>
<div class="govuk-grid-column-one-third">
<p class="govuk-body govuk-!-text-align-right">
<%= link_to("Reorder", reorder_admin_edition_social_media_accounts_path(@edition), class: "govuk-link govuk-link--no-visited-state") %>
</p>
</div>
<% end %>
</div>

<% if @edition.social_media_accounts.any? %>
<% @editionable_social_media_accounts_index_presenter.social_media_accounts.each do |social_media_account| %>
Expand Down
26 changes: 26 additions & 0 deletions app/views/admin/editionable_social_media_accounts/reorder.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% content_for :page_title, "Reorder social media accounts" %>
<% content_for :title, "Reorder social media accounts" %>
<% content_for :title_margin_bottom, 6 %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-full">
<%= form_with url: order_admin_edition_social_media_accounts_path(@edition), method: :put do %>
<%= render "govuk_publishing_components/components/reorderable_list", {
items: @reorderable_social_media_accounts.map do |account|
{
id: account.id,
title: account.title,
}
end,
} %>

<div class="govuk-button-group govuk-!-margin-bottom-6">
<%= render "govuk_publishing_components/components/button", {
text: "Update order",
} %>

<%= link_to("Cancel", admin_edition_social_media_accounts_path(@edition), class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
</div>
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def redirect(path, options = { prefix: Whitehall.router_prefix })
resources :lead_images, controller: "edition_lead_images", only: %i[update]
resources :social_media_accounts, only: %i[create destroy edit index new update], controller: "editionable_social_media_accounts" do
get :confirm_destroy, on: :member
collection do
get :reorder
put :order
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddOrderingToSocialMediaAccounts < ActiveRecord::Migration[7.1]
def change
add_column :social_media_accounts, :ordering, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2025_01_06_160308) do
ActiveRecord::Schema[7.1].define(version: 2025_01_08_102234) do
create_table "assets", charset: "utf8mb3", force: :cascade do |t|
t.string "asset_manager_id", null: false
t.string "variant", null: false
Expand Down Expand Up @@ -1006,6 +1006,7 @@
t.datetime "created_at", precision: nil
t.datetime "updated_at", precision: nil
t.string "socialable_type"
t.integer "ordering"
t.index ["social_media_service_id"], name: "index_social_media_accounts_on_social_media_service_id"
t.index ["socialable_id"], name: "index_social_media_accounts_on_organisation_id"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te
}

assert_response :redirect
assert_equal "Account title", @edition.social_media_accounts.last.title
assert_equal "https://www.social.gov.uk", @edition.social_media_accounts.last.url
account = @edition.social_media_accounts.where(title: "Account title").first
assert_equal "https://www.social.gov.uk", account.url
end

view_test "POST :create with invalid data shows errors" do
Expand Down Expand Up @@ -146,4 +146,32 @@ class Admin::EditionableSocialMediaAccountsControllerTest < ActionController::Te
assert_response :redirect
assert_empty @edition.social_media_accounts
end

view_test "GET :reorder displays the reorderable social media accounts" do
edition = create(:worldwide_organisation)
social_media_accounts = create_list(:social_media_account, 2, socialable: edition)
get :reorder, params: { edition_id: edition.id }

assert_response :success
assert_select ".gem-c-reorderable-list__item", count: social_media_accounts.size
assert_select "form" do
assert_select "[action=?]", order_admin_edition_social_media_accounts_path(edition)
end
end

test "PUT :order updates the ordering of social media accounts" do
edition = create(:worldwide_organisation)
social_media_account1 = create(:social_media_account, socialable: edition, ordering: 1)
social_media_account2 = create(:social_media_account, socialable: edition, ordering: 2)

put :order,
params: { edition_id: edition.id,
ordering: {
social_media_account2.id.to_s => "1",
social_media_account1.id.to_s => "2",
} }

assert_response :redirect
assert_equal [social_media_account2, social_media_account1], edition.reload.social_media_accounts
end
end

0 comments on commit 235cea4

Please sign in to comment.