Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Généralisation des arbres de catégories pour l'agenda et les actualités #1686

Merged
merged 22 commits into from
Mar 6, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,13 @@ class Admin::Communication::Websites::Agenda::CategoriesController < Admin::Comm
through_association: :agenda_categories

include Admin::Translatable
include Admin::Categorizable

def index
@categories = @categories.for_language(current_website_language).ordered
@root_categories = categories.root.ordered
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
breadcrumb
end

def reorder
ids = params[:ids] || []
ids.each.with_index do |id, index|
category = @website.post_categories.find(id)
category.update_column :position, index + 1
end
@category = @website.agenda_categories.find(params[:itemId])
@category.sync_with_git # Will sync siblings
end

def show
@events = @category.events.ordered.page(params[:page])
breadcrumb
Expand Down Expand Up @@ -68,6 +59,10 @@ def destroy

protected

def categories_class
Communication::Website::Agenda::Category
end

def breadcrumb
super
add_breadcrumb Communication::Website::Agenda::Category.model_name.human(count: 2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ class Admin::Communication::Websites::Agenda::EventsController < Admin::Communic
before_action :load_categories
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved

def index
@events = apply_scopes(@events).for_language(current_website_language).ordered_desc.page params[:page]
@events = apply_scopes(@events).for_language(current_website_language)
.ordered_desc
.page(params[:page])
@root_categories = @categories.root
breadcrumb
end

Expand Down Expand Up @@ -80,7 +83,9 @@ def breadcrumb
end

def load_categories
@categories = @website.agenda_categories.for_language(current_website_language).ordered
@categories = @website.agenda_categories
.for_language(current_website_language)
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
.ordered
end

def event_params
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,15 @@ class Admin::Communication::Websites::Posts::CategoriesController < Admin::Commu
through_association: :post_categories

include Admin::Translatable
include Admin::Categorizable

before_action :get_root_categories, only: [:index, :new, :create, :edit, :update]

def index
@categories = @website.post_categories.for_language(current_website_language).ordered
@categories = categories.ordered
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
breadcrumb
end

def reorder
parent_id = params.dig(:parentId)
old_parent_id = params.dig(:oldParentId)
ids = params[:ids] || []
ids.each.with_index do |id, index|
category = @website.post_categories.find(id)
category.update_columns parent_id: parent_id,
position: index + 1
end
if old_parent_id
old_parent = @website.post_categories.find(old_parent_id)
old_parent.sync_with_git
end
@website.post_categories.find(params[:itemId]).sync_with_git # Will sync siblings
end

def children
return unless request.xhr?
@category = @website.post_categories.for_language(current_website_language).find(params[:id])
@children = @category.children.ordered
end

def show
@posts = @category.posts.ordered.page(params[:page])
breadcrumb
Expand Down Expand Up @@ -84,7 +63,11 @@ def destroy
protected

def get_root_categories
@root_categories = @website.post_categories.root.for_language(current_website_language).ordered
@root_categories = categories.root.ordered
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
end

def categories_class
Communication::Website::Post::Category
end

def breadcrumb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Admin::Communication::Websites::PostsController < Admin::Communication::We
include Admin::Translatable

before_action :load_filters, only: :index
before_action :load_categories, only: [:new, :edit]
before_action :load_categories

has_scope :for_search_term
has_scope :for_author
Expand All @@ -19,7 +19,7 @@ def index
.accessible_by(current_ability)
.ordered
.page(params[:authors_page])
@root_categories = @website.post_categories.for_language(current_website_language).root.ordered
@root_categories = @categories.root
breadcrumb
end

Expand Down Expand Up @@ -131,6 +131,8 @@ def load_filters
end

def load_categories
@categories = @website.post_categories.for_language(current_website_language)
@categories = @website.post_categories
.for_language(current_website_language)
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
.ordered
end
end
40 changes: 40 additions & 0 deletions app/controllers/concerns/admin/categorizable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Admin::Categorizable
extend ActiveSupport::Concern

def reorder
parent_id = params.dig(:parentId)
old_parent_id = params.dig(:oldParentId)
ids = params[:ids] || []
ids.each.with_index do |id, index|
category = categories.find(id)
category.update_columns parent_id: parent_id,
position: index + 1
end
if old_parent_id.present?
old_parent = categories.find(old_parent_id)
old_parent.sync_with_git
end
categories.find(params[:itemId]).sync_with_git # Will sync siblings
end

def children
return unless request.xhr?
@kind = categories_class
@category = categories.find(params[:id])
@children = @category.children.ordered
render 'admin/application/categories/children'
end

protected

def categories
categories_class.where(communication_website_id: @website.id)
.for_language(current_website_language)
.ordered
end

# Communication::Website::Agenda::Category
def categories_class
raise NoMethodError.new("@kind doit être défini dans le controller")
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
end
end
23 changes: 23 additions & 0 deletions app/views/admin/application/categories/_panel.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%
# kind = Communication::Website::Agenda::Category
visible = can?(:create, kind) || (root_categories.any? && can?(:edit, root_categories.first))
# lien vers la création d'une nouvelle catégorie
action = create_link kind
# Catégories
title = kind.model_name.human(count: 2)
# communication_website_agenda_categories
kind_as_path = kind.model_name.route_key
# reorder_admin_communication_website_post_categories_path
order_path = "reorder_admin_#{kind_as_path}_path"
%>
<% if visible %>
<%= osuny_panel title, action: action do %>
<ul class="list-unstyled treeview treeview--sortable js-treeview js-treeview-sortable js-treeview-sortable-container"
data-id=""
data-sort-url="<%= public_send order_path %>">
<%= render 'admin/application/categories/treebranch',
categories: root_categories,
kind: kind %>
</ul>
<% end %>
<% end %>
50 changes: 50 additions & 0 deletions app/views/admin/application/categories/_treebranch.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<%
# communication_website_agenda_category
kind_as_path = kind.to_s.parameterize.underscore
%>
<% categories.each do |category| %>
<%
# children_admin_communication_website_post_category_path
children_path = send "children_admin_#{kind_as_path}_path",
SebouChu marked this conversation as resolved.
Show resolved Hide resolved
website_id: category.website.id,
id: category.id
# admin_communication_website_post_category_path
show_path = send "admin_#{kind_as_path}_path",
SebouChu marked this conversation as resolved.
Show resolved Hide resolved
website_id: @website.id,
id: category.id
# edit_admin_communication_website_post_category_path
edit_path = send "edit_admin_#{kind_as_path}_path",
SebouChu marked this conversation as resolved.
Show resolved Hide resolved
website_id: @website.id,
id: category.id
%>
<li class="treeview__element js-treeview-element <%= 'treeview__element--empty' unless category.has_children? %>"
data-id="<%= category.id %>"
data-parent="<%= category.parent_id %>">
<div class="d-flex align-items-center treeview__label border-bottom py-1">
<%= link_to children_path,
class: 'js-treeview-openzone d-inline-block p-2 ps-0', style: 'width: 22px', remote: true do %>
<%= render 'admin/application/tree/folder' %>
<% end %>
<%= link_to category, show_path %>
<%= render 'admin/application/tree/sort' %>
<%= link_to children_path,
class: 'js-treeview-openzone small ps-2', remote: true do %>
<span class="open_text"><%= t 'folder.open' %></span>
<span class="close_text"><%= t 'folder.close' %></span>
<% end %>
<div class="btn-group ms-auto" role="group">
<%= link_to t('show'), show_path, class: 'action ps-3' %>
<%= link_to t('edit'), edit_path, class: 'action ps-3' %>
<%= link_to t('delete'), show_path, method: :delete, data: { confirm: t('please_confirm') }, class: 'action text-danger ps-3' %>
</div>
</div>
<ul class="list-unstyled treeview__children js-treeview-children js-treeview-sortable-container ms-4" data-id="<%= category.id %>">
<li class="treeview__empty">
<div class="d-flex align-items-center treeview__label border-bottom p-1">
<span class="p-2 ps-0"><%= t('folder.empty') %></span>
</div>
</li>
<li class="treeview__loading border-bottom p-1"><%= t('loading') %></li>
</ul>
</li>
<% end %>
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
$branch = $('.js-treeview-element[data-id=<%= @category.id %>]');
<% if @children.any? %>
$('.js-treeview-children', $branch).append("<%= escape_javascript(render 'treebranch', categories: @children) %>");
$('.js-treeview-children', $branch).append("<%= escape_javascript(render 'admin/application/categories/treebranch',
categories: @children,
kind: @kind) %>");
<% else %>
$branch.addClass('treeview__element--empty');
<% end %>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<% content_for :title, "#{Communication::Website::Agenda::Category.model_name.human(count: 2)} (#{@categories.count})" %>

<%= render 'admin/communication/websites/sidebar' do %>
<%
action = create_link Communication::Website::Agenda::Category
%>
<%= osuny_panel Communication::Website::Agenda::Category.model_name.human(count: 2), action: action do %>
<%= render 'admin/communication/websites/agenda/categories/list', categories: @categories %>
<% end %>
<%= render 'admin/application/categories/panel',
root_categories: @root_categories,
kind: Communication::Website::Agenda::Category %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<%= f.association :categories,
label_text: false,
as: :check_boxes,
collection: @categories %>
collection: collection_tree_for_checkboxes(@categories) %>
<% end %>
<% end %>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<%= paginate @events, theme: 'bootstrap-5' %>
<% end %>

<%= render 'admin/communication/websites/agenda/categories/panel', categories: @categories %>
<%= render 'admin/application/categories/panel',
root_categories: @root_categories,
kind: Communication::Website::Agenda::Category %>
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
<% end %>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<% content_for :title, "#{Communication::Website::Post::Category.model_name.human(count: 2)} (#{@categories.count})" %>

<%= render 'admin/communication/websites/sidebar' do %>
<%
action = create_link Communication::Website::Post::Category
%>
<%= osuny_panel Communication::Website::Post::Category.model_name.human(count: 2), action: action do %>
<ul class="list-unstyled treeview treeview--sortable js-treeview js-treeview-sortable js-treeview-sortable-container"
data-id=""
data-sort-url="<%= reorder_admin_communication_website_post_categories_path %>">
<%= render 'treebranch', categories: @root_categories %>
</ul>
<% end %>
<%= render 'admin/application/categories/panel',
root_categories: @root_categories,
kind: Communication::Website::Post::Category %>
arnaudlevy marked this conversation as resolved.
Show resolved Hide resolved
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
<% end %>
</div>
<% end %>
<%= render 'admin/communication/websites/posts/categories/panel', root_categories: @root_categories %>

<%= render 'admin/application/categories/panel',
root_categories: @root_categories,
kind: Communication::Website::Post::Category %>

<% if @authors.any? %>
<%= osuny_panel t('communication.authors', count: 2) do %>
<%= render 'admin/communication/websites/posts/authors/list', authors: @authors %>
Expand Down
1 change: 1 addition & 0 deletions config/routes/admin/communication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
post :reorder
end
member do
get :children
get :static
end
end
Expand Down
Loading
Loading