forgeplus/app/controllers/users/organizations_controller.rb

25 lines
1.1 KiB
Ruby
Raw Normal View History

2021-01-15 14:55:52 +08:00
class Users::OrganizationsController < Users::BaseController
def index
if current_user.logged?
logged_organizations_sql = observed_user.organizations.with_visibility(%w(common limited)).to_sql
privacy_organizations_sql = observed_user.organizations.with_visibility("privacy").joins(:organization_users).where(organization_users: {user_id: current_user.id}).to_sql
@organizations = Organization.from("( #{ logged_organizations_sql } UNION #{ privacy_organizations_sql } ) AS users")
else
@organizations = observed_user.organizations.with_visibility("common")
end
@organizations = @organizations.ransack(login_cont: params[:search]).result if params[:search].present?
@organizations = @organizations.includes(:organization_extension).order("organization_extensions.#{sort_by} #{sort_direction}")
@organizations = kaminari_paginate(@organizations)
end
private
def sort_by
2021-06-17 16:13:40 +08:00
OrganizationExtension.column_names.include?(params[:sort_by]) ? params[:sort_by] : 'created_at'
end
def sort_direction
2021-06-17 16:13:40 +08:00
%w(desc asc).include?(params[:sort_direction]) ? params[:sort_direction] : 'desc'
2021-01-15 14:55:52 +08:00
end
end