Skip to content
Snippets Groups Projects
Unverified Commit d54ff310 authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

batch update posts

parent 00bc4442
No related branches found
No related tags found
No related merge requests found
/* global */
window.osuny.BatchSelectable = function BatchSelectable (element) {
'use strict';
this.element = element;
this.selectAllInput = this.element.querySelector('[data-batch-selectable-role="select-all"]');
this.selectSingleInputs = this.element.querySelectorAll('[data-batch-selectable-role="select-single"]');
this.initEvents();
};
window.osuny.BatchSelectable.prototype.initEvents = function () {
'use strict';
if (this.selectAllInput === null) {
return;
}
this.selectAllInput.addEventListener('change', function () {
this.toggleSingleInputs(this.selectAllInput.checked);
}.bind(this));
};
window.osuny.BatchSelectable.prototype.toggleSingleInputs = function (checked) {
'use strict';
var i;
for (i = 0; i < this.selectSingleInputs.length; i += 1) {
this.selectSingleInputs[i].checked = checked;
}
};
window.addEventListener('DOMContentLoaded', function () {
'use strict';
var elements = document.querySelectorAll('[data-batch-selectable]'),
i;
for (i = 0; i < elements.length; i += 1) {
new window.osuny.BatchSelectable(elements[i]);
}
});
......@@ -6,6 +6,19 @@ class Admin::Communication::Website::PostsController < Admin::Communication::Web
breadcrumb
end
def publish
ids = params[:ids] || []
target_posts = @website.posts.where(id: ids)
if params[:published] == "true"
target_posts.update(published: true)
elsif params[:published] == "false"
target_posts.update(published: false)
end
@website.sync_objects_with_git(target_posts) if target_posts.any?
redirect_back fallback_location: admin_communication_website_posts_path,
notice: t('communication.website.posts.successful_batch_update')
end
def show
breadcrumb
end
......
......@@ -10,4 +10,18 @@ module Communication::Website::WithGitRepository
def git_repository
@git_repository ||= Git::Repository.new self
end
def sync_objects_with_git(objects)
touch
return unless git_repository.valid?
objects.each do |object|
next unless object.has_website_for_self?(self)
dependencies = object.git_dependencies(self).to_a.flatten.uniq.compact
dependencies.each do |dependency|
Communication::Website::GitFile.sync self, dependency
end
end
git_repository.sync!
end
handle_asynchronously :sync_objects_with_git, queue: 'default'
end
......@@ -64,6 +64,18 @@ module WithGit
end
end
def has_website_for_self?(website)
websites_for_self.include?(website)
end
def git_dependencies(website = nil)
[self]
end
def git_destroy_dependencies(website = nil)
[self]
end
protected
def in_block_dependencies?(website)
......@@ -83,12 +95,4 @@ module WithGit
[]
end
end
def git_dependencies(website = nil)
[self]
end
def git_destroy_dependencies(website = nil)
[self]
end
end
<%
hide_author |= false
hide_category |= false
selectable |= false
%>
<table class="<%= table_classes %>">
<table class="<%= table_classes %>" <%= "data-batch-selectable" if selectable %>>
<thead>
<tr>
<% if selectable %>
<th>
<%= check_box_tag nil, nil, false, data: { batch_selectable_role: "select-all" } %>
</th>
<% end %>
<th><%= Communication::Website::Post.human_attribute_name('title') %></th>
<th><%= Communication::Website::Post.human_attribute_name('featured_image') %></th>
<% unless hide_author %>
......@@ -20,6 +26,11 @@
<tbody>
<% posts.each do |post| %>
<tr>
<% if selectable %>
<td>
<%= check_box_tag "ids[]", post.id, false, data: { batch_selectable_role: "select-single" } %>
</td>
<% end %>
<td><%= link_to post,
admin_communication_website_post_path(website_id: post.website.id, id: post.id),
class: "#{'opacity-50' unless post.published?}" %></td>
......
......@@ -2,10 +2,29 @@
<%= render 'admin/communication/websites/sidebar' do %>
<div class="card">
<%= render 'admin/communication/website/posts/list', posts: @posts, hide_author: true %>
<% if @posts.total_pages > 1 %>
<%= form_tag publish_admin_communication_website_posts_path do %>
<input type="hidden" name="ids[]" value="">
<%= render 'admin/communication/website/posts/list', posts: @posts, hide_author: true, selectable: true %>
<div class="card-footer">
<%= paginate @posts, theme: 'bootstrap-5' %>
<% if @posts.total_pages > 1 %>
<div class="float-end">
<%= paginate @posts, theme: 'bootstrap-5' %>
</div>
<% end %>
<div class="row align-items-center">
<div class="col-auto">
Modifier la sélection
</div>
<div class="col-auto">
<select name="published" class="form-select">
<option value="false">Non publiée</option>
<option value="true">Publiée</option>
</select>
</div>
<div class="col-auto">
<input type="submit" value="Enregistrer" class="btn btn-primary">
</div>
</div>
</div>
<% end %>
</div>
......
......@@ -302,6 +302,7 @@ fr:
title: Équipe pédagogique
posts:
new_curation: Nouvelle curation
successful_batch_update: Les actualités ont bien été mises à jour
see_all: Voir la liste complète (%{number} éléments)
enums:
communication:
......
......@@ -25,7 +25,9 @@ namespace :communication do
end
end
resources :authors, controller: 'website/authors', only: [:index, :show]
resources :posts, controller: 'website/posts'
resources :posts, controller: 'website/posts' do
post :publish, on: :collection
end
resources :curations,
path: 'posts/curations',
as: :post_curations,
......@@ -44,7 +46,7 @@ namespace :communication do
end
get 'structure' => 'website/structure#edit'
patch 'structure' => 'website/structure#update'
end
resources :blocks, controller: 'blocks', except: :index do
collection do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment