Skip to content

Commit

Permalink
Split users between active and not active
Browse files Browse the repository at this point in the history
  • Loading branch information
cassar committed Sep 19, 2024
1 parent 0acce36 commit 7afe935
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 25 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/contributions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def new
def create
if contribution.save
flash[:success] = I18n.t('controllers.admin.contributions.create.success')
redirect_to admin_users_path
redirect_to active_admin_users_path
else
@user = member.user
flash.now[:alert] = contribution_error_message
Expand Down
16 changes: 10 additions & 6 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ class UsersController < ApplicationController
before_action :authenticate_user!
before_action :authorise_admin!

def index
@users = User.order(
Arel.sql('last_sign_in_at DESC NULLS LAST'),
Arel.sql('confirmed_at NULLS LAST')
).preload(:member)
@total_user_count = User.count
def active
@members = Member.active.preload(:user)
@total_member_count = @members.count
render :index
end

def inactive
@members = Member.where.not(id: Member.active).preload(:user)
@total_member_count = @members.count
render :index
end

private
Expand Down
18 changes: 9 additions & 9 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<% content_for :title, "Admin" %>
<h1>Users</h1>
<% if @users.any? %>
<h1>Members</h1>
<% if @members.any? %>
<article>
<p><b>Total Users:</b> <%= @total_user_count %></p>
<p><b>Total Members:</b> <%= @total_member_count %></p>
</article>
<table>
<thead>
Expand All @@ -14,18 +14,18 @@
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<% @members.each do |member| %>
<tr>
<td><%= user.email %></td>
<td><%= user.confirmed_at&.strftime('%a, %d %b %Y %H:%M:%S') %></td>
<td><%= user.last_sign_in_at&.strftime('%a, %d %b %Y %H:%M:%S') %></td>
<td><%= link_to "New Contribution", new_admin_member_contribution_path(user.member) %></td>
<td><%= member.user.email %></td>
<td><%= member.user.confirmed_at&.strftime('%a, %d %b %Y %H:%M:%S') %></td>
<td><%= member.user.last_sign_in_at&.strftime('%a, %d %b %Y %H:%M:%S') %></td>
<td><%= link_to "New Contribution", new_admin_member_contribution_path(member) %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<article>
<p>No Users.</p>
<p>No Members.</p>
</article>
<% end %>
3 changes: 2 additions & 1 deletion app/views/layouts/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
</ul>
<% if current_user.admin? %>
<ul>
<li><%= link_to 'Users', admin_users_path %></li>
<li><%= link_to 'Active Users', active_admin_users_path %></li>
<li><%= link_to 'Inactive Users', inactive_admin_users_path %></li>
</ul>
<% end %>
<% else %>
Expand Down
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

namespace :admin do
resources :dividends, only: [:show, :update]
resources :users, only: :index
resources :users, only: [] do
collection do
get :active
get :inactive
end
end
resources :members, only: [] do
resources :contributions, only: [:new, :create]
end
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/admin/contributions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ContributionsControllerTest < ActionDispatch::IntegrationTest
} }
end

assert_redirected_to admin_users_path
assert_redirected_to active_admin_users_path
assert_equal 'New contribution created and notification sent', flash[:success]
assert_equal 'xxxx', Contribution.last.transaction_identifier
end
Expand Down
33 changes: 27 additions & 6 deletions test/controllers/admin/users_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,45 @@

module Admin
class UsersControllerTest < ActionDispatch::IntegrationTest
test 'should redirect to sign in when not authenticated for index' do
get admin_users_path
test 'should redirect to sign in when not authenticated for active' do
get active_admin_users_path
assert_redirected_to new_user_session_path
end

test 'should redirect to sign in when not admin for index' do
test 'should redirect to sign in when not admin for active' do
assert_not users(:one).admin?
sign_in users(:one)

get admin_users_path
get active_admin_users_path
assert_redirected_to root_path
end

test 'should get index as admin' do
test 'should get active as admin' do
assert users(:admin).admin?
sign_in users(:admin)

get admin_users_path
get active_admin_users_path
assert_response :success
end

test 'should redirect to sign in when not authenticated for inactive' do
get inactive_admin_users_path
assert_redirected_to new_user_session_path
end

test 'should redirect to sign in when not admin for inactive' do
assert_not users(:one).admin?
sign_in users(:one)

get inactive_admin_users_path
assert_redirected_to root_path
end

test 'should get inactive as admin' do
assert users(:admin).admin?
sign_in users(:admin)

get inactive_admin_users_path
assert_response :success
end
end
Expand Down

0 comments on commit 7afe935

Please sign in to comment.