Skip to content

Commit

Permalink
Ticket #57: Liste der Zuständigkeiten nach Gruppen sortieren (#302)
Browse files Browse the repository at this point in the history
inkl. Erhaltung des aktuellen Seitenkontext nach `create`, `update` und `destroy` Aktionen.

Co-authored-by: Niels Bennke <[email protected]>
  • Loading branch information
HansBudel and nbennke authored Dec 19, 2024
1 parent eece932 commit f13bb45
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 59 deletions.
22 changes: 16 additions & 6 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,23 @@ th.action {
.inner-table {
margin-bottom: 0;

tr:first-of-type td {
padding-top: 0;
}
tr {
&:first-of-type td {
padding-top: 0;
}

tr:last-of-type td {
border-bottom: 0;
padding-bottom: 0;
&:last-of-type td {
border-bottom: 0;
padding-bottom: 0;
}

td:first-of-type {
padding-left: 0;
}

td:last-of-type {
padding-right: 0;
}
}
}

Expand Down
29 changes: 21 additions & 8 deletions app/controllers/responsibilities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
class ResponsibilitiesController < ApplicationController
include Filter
include Sorting

before_action { check_auth :manage_responsibilities }

helper_method :permitted_order_and_pagination_params

def index
@categories = filter(Category.active).order(order_attr).page(params[:page] || 1).per(params[:per_page] || 20)
@responsibilities = result_list_responsibilities
end

def new
Expand All @@ -20,30 +24,32 @@ def edit
def create
@responsibility = Responsibility.new(responsibility_params)
if @responsibility.save
return redirect_to action: :index if params[:save_and_close].present?
render :edit
else
render :new
return redirect_to permitted_order_and_pagination_params.merge(action: :index) if params[:save_and_close].present?
return render :edit
end
render :new
end

def update
@responsibility = Responsibility.authorized.find(params[:id])
if @responsibility.update(responsibility_params) && params[:save_and_close].present?
redirect_to action: :index
else
render :edit
return redirect_to permitted_order_and_pagination_params.merge(action: :index)
end
render :edit
end

def destroy
@responsibility = Responsibility.authorized.find(params[:id])
@responsibility.update!(deleted_at: Time.current)
redirect_to action: :index
redirect_to permitted_order_and_pagination_params.merge(action: :index)
end

private

def permitted_order_and_pagination_params
params.permit :page, order_by: %i[column dir]
end

def responsibility_params
return {} if params[:responsibility].blank?
params.require(:responsibility).permit(:group_id, :category_id)
Expand All @@ -55,10 +61,17 @@ def custom_order(col, dir)
main_category_arel_table[:kind].send(dir)
when :category
[main_category_arel_table[:name].send(dir), sub_category_arel_table[:name].send(dir)]
when :group
group_arel_table[:name].send(dir)
end
end

def default_order
[main_category_arel_table[:kind], main_category_arel_table[:name], sub_category_arel_table[:name]]
end

def result_list_responsibilities
filter(Responsibility.includes(:group, { category: %i[main_category sub_category] })
.authorized.active).order(order_attr).page(params[:page] || 1).per(params[:per_page] || 20)
end
end
4 changes: 4 additions & 0 deletions app/helpers/responsibilities_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ def groups_options(resp_or_category)
options_for_select groups
end

def order_by_group?
order_params[:column] == 'group'
end

private

def groups_options_with_selected(responsibility, groups)
Expand Down
27 changes: 27 additions & 0 deletions app/views/responsibilities/_category.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<tr>
<td><%= category.kind_name %></td>
<td><%= category %></td>
<td colspan="2">
<table class="table inner-table">
<% active_responsibilities(category).find_each do |responsibility| -%>
<tr>
<td title="<%= responsibility.group.logging_subject_name %>">
<%= responsibility.group %>
</td>
<td class="text-end">
<%= render partial: 'responsibility_actions', object: responsibility, as: :responsibility %>
</td>
</tr>
<% end -%>
<% if Current.user.role_admin? || active_responsibilities(category).blank? -%>
<tr>
<td colspan="2" class="text-end">
<%= link_to tag.i('', class: 'fa fa-plus'),
new_category_responsibility_path(category, permitted_order_and_pagination_params),
remote: true, class: 'btn btn-sm btn-outline-primary' %>
</td>
</tr>
<% end -%>
</table>
</td>
</tr>
2 changes: 1 addition & 1 deletion app/views/responsibilities/_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="modal-dialog modal-xl">
<% opts = { url: responsibility_path(@responsibility), remote: true,
<% opts = { url: responsibility_path(@responsibility, permitted_order_and_pagination_params), remote: true,
html: { 'data-dependencies': data_dependencies_path } } %>
<%= form_for @responsibility, opts do |f| %>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/responsibilities/_new.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="modal-dialog modal-xl">
<% options = { method: :post, remote: true, html: { 'data-dependencies': data_dependencies_path } } -%>
<%= form_for @responsibility, options.merge(url: responsibilities_path) do |f| %>
<%= form_for @responsibility, options.merge(url: responsibilities_path(permitted_order_and_pagination_params)) do |f| %>
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<div class="modal-content">
<div class="modal-header">
Expand Down
8 changes: 8 additions & 0 deletions app/views/responsibilities/_responsibility.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<tr>
<td><%= responsibility.category.kind_name %></td>
<td><%= responsibility.category %></td>
<td title="<%= responsibility.group.logging_subject_name %>"><%= responsibility.group %></td>
<td class="text-nowrap">
<%= render partial: 'responsibility_actions', object: responsibility, as: :responsibility %>
</td>
</tr>
6 changes: 6 additions & 0 deletions app/views/responsibilities/_responsibility_actions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= link_to tag.i('', class: 'fa fa-pencil'),
edit_responsibility_path(responsibility, permitted_order_and_pagination_params),
remote: true, class: 'btn btn-sm btn-outline-primary' %>
<%= link_to tag.i('', class: 'fa fa-trash'), responsibility_path(responsibility, permitted_order_and_pagination_params),
method: :delete, class: 'btn btn-sm btn-outline-danger',
data: { confirm: t('responsibilities.index.confirm_delete') } %>
53 changes: 10 additions & 43 deletions app/views/responsibilities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,60 +1,27 @@
<h1><%= Responsibility.model_name.human count: 2 %></h1>

<%= render partial: 'simple_text_filter' %>
<%= paginate @categories %>
<%= paginate order_by_group? ? @responsibilities : @categories %>

<table class="table">
<thead>
<tr>
<th>
<%= order_link MainCategory.human_attribute_name(:kind), :responsibilities, :kind %>
</th>
<th>
<%= order_link Category.model_name.human(count: 1), :responsibilities, :category %>
</th>
<th>
<%= Group.model_name.human(count: 1) %>
<span class="float-end"><%= t :actions %></span>
</th>
<th><%= order_link MainCategory.human_attribute_name(:kind), :responsibilities, :kind %></th>
<th><%= order_link Category.model_name.human(count: 1), :responsibilities, :category %></th>
<th><%= order_link Group.model_name.human(count: 1), :responsibilities, :group %></th>
<th class="action"><%= t :actions %></th>
</tr>
</thead>
<tbody>
<% @categories.each do |category| -%>
<tr>
<td><%= category.kind_name %></td>
<td><%= category %></td>
<td>
<table class="table inner-table">
<% active_responsibilities(category).find_each do |responsibility| -%>
<tr>
<td title="<%= responsibility.group.logging_subject_name %>">
<%= responsibility.group %>
</td>
<td class="text-end">
<%= link_to tag.i('', class: 'fa fa-pencil'), edit_responsibility_path(responsibility),
remote: true, class: 'btn btn-sm btn-outline-primary' %>
<%= link_to tag.i('', class: 'fa fa-trash'), responsibility_path(responsibility),
method: :delete, class: 'btn btn-sm btn-outline-danger',
data: { confirm: t('.confirm_delete') } %>
</td>
</tr>
<% end -%>
<% if Current.user.role_admin? || active_responsibilities(category).blank? -%>
<tr>
<td colspan="2" class="text-end">
<%= link_to tag.i('', class: 'fa fa-plus'), new_category_responsibility_path(category),
remote: true, class: 'btn btn-sm btn-outline-primary' %>
</td>
</tr>
<% end -%>
</table>
</td>
</tr>
<% if order_by_group? -%>
<%= render partial: 'responsibility', collection: @responsibilities %>
<% else -%>
<%= render partial: 'category', collection: @categories %>
<% end -%>
</tbody>
</table>

<%= paginate @categories %>
<%= paginate order_by_group? ? @responsibilities : @categories %>

<% content_for :footer do %>
<div class="float-end">
Expand Down

0 comments on commit f13bb45

Please sign in to comment.