From af7ecf81d0c838a1a05b5ff2d913dcd871dacae0 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Thu, 9 May 2019 22:54:32 +0200 Subject: [PATCH 1/2] sort members by last_sign_in_at by default --- app/controllers/users_controller.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4077113dc..398a80292 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,11 +2,11 @@ class UsersController < ApplicationController before_filter :authenticate_user! def index - search_and_load_members current_organization.members.active, {s: 'member_uid asc'} + search_and_load_members current_organization.members.active, { s: 'user_last_sign_in_at DESC' } end def manage - search_and_load_members current_organization.members, {s: 'member_uid asc'} + search_and_load_members current_organization.members, { s: 'member_uid ASC' } end def show @@ -60,8 +60,11 @@ def update def search_and_load_members(members_scope, default_search_params) @search = members_scope.ransack(default_search_params.merge(params.fetch(:q, {}))) - @members = - @search.result.eager_load(:account, :user).page(params[:page]).per(20) + result = @search.result + orders = result.orders.map { |order| order.direction == :asc ? "#{order.to_sql} NULLS FIRST" : "#{order.to_sql} NULLS LAST" } + result = result.except(:order).order(orders.join(", ")) if orders.count > 0 + + @members = result.eager_load(:account, :user).page(params[:page]).per(20) @member_view_models = @members.map { |m| MemberDecorator.new(m, self.class.helpers) } From 8712b410c808b9c1f7128136a3fba5d9126a7ba4 Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Fri, 10 May 2019 21:47:27 +0200 Subject: [PATCH 2/2] adapt specs to new behavior: members page sorted by last_sign_in_at desc by default --- spec/controllers/users_controller_spec.rb | 49 ++++++++++++++--------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index fbbde0a8c..698af6d57 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -40,12 +40,14 @@ describe "GET #index" do before { login(user) } - it 'sorts the users by their member_uid asc by default' do - member.increment!(:member_uid, Member.maximum(:member_uid) + 1) + it 'sorts the users by their last_sign_in_at desc by default' do + member.user.update_column(:last_sign_in_at, DateTime.now) + another_member.user.update_column(:last_sign_in_at, nil) get :index - expect(assigns(:members).last).to eq(member) + expect(assigns(:members).first).to eq(member) + expect(assigns(:members).last).to eq(another_member) end it 'allows to sort by member_uid' do @@ -81,8 +83,6 @@ context "with an normal logged user" do it "populates and array of users" do - login(user) - get "index" expect(assigns(:members).map(&:user)) @@ -101,9 +101,31 @@ end end + context 'when searching' do + it 'allows to search by member_uid' do + user = Fabricate(:user, username: 'foo', email: 'foo@email.com') + member = Fabricate(:member, user: user, organization: test_organization, member_uid: 1000) + + get :index, q: { user_username_or_user_email_or_member_uid_search_contains: 1000 } + + expect(assigns(:members)).to include(member) + end + end + end + + describe "GET #manage" do + before { login(user) } + + it 'sorts the users by their member_uid asc by default' do + member.increment!(:member_uid, Member.maximum(:member_uid) + 1) + + get :manage + + expect(assigns(:members).last).to eq(member) + end + context 'when sorting by balance' do before do - login(user) member_admin.account.update_attribute(:balance, 3600) end @@ -111,7 +133,7 @@ let(:direction) { 'desc' } it 'orders the rows by their balance' do - get :index, q: { s: "account_balance #{direction}" } + get :manage, q: { s: "account_balance #{direction}" } expect(assigns(:members).pluck(:user_id).first).to eq(admin_user.id) end @@ -121,23 +143,12 @@ let(:direction) { 'asc' } it 'orders the rows by their balance' do - get :index, q: { s: "account_balance #{direction}" } + get :manage, q: { s: "account_balance #{direction}" } expect(assigns(:members).pluck(:user_id).last).to eq(admin_user.id) end end end - - context 'when searching' do - it 'allows to search by member_uid' do - user = Fabricate(:user, username: 'foo', email: 'foo@email.com') - member = Fabricate(:member, user: user, organization: test_organization, member_uid: 1000) - - get :index, q: { user_username_or_user_email_or_member_uid_search_contains: 1000 } - - expect(assigns(:members)).to include(member) - end - end end describe "GET #show" do