From cc6bf6e9614c736df99200a9054f20f7eb50b04d Mon Sep 17 00:00:00 2001 From: Tony Vincent Date: Fri, 5 Jul 2024 13:36:18 +0200 Subject: [PATCH] Enable syncing all accounts in one click (#948) * Enable syncing all accounts on one click * Remove argument to sync_later method call * Add partial for sync all accounts button * Redirect back if possible when syncing all accounts --- app/controllers/accounts_controller.rb | 16 ++++++++++++++++ app/views/accounts/_header.html.erb | 2 ++ app/views/accounts/_sync_all_button.html.erb | 3 +++ app/views/accounts/index.html.erb | 2 ++ config/locales/views/accounts/en.yml | 3 +++ config/routes.rb | 1 + test/controllers/accounts_controller_test.rb | 6 ++++++ 7 files changed, 33 insertions(+) create mode 100644 app/views/accounts/_sync_all_button.html.erb diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 4aa41e54..4633d8e9 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -69,6 +69,22 @@ class AccountsController < ApplicationController redirect_to account_path(@account), notice: t(".success") end + def sync_all + synced_accounts_count = 0 + Current.family.accounts.each do |account| + next unless account.can_sync? + + account.sync_later + synced_accounts_count += 1 + end + + if synced_accounts_count > 0 + redirect_back_or_to accounts_path, notice: t(".success", count: synced_accounts_count) + else + redirect_back_or_to accounts_path, alert: t(".no_accounts_to_sync") + end + end + private def set_account diff --git a/app/views/accounts/_header.html.erb b/app/views/accounts/_header.html.erb index f8c8eb3e..dcf2c914 100644 --- a/app/views/accounts/_header.html.erb +++ b/app/views/accounts/_header.html.erb @@ -17,5 +17,7 @@ <%= lucide_icon("plus", class: "w-5 h-5") %>

<%= t(".new") %>

<% end %> + + <%= render "sync_all_button" %> diff --git a/app/views/accounts/_sync_all_button.html.erb b/app/views/accounts/_sync_all_button.html.erb new file mode 100644 index 00000000..c24ee93a --- /dev/null +++ b/app/views/accounts/_sync_all_button.html.erb @@ -0,0 +1,3 @@ +<%= button_to sync_all_accounts_path, method: :post, class: "rounded-lg bg-gray-900 text-white flex items-center gap-1 justify-center hover:bg-gray-700 px-3 py-2", title: "Sync All" do %> + <%= lucide_icon "refresh-cw", class: "w-5 h-5" %> +<% end %> diff --git a/app/views/accounts/index.html.erb b/app/views/accounts/index.html.erb index 9c5a88b0..6ef4a852 100644 --- a/app/views/accounts/index.html.erb +++ b/app/views/accounts/index.html.erb @@ -24,6 +24,8 @@ <%= lucide_icon("plus", class: "w-5 h-5") %>

<%= t(".new_account") %>

<% end %> + + <%= render "sync_all_button" %> diff --git a/config/locales/views/accounts/en.yml b/config/locales/views/accounts/en.yml index 65c63348..6a55b619 100644 --- a/config/locales/views/accounts/en.yml +++ b/config/locales/views/accounts/en.yml @@ -65,5 +65,8 @@ en: new: New account sync: success: Account sync started + sync_all: + no_accounts_to_sync: No accounts were eligible for syncing. + success: Successfully queued %{count} accounts for syncing. update: success: Account updated diff --git a/config/routes.rb b/config/routes.rb index 65f1bf20..eee50831 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,6 +63,7 @@ Rails.application.routes.draw do collection do get :summary get :list + post :sync_all end member do diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 63cf0e81..629a19bd 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -30,6 +30,12 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest assert_redirected_to account_url(@account) end + test "can sync all accounts" do + post sync_all_accounts_path + assert_redirected_to accounts_url + assert_equal "Successfully queued #{ @user.family.accounts.size } accounts for syncing.", flash[:notice] + end + test "should update account" do patch account_url(@account), params: { account: {