diff --git a/app/assets/javascripts/osem-datatables.js b/app/assets/javascripts/osem-datatables.js index f950159e..b6453604 100644 --- a/app/assets/javascripts/osem-datatables.js +++ b/app/assets/javascripts/osem-datatables.js @@ -12,7 +12,14 @@ $(function () { pagingType: 'full_numbers', order: [[ 0, 'desc' ]] }); + + $('#userstable').DataTable({ + pagingType: 'full_numbers', + processing: true, + serverSide: true, + sAjaxSource: $('#userstable').data('source'), + aoColumns: [ null, { "bSortable": false }, null, null, { "bSortable": false }, { "bSortable": false }, { "bSortable": false }, { "bSortable": false }] + }); }); }); - diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index dbd54ffd..5d01278b 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,13 +1,20 @@ module Admin class UsersController < Admin::BaseController load_and_authorize_resource + include DatatableServersideProcessing def new @user = User.new end def index - @users = User.all + @users = filtered_records(User.where(nil), %w(name email), %w(id state email name)) + respond_to do |format| + format.html + format.json do + render json: datatable_response(@users.map {|user| users_datatable_data(user)}) + end + end end def show diff --git a/app/controllers/concerns/datatable_serverside_processing.rb b/app/controllers/concerns/datatable_serverside_processing.rb new file mode 100644 index 00000000..42f7b3cf --- /dev/null +++ b/app/controllers/concerns/datatable_serverside_processing.rb @@ -0,0 +1,44 @@ +module DatatableServersideProcessing + extend ActiveSupport::Concern + + def datatable_response(data) + { + sEcho: params[:sEcho].to_i, + iTotalRecords: @collection.count, + iTotalDisplayRecords: records.count, + aaData: data + } + end + + def filtered_records(collection, search_columns, sort_columns) + @collection = collection + @search_columns = search_columns + @sort_columns = sort_columns + paginate(records) + end + + private + + def records + @records ||= fetch_records + end + + def fetch_records + sort_direction = params[:sSortDir_0] == 'desc' ? 'desc' : 'asc' + sort_column = @sort_columns[params[:iSortCol_0].to_i] + records = @collection.order("#{sort_column} #{sort_direction}") + + if params[:sSearch].present? && @search_columns.present? + search_query = @search_columns.map{ |column| "#{column} like :search" }.join(' or ') + records = records.where(search_query, search: "%#{params[:sSearch]}%") + end + + records + end + + def paginate(records) + per_page = params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10 + page = params[:iDisplayStart].to_i / per_page + 1 + records.offset((page - 1) * per_page).limit(per_page) + end +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ce426cc2..2b3ec99f 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -493,4 +493,17 @@ module ApplicationHelper end end end + + def users_datatable_data(user) + [ + user.id, + (user.confirmed? ? 'confirmed' : 'unconfirmed'), + user.email, + user.name, + user.registrations.where(attended: true).count, + (user.roles.empty? ? 'None' : "#{show_roles(user.get_roles.first(2))} #{'...' if user.get_roles.count > 2}"), + (view_context.link_to('View', admin_user_path(user), class: 'btn btn-success') if current_ability.can?(:show, user)), + (view_context.link_to('Edit', edit_admin_user_path(user), class: 'btn btn-primary') if current_ability.can?(:update, user)) + ] + end end diff --git a/app/views/admin/users/index.html.haml b/app/views/admin/users/index.html.haml index c3d7b672..ffea91c2 100644 --- a/app/views/admin/users/index.html.haml +++ b/app/views/admin/users/index.html.haml @@ -7,7 +7,7 @@ = "(#{@users.length})" .row .col-md-12.table-responsive - %table.table.table-striped.table-bordered.table-hover.datatable + %table.table.table-striped.table-bordered.table-hover#userstable{data: {source: admin_users_path(format: :json)}} %thead %th ID %th State @@ -18,31 +18,3 @@ %th View %th Edit %tbody - - @users.each do |user| - %tr - %td - = user.id - %td - - if user.confirmed? - confirmed - - else - unconfirmed - %td - = user.email - %td - = user.name - %td - = user.registrations.where(attended: true).count - %td - - unless user.get_roles.blank? - = show_roles(user.get_roles.first(2)) - - if user.get_roles.count > 2 - = '...' - - else - None - %td - - if can? :show, user - = link_to 'View', admin_user_path(user), class: 'btn btn-success' - %td - - if can? :update, user - = link_to 'Edit', edit_admin_user_path(user), class: 'btn btn-primary'