Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Group users by family in `/admin/users` (#1139)

Juan José Mata7fce804c89ad
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index d460b1ac5e..f5a3ae953b 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -13,7 +13,7 @@ module Admin
scope = scope.where(role: params[:role]) if params[:role].present?
scope = apply_trial_filter(scope) if params[:trial_status].present?
- @users = scope.order(
+ users = scope.order(
Arel.sql(
"CASE " \
"WHEN subscriptions.status = 'trialing' THEN 0 " \
@@ -23,14 +23,18 @@ module Admin
)
)
- family_ids = @users.map(&:family_id).uniq
+ family_ids = users.map(&:family_id).uniq
@accounts_count_by_family = Account.where(family_id: family_ids).group(:family_id).count
@entries_count_by_family = Entry.joins(:account).where(accounts: { family_id: family_ids }).group("accounts.family_id").count
- user_ids = @users.map(&:id).uniq
+ user_ids = users.map(&:id).uniq
@last_login_by_user = Session.where(user_id: user_ids).group(:user_id).maximum(:created_at)
@sessions_count_by_user = Session.where(user_id: user_ids).group(:user_id).count
+ @families_with_users = users.group_by(&:family).sort_by do |family, _users|
+ -(@entries_count_by_family[family.id] || 0)
+ end
+
@trials_expiring_in_7_days = Subscription
.where(status: :trialing)
.where(trial_ends_at: Time.current..7.days.from_now)
diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb
index abc09a6526..0c21d20600 100644
--- a/app/views/admin/users/index.html.erb
+++ b/app/views/admin/users/index.html.erb
@@ -43,80 +43,110 @@
</div>
</div>
- <!-- Users table -->
+ <!-- Families/Groups & Users -->
<div>
<h2 class="text-lg font-semibold text-primary mb-3"><%= t(".section_title") %></h2>
- <div class="bg-container-inset rounded-lg overflow-hidden">
- <% if @users.any? %>
- <table class="w-full">
- <thead class="bg-surface-default border-b border-primary">
- <tr>
- <th class="px-4 py-3 text-left text-xs font-medium text-secondary uppercase"><%= t(".table.user") %></th>
- <th class="px-4 py-3 text-left text-xs font-medium text-secondary uppercase"><%= t(".table.trial_ends_at") %></th>
- <th class="px-4 py-3 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.family_accounts") %></th>
- <th class="px-4 py-3 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.family_transactions") %></th>
- <th class="px-4 py-3 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.role") %></th>
- </tr>
- </thead>
- <tbody class="divide-y divide-primary">
- <% @users.each do |user| %>
- <tr>
- <td class="px-4 py-3">
- <div class="flex items-center gap-3">
- <div class="w-8 h-8 rounded-full bg-surface flex items-center justify-center shrink-0">
- <span class="text-sm font-medium text-primary"><%= user.initials %></span>
- </div>
- <div>
- <p class="font-medium text-primary"><%= user.display_name %></p>
- <p class="text-sm text-secondary"><%= user.email %></p>
- <p class="text-xs text-secondary mt-1 space-y-0.5">
- <span class="block"><%= t(".table.last_login") %>: <%= @last_login_by_user[user.id]&.to_fs(:long) || t(".table.never") %></span>
- <span class="block"><%= t(".table.session_count") %>: <%= number_with_delimiter(@sessions_count_by_user[user.id] || 0) %></span>
- </p>
- </div>
- </div>
- </td>
- <td class="px-4 py-3 text-sm text-primary whitespace-nowrap">
- <%= user.family.subscription&.trial_ends_at&.to_fs(:long) || t(".not_available") %>
- </td>
- <td class="px-4 py-3 text-sm text-primary text-right whitespace-nowrap">
- <%= number_with_delimiter(@accounts_count_by_family[user.family_id] || 0) %>
- </td>
- <td class="px-4 py-3 text-sm text-primary text-right whitespace-nowrap">
- <%= number_with_delimiter(@entries_count_by_family[user.family_id] || 0) %>
- </td>
- <td class="px-4 py-3 text-right">
- <% if user.id == Current.user.id %>
- <span class="text-sm text-secondary"><%= t(".you") %></span>
- <% else %>
- <%= form_with model: [:admin, user], method: :patch, class: "flex items-center justify-end gap-2" do |form| %>
- <%= form.select :role,
- options_for_select([
- [t(".roles.guest"), "guest"],
- [t(".roles.member", default: "Member"), "member"],
- [t(".roles.admin"), "admin"],
- [t(".roles.super_admin"), "super_admin"]
- ], user.role),
- {},
- class: "text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1",
- onchange: "this.form.requestSubmit()" %>
- <% end %>
+
+ <% if @families_with_users.any? %>
+ <div class="space-y-4">
+ <% @families_with_users.each do |family, users| %>
+ <details class="bg-container-inset rounded-lg overflow-hidden group">
+ <summary class="flex items-center justify-between gap-4 px-4 py-3 cursor-pointer select-none hover:bg-surface-hover">
+ <div class="flex items-center gap-3">
+ <%= icon "users", class: "w-5 h-5 text-secondary shrink-0" %>
+ <div>
+ <p class="font-semibold text-primary"><%= family.name.presence || t(".unnamed_family") %></p>
+ <p class="text-xs text-secondary">
+ <%= t(".family_summary",
+ members: users.size,
+ accounts: number_with_delimiter(@accounts_count_by_family[family.id] || 0),
+ transactions: number_with_delimiter(@entries_count_by_family[family.id] || 0)) %>
+ </p>
+ </div>
+ </div>
+ <div class="flex items-center gap-4 shrink-0">
+ <% sub = family.subscription %>
+ <% if sub&.trialing? %>
+ <span class="text-xs text-secondary">
+ <%= t(".table.trial_ends_at") %>: <%= sub.trial_ends_at&.to_fs(:long) || t(".not_available") %>
+ </span>
+ <% elsif sub %>
+ <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium
+ <%= sub.active? ? 'bg-green-100 text-green-800' : 'bg-surface text-secondary' %>">
+ <%= sub.status.humanize %>
+ </span>
+ <% else %>
+ <span class="text-xs text-secondary"><%= t(".no_subscription") %></span>
+ <% end %>
+ <%= icon "chevron-down", class: "w-4 h-4 text-secondary transition-transform group-open:rotate-180" %>
+ </div>
+ </summary>
+
+ <div class="border-t border-primary">
+ <table class="w-full">
+ <thead class="bg-surface-default border-b border-primary">
+ <tr>
+ <th class="px-4 py-2 text-left text-xs font-medium text-secondary uppercase"><%= t(".table.user") %></th>
+ <th class="px-4 py-2 text-left text-xs font-medium text-secondary uppercase"><%= t(".table.last_login") %></th>
+ <th class="px-4 py-2 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.session_count") %></th>
+ <th class="px-4 py-2 text-right text-xs font-medium text-secondary uppercase"><%= t(".table.role") %></th>
+ </tr>
+ </thead>
+ <tbody class="divide-y divide-primary">
+ <% users.each do |user| %>
+ <tr>
+ <td class="px-4 py-3">
+ <div class="flex items-center gap-3">
+ <div class="w-8 h-8 rounded-full bg-surface flex items-center justify-center shrink-0">
+ <span class="text-sm font-medium text-primary"><%= user.initials %></span>
+ </div>
+ <div>
+ <p class="font-medium text-primary"><%= user.display_name %></p>
+ <p class="text-sm text-secondary"><%= user.email %></p>
+ </div>
+ </div>
+ </td>
+ <td class="px-4 py-3 text-sm text-primary whitespace-nowrap">
+ <%= @last_login_by_user[user.id]&.to_fs(:long) || t(".table.never") %>
+ </td>
+ <td class="px-4 py-3 text-sm text-primary text-right whitespace-nowrap">
+ <%= number_with_delimiter(@sessions_count_by_user[user.id] || 0) %>
+ </td>
+ <td class="px-4 py-3 text-right">
+ <% if user.id == Current.user.id %>
+ <span class="text-sm text-secondary"><%= t(".you") %></span>
+ <% else %>
+ <%= form_with model: [:admin, user], method: :patch, class: "flex items-center justify-end gap-2", data: { controller: "auto-submit-form" } do |form| %>
+ <%= form.select :role,
+ options_for_select([
+ [t(".roles.guest"), "guest"],
+ [t(".roles.member", default: "Member"), "member"],
+ [t(".roles.admin"), "admin"],
+ [t(".roles.super_admin"), "super_admin"]
+ ], user.role),
+ {},
+ class: "text-sm rounded-lg border border-primary bg-container text-primary px-2 py-1",
+ data: { auto_submit_form_target: "auto" } %>
+ <% end %>
+ <% end %>
+ </td>
+ </tr>
<% end %>
- </td>
- </tr>
- <% end %>
- </tbody>
- </table>
- <% else %>
- <div class="p-8 text-center">
- <%= icon "users", class: "w-12 h-12 mx-auto text-secondary mb-3" %>
- <p class="text-secondary"><%= t(".no_users") %></p>
- </div>
- <% end %>
- </div>
+ </tbody>
+ </table>
+ </div>
+ </details>
+ <% end %>
+ </div>
+ <% else %>
+ <div class="bg-container-inset rounded-lg p-8 text-center">
+ <%= icon "users", class: "w-12 h-12 mx-auto text-secondary mb-3" %>
+ <p class="text-secondary"><%= t(".no_users") %></p>
+ </div>
+ <% end %>
</div>
- <%= settings_section title: t(".role_descriptions_title"), collapsible: true, open: false do %>
+ <%= settings_section title: t(".role_descriptions_title"), collapsible: true, open: true do %>
<div class="space-y-3 text-sm">
<div class="flex items-start gap-3">
<span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-surface text-primary shrink-0">
diff --git a/config/locales/views/admin/users/en.yml b/config/locales/views/admin/users/en.yml
index 7eb7102a74..b14feb7853 100644
--- a/config/locales/views/admin/users/en.yml
+++ b/config/locales/views/admin/users/en.yml
@@ -5,11 +5,14 @@ en:
index:
title: "User Management"
description: "Manage user roles for your instance. Super admins can access SSO provider settings and user management."
- section_title: "Users"
+ section_title: "Families / Groups"
you: "(You)"
trial_ends_at: "Trial ends"
not_available: "n/a"
no_users: "No users found."
+ unnamed_family: "Unnamed Family/Group"
+ no_subscription: "No subscription"
+ family_summary: "%{members} members · %{accounts} accounts · %{transactions} transactions"
filters:
role: "Role"
role_all: "All roles"
diff --git a/test/controllers/admin/users_controller_test.rb b/test/controllers/admin/users_controller_test.rb
index e23180e2cc..1273c4b18e 100644
--- a/test/controllers/admin/users_controller_test.rb
+++ b/test/controllers/admin/users_controller_test.rb
@@ -5,43 +5,45 @@ class Admin::UsersControllerTest < ActionDispatch::IntegrationTest
sign_in users(:sure_support_staff)
end
- test "index sorts users by subscription trial end date with nils last" do
- user_with_trial = User.find_by!(email: "[email protected]")
- user_without_trial = User.find_by!(email: "[email protected]")
+ test "index groups users by family sorted by transaction count" do
+ family_with_more = users(:family_admin).family
+ family_with_fewer = users(:empty).family
- user_with_trial.family.subscription&.destroy
- Subscription.create!(
- family_id: user_with_trial.family_id,
- status: :trialing,
- trial_ends_at: 2.days.from_now
- )
-
- user_without_trial.family.subscription&.destroy
- Subscription.create!(
- family_id: user_without_trial.family_id,
- status: :active,
- trial_ends_at: nil,
- stripe_id: "cus_test_#{user_without_trial.family_id}"
- )
+ account = Account.create!(family: family_with_more, name: "Test", balance: 0, currency: "USD", accountable: Depository.new)
+ 3.times { |i| account.entries.create!(name: "Txn #{i}", date: Date.current, amount: 10, currency: "USD", entryable: Transaction.new) }
get admin_users_url
-
assert_response :success
body = response.body
- trial_user_index = body.index("[email protected]")
- no_trial_user_index = body.index("[email protected]")
+ more_idx = body.index(family_with_more.name)
+ fewer_idx = body.index(family_with_fewer.name)
- assert_not_nil trial_user_index
- assert_not_nil no_trial_user_index
- assert_operator trial_user_index, :<, no_trial_user_index,
- "User with trialing subscription ([email protected]) should appear before user with non-trial subscription ([email protected])"
+ assert_not_nil more_idx
+ assert_not_nil fewer_idx
+ assert_operator more_idx, :<, fewer_idx,
+ "Family with more transactions should appear before family with fewer"
end
- test "index shows n/a when trial end date is unavailable" do
+ test "index shows subscription status for families" do
+ family = users(:family_admin).family
+ family.subscription&.destroy
+ Subscription.create!(
+ family_id: family.id,
+ status: :active,
+ stripe_id: "cus_test_#{family.id}"
+ )
+
get admin_users_url
+ assert_response :success
+ assert_match(/Active/, response.body, "Page should show subscription status for families with active subscriptions")
+ end
+
+ test "index shows no subscription label for families without subscription" do
+ users(:family_admin).family.subscription&.destroy
+ get admin_users_url
assert_response :success
- assert_match(/n\/a/, response.body, "Page should show n/a for users without trial end date")
+ assert_match(/No subscription/, response.body, "Page should show 'No subscription' for families without one")
end
end