Fix sort functionality for group/project members

parent f54ddbf1
...@@ -6,19 +6,13 @@ class Groups::GroupMembersController < Groups::ApplicationController ...@@ -6,19 +6,13 @@ class Groups::GroupMembersController < Groups::ApplicationController
def index def index
@project = @group.projects.find(params[:project_id]) if params[:project_id] @project = @group.projects.find(params[:project_id]) if params[:project_id]
@members = @group.group_members @members = @group.group_members
@members = @members.non_invite unless can?(current_user, :admin_group, @group) @members = @members.non_invite unless can?(current_user, :admin_group, @group)
@members = @members.search(params[:search]) if params[:search].present?
if params[:search].present? @members = @members.sort(@sort = params[:sort]) if params[:sort].present?
@members = @members.joins(:user).merge(User.search(params[:search]))
end
if params[:sort].present?
@members = @members.joins(:user).merge(User.sort(@sort = params[:sort]))
end
@members = @members.page(params[:page]).per(50) @members = @members.page(params[:page]).per(50)
@requesters = AccessRequestsFinder.new(@group).execute(current_user) @requesters = AccessRequestsFinder.new(@group).execute(current_user)
@group_member = @group.group_members.new @group_member = @group.group_members.new
......
...@@ -57,6 +57,11 @@ class Member < ActiveRecord::Base ...@@ -57,6 +57,11 @@ class Member < ActiveRecord::Base
scope :owners, -> { active.where(access_level: OWNER) } scope :owners, -> { active.where(access_level: OWNER) }
scope :owners_and_masters, -> { active.where(access_level: [OWNER, MASTER]) } scope :owners_and_masters, -> { active.where(access_level: [OWNER, MASTER]) }
scope :order_name_asc, -> { joins(:user).merge(User.order_name_asc) }
scope :order_name_desc, -> { joins(:user).merge(User.order_name_desc) }
scope :order_recent_sign_in, -> { joins(:user).merge(User.order_recent_sign_in) }
scope :order_oldest_sign_in, -> { joins(:user).merge(User.order_oldest_sign_in) }
before_validation :generate_invite_token, on: :create, if: -> (member) { member.invite_email.present? } before_validation :generate_invite_token, on: :create, if: -> (member) { member.invite_email.present? }
after_create :send_invite, if: :invite?, unless: :importing? after_create :send_invite, if: :invite?, unless: :importing?
...@@ -72,6 +77,21 @@ class Member < ActiveRecord::Base ...@@ -72,6 +77,21 @@ class Member < ActiveRecord::Base
default_value_for :notification_level, NotificationSetting.levels[:global] default_value_for :notification_level, NotificationSetting.levels[:global]
class << self class << self
def search(query)
joins(:user).merge(User.search(query))
end
def sort(method)
case method.to_s
when 'recent_sign_in' then order_recent_sign_in
when 'oldest_sign_in' then order_oldest_sign_in
when 'last_joined' then order_created_desc
when 'oldest_joined' then order_created_asc
else
order_by(method)
end
end
def access_for_user_ids(user_ids) def access_for_user_ids(user_ids)
where(user_id: user_ids).has_access.pluck(:user_id, :access_level).to_h where(user_id: user_ids).has_access.pluck(:user_id, :access_level).to_h
end end
......
...@@ -178,6 +178,8 @@ class User < ActiveRecord::Base ...@@ -178,6 +178,8 @@ class User < ActiveRecord::Base
scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all } scope :not_in_project, ->(project) { project.users.present? ? where("id not in (:ids)", ids: project.users.map(&:id) ) : all }
scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM members WHERE user_id IS NOT NULL AND requested_at IS NULL)') } scope :without_projects, -> { where('id NOT IN (SELECT DISTINCT(user_id) FROM members WHERE user_id IS NOT NULL AND requested_at IS NULL)') }
scope :todo_authors, ->(user_id, state) { where(id: Todo.where(user_id: user_id, state: state).select(:author_id)) } scope :todo_authors, ->(user_id, state) { where(id: Todo.where(user_id: user_id, state: state).select(:author_id)) }
scope :order_recent_sign_in, -> { reorder(last_sign_in_at: :desc) }
scope :order_oldest_sign_in, -> { reorder(last_sign_in_at: :asc) }
def self.with_two_factor def self.with_two_factor
joins("LEFT OUTER JOIN u2f_registrations AS u2f ON u2f.user_id = users.id"). joins("LEFT OUTER JOIN u2f_registrations AS u2f ON u2f.user_id = users.id").
...@@ -205,8 +207,8 @@ class User < ActiveRecord::Base ...@@ -205,8 +207,8 @@ class User < ActiveRecord::Base
def sort(method) def sort(method)
case method.to_s case method.to_s
when 'recent_sign_in' then reorder(last_sign_in_at: :desc) when 'recent_sign_in' then order_recent_sign_in
when 'oldest_sign_in' then reorder(last_sign_in_at: :asc) when 'oldest_sign_in' then order_oldest_sign_in
else else
order_by(method) order_by(method)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment