Added ransack search function to admin/users index

This commit is contained in:
2024-09-18 14:59:07 +02:00
parent 4bff15cd4e
commit ff1c1017cf
11 changed files with 77 additions and 21 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -1,4 +1,6 @@
class Admin::JobsController < ApplicationController
include Pagy::Backend
before_action :authorize!
def index
@jobs = Job.all

View File

@@ -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

View File

@@ -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!

View File

@@ -1,2 +1,3 @@
module Admin::JobsHelper
include Pagy::Frontend
end

View File

@@ -1,2 +1,3 @@
module Admin::UsersHelper
include Pagy::Frontend
end

View File

@@ -1,6 +1,4 @@
module ApplicationHelper
include Pagy::Frontend
def icon(name, options = {})
options[:title] ||= name.underscore.humanize
options[:aria] = true

View File

@@ -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

View File

@@ -7,17 +7,26 @@
<% end %>
</td>
<td class="p-2 py-3">
<%= user.name %>
<%= highlight(user.firstname, [ params.dig(:q, :firstname_or_lastname_or_email_cont).to_s, params.dig(:q, :firstname_cont).to_s ]) %>
<%#= user.firstname %>
</td>
<td class="p-2 py-3">
<%= highlight user.lastname, [params.dig(:q, :firstname_or_lastname_or_email_cont).to_s, params.dig(:q, :lastname_cont).to_s] %>
</td>
<td class="p-2 py-3">
<%= icon bool_icon(user.verified), class: "icon #{user.verified ? "text-green-600" : "text-red-600"}", title: "E-Mail-Adresse verifiziert" %>
<%= user.email %>
<%= highlight user.email, [params.dig(:q, :firstname_or_lastname_or_email_cont).to_s, params.dig(:q, :email_start).to_s] %>
</td>
<td class="p-2 py-3">
<span class="badge block w-28 bg-role-<%= user.role %>-light">
<%= user.jobs_as_costumer.size %>
</span>
</td>
<td class="p-2 py-3">
<span class="badge block w-28 bg-role-<%= user.role %>-light">
<%= l user.created_at.localtime.to_date %>
</span>
</td>
<td class="p-2 py-3">
<span class="badge block w-28 bg-role-<%= user.role %>-light">
<%= user.role %>

View File

@@ -1,19 +1,43 @@
<%= turbo_frame_tag "admin_users" do %>
<div class="w-full">
<% content_for :title, "Current Print Jobs" %>
<div class="flex items-center justify-between py-4">
<h1 class="text-4xl font-bold text-hsrm-gray">Benutzerliste</h1>
<%= link_to "Filter", "#", class: "px-3 py-2 bg-hsrm-red drop-shadow-lg transition-colors hover:bg-hsrm-red-light text-white block font-medium" %>
<div class="w-full">
<% content_for :title, "Current Print Jobs" %>
<h1 class="text-4xl font-bold text-hsrm-gray">Benutzerliste</h1>
<div class="flex items-center justify-between py-4">
<div>
<%= search_form_for @q, data: { turbo_frame: :admin_users, turbo_action: 'advance' }, url: admin_users_path do |f| %>
<%#= f.label :firstname_cont, "Vorname:" %>
<%#= f.search_field :firstname_cont, oninput: 'this.form.requestSubmit();' %>
<%#= f.label :lastname_cont, "Nachname:" %>
<%#= f.search_field :lastname_cont, oninput: 'this.form.requestSubmit();' %>
<%#= f.label :email_start, "E-Mail:" %>
<%#= f.search_field :email_start, oninput: 'this.form.requestSubmit();' %>
<%#= f.label :name_or_email_cont, "Name oder E-Mail:" %>
<%= f.search_field :firstname_or_lastname_or_email_cont, oninput: 'this.form.requestSubmit();' %>
<%= f.label :created_at_gteq, "Erstellt von:" %>
<%= f.date_field :created_at_gteq, onchange: 'this.form.requestSubmit();' %>
<%= f.label :created_at_lteq, "bis:" %>
<%= f.date_field :created_at_lteq, onchange: 'this.form.requestSubmit();' %>
<%= f.label :verified_eq, "E-Mail validiert:" %>
<%= f.select :verified_eq, [true,false], {include_blank: true}, onchange: 'this.form.requestSubmit();' %>
<%= f.label :role_eq, "Rolle:" %>
<%= f.select :role_eq, User::AVAILABLE_ROLES, {include_blank: true}, onchange: 'this.form.requestSubmit();' %>
<%#= f. %>
<%= f.submit "Filter anwenden", class: "py-2 px-3 bg-hsrm-red hover:bg-hsrm-red-light shadow-lg text-white inline-block font-medium cursor-pointer" %>
<% end %>
</div>
<div class="min-w-full overflow-auto shadow-lg">
</div>
<%= turbo_frame_tag "admin_users", data: { turbo_action: 'advance'} do %>
<%== pagy_nav(@pagy) %>
<div class="min-w-full overflow-auto shadow-lg pt-2">
<table class="w-full py-8 table-auto">
<thead class="font-semibold tracking-wide bg-gray-200 border-b-2 border-gray-300 text text-hsrm-gray">
<tr>
<th class="w-1 p-2 py-3 text-center">ID</th>
<th class="w-1 p-2 py-3 text-left">Name</th>
<th class="p-2 py-3 text-center">E-Mail-Adresse</th>
<th class="w-1 p-2 py-3 text-center"># Jobs</th>
<th class="w-1 p-2 py-3 text-center">Rolle</th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :id, "ID", ) %></th>
<th class="min-w-24 p-2 py-3 text-left"><%= sort_link(@q, :firstname, "Vorname") %></th>
<th class="min-w-24 p-2 py-3 text-left"><%= sort_link(@q, :lastname, "Nachname") %></th>
<th class="p-2 py-3 text-left"><%= sort_link(@q, :email, "E-Mail-Adresse") %></th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :jobs_as_costumer_count, "# Jobs") %></th>
<th class="w-1 p-2 py-3 text-center text-nowrap"><%= sort_link(@q, :created_at, "Registriert am") %></th>
<th class="w-1 p-2 py-3 text-center"><%= sort_link(@q, :role, "Rolle") %></th>
<th class="w-1 p-2 py-3 text-center">Rolle ändern zu</th>
</tr>
</thead>