diff --git a/Gemfile b/Gemfile index ef18075..e4d020d 100644 --- a/Gemfile +++ b/Gemfile @@ -90,3 +90,5 @@ gem "csv", "~> 3.3" # Centralization of locale data collection for Ruby on Rails. # URL: https://github.com/svenfuchs/rails-i18n gem "rails-i18n", "~> 7.0" + +gem "ransack", "~> 4.2" diff --git a/Gemfile.lock b/Gemfile.lock index 710e1d4..f830c66 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -247,6 +247,10 @@ GEM zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.2.1) + ransack (4.2.1) + activerecord (>= 6.1.5) + activesupport (>= 6.1.5) + i18n rdoc (6.7.0) psych (>= 4.0.0) redis (5.3.0) @@ -383,6 +387,7 @@ DEPENDENCIES pwned rails (~> 7.2.0, >= 7.2.0) rails-i18n (~> 7.0) + ransack (~> 4.2) redis (>= 4.0.1) rubocop-rails-omakase selenium-webdriver diff --git a/app/controllers/admin/jobs_controller.rb b/app/controllers/admin/jobs_controller.rb index d19b427..15e6520 100644 --- a/app/controllers/admin/jobs_controller.rb +++ b/app/controllers/admin/jobs_controller.rb @@ -1,4 +1,6 @@ class Admin::JobsController < ApplicationController + include Pagy::Backend + before_action :authorize! def index @jobs = Job.all diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 5c1be13..c8e16f8 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,10 +1,14 @@ class Admin::UsersController < ApplicationController + include Pagy::Backend + before_action :set_user, only: [ :show, :update ] before_action :authorize! def index - @users = User.all.order(:lastname, :firstname) - @pagy, @records = pagy(@users, limit: 20) + # @users = User.all.order(:lastname, :firstname) + @q = User.ransack(params[:q]) + @q.sorts = "id asc" if @q.sorts.empty? + @pagy, @records = pagy(@q.result(distinct: true), limit: 20) end def edit @@ -23,7 +27,7 @@ class Admin::UsersController < ApplicationController authorize! @user if @user.update(user_params) respond_to do |format| - format.html { redirect_to admin_users_path } + format.html { redirect_back(fallback_location: admin_users_path) } end else render :index, status: :unprocessable_entity diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5258263..984f8b8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,4 @@ class ApplicationController < ActionController::Base - include Pagy::Backend - before_action :set_current_request_details before_action :authenticate_user! before_action :verified_user! diff --git a/app/helpers/admin/jobs_helper.rb b/app/helpers/admin/jobs_helper.rb index 2554ac6..f400f66 100644 --- a/app/helpers/admin/jobs_helper.rb +++ b/app/helpers/admin/jobs_helper.rb @@ -1,2 +1,3 @@ module Admin::JobsHelper + include Pagy::Frontend end diff --git a/app/helpers/admin/users_helper.rb b/app/helpers/admin/users_helper.rb index 5995c2a..daba7f7 100644 --- a/app/helpers/admin/users_helper.rb +++ b/app/helpers/admin/users_helper.rb @@ -1,2 +1,3 @@ module Admin::UsersHelper + include Pagy::Frontend end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 3cf2bc8..b3fac19 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,6 +1,4 @@ module ApplicationHelper - include Pagy::Frontend - def icon(name, options = {}) options[:title] ||= name.underscore.humanize options[:aria] = true diff --git a/app/models/user.rb b/app/models/user.rb index d67f9a1..11a29cb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -42,4 +42,16 @@ class User < ApplicationRecord def name [ firstname, " ", lastname ].join end + + def self.ransackable_attributes(auth_object = nil) + [ "created_at", "email", "firstname", "id", "jobs_as_costumer_count", "jobs_as_operator_count", "lastname", "role", "verified", "name" ] + end + + def self.ransackable_associations(auth_object = nil) + [] + end + + ransacker :name do + Arel.sql("CONCAT_WS(' ', users.firstname, users.lastname)") + end end diff --git a/app/views/admin/users/_user_tr.html.erb b/app/views/admin/users/_user_tr.html.erb index a45eaa2..46b5eb4 100644 --- a/app/views/admin/users/_user_tr.html.erb +++ b/app/views/admin/users/_user_tr.html.erb @@ -7,17 +7,26 @@ <% end %>
| ID | -Name | -E-Mail-Adresse | -# Jobs | -Rolle | +<%= sort_link(@q, :id, "ID", ) %> | +<%= sort_link(@q, :firstname, "Vorname") %> | +<%= sort_link(@q, :lastname, "Nachname") %> | +<%= sort_link(@q, :email, "E-Mail-Adresse") %> | +<%= sort_link(@q, :jobs_as_costumer_count, "# Jobs") %> | +<%= sort_link(@q, :created_at, "Registriert am") %> | +<%= sort_link(@q, :role, "Rolle") %> | Rolle ändern zu |
|---|