From 8ef4b9f2175c36d8bb27bc9c3cb80c4883669584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com> Date: Wed, 29 Dec 2021 15:08:31 +0100 Subject: [PATCH] batch objects --- .../communication/website/pages_controller.rb | 9 ++++- app/models/concerns/with_github_files.rb | 34 ++++++------------- app/services/github.rb | 32 ++++++++++++++--- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb index 013902149..d9788ae24 100644 --- a/app/controllers/admin/communication/website/pages_controller.rb +++ b/app/controllers/admin/communication/website/pages_controller.rb @@ -10,13 +10,20 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web def reorder parent_id = params[:parentId].blank? ? nil : params[:parentId] ids = params[:ids] || [] + pages = [] + github_files = [] ids.each.with_index do |id, index| page = @website.pages.find(id) + pages << page + pages.concat(page.descendents) if parent_id != page.parent_id page.update( parent_id: parent_id, - position: index + 1 + position: index + 1, + skip_github_publication: true ) end + github = Github.with_website @website + github.send_batch_to_website(pages, message: '[Page] Reorder pages.') end def children diff --git a/app/models/concerns/with_github_files.rb b/app/models/concerns/with_github_files.rb index 3232d8ae3..f9762bb6f 100644 --- a/app/models/concerns/with_github_files.rb +++ b/app/models/concerns/with_github_files.rb @@ -2,13 +2,15 @@ module WithGithubFiles extend ActiveSupport::Concern included do + attr_accessor :skip_github_publication + has_many :github_files, class_name: "Communication::Website::GithubFile", as: :about, dependent: :destroy after_save :create_github_files - after_save_commit :publish_github_files + after_save_commit :publish_github_files, unless: :skip_github_publication after_save_commit :unpublish_github_files, if: :should_unpublish_github_files? end @@ -39,6 +41,10 @@ module WithGithubFiles ] end + def list_of_websites + respond_to?(:websites) ? websites : [website] + end + protected def create_github_files @@ -63,26 +69,10 @@ module WithGithubFiles end def publish_github_files_with_descendents + target_objects = [self, descendents].flatten list_of_websites.each do |current_website| - website_github = Github.with_website current_website - next unless website_github.valid? - target_github_files = [] - github_manifest.each do |manifest_item| - github_file = github_files.where(website: current_website, manifest_identifier: manifest_item[:identifier]).first_or_create - target_github_files << github_file - github_file.add_to_batch(website_github) - descendents.each do |descendent| - next unless descendent.list_of_websites.include? current_website - descendent_github_file = descendent.github_files.where(website: current_website, manifest_identifier: manifest_item[:identifier]).first_or_create - target_github_files << descendent_github_file - descendent_github_file.add_to_batch(website_github) - end - end - if website_github.commit_batch("[#{self.class.name.demodulize}] Save #{to_s} & descendents") - target_github_files.each { |file| - file.update_column :github_path, file.manifest_data[:generated_path].call(file) - } - end + github = Github.with_website current_website + github.send_batch_to_website(target_objects, message: "[#{self.class.name.demodulize}] Save #{to_s} & descendents") end end @@ -97,8 +87,4 @@ module WithGithubFiles def should_unpublish_github_files? respond_to?(:published?) && saved_change_to_published? && !published? end - - def list_of_websites - respond_to?(:websites) ? websites : [website] - end end diff --git a/app/services/github.rb b/app/services/github.rb index 4a4203b28..2dd82a463 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -1,13 +1,14 @@ class Github - attr_reader :access_token, :repository + attr_reader :website, :access_token, :repository def self.with_website(website) - new website&.access_token, website&.repository + new website end - def initialize(access_token, repository) - @access_token = access_token - @repository = repository + def initialize(website) + @website = website + @access_token = website&.access_token + @repository = website&.repository end def valid? @@ -85,6 +86,27 @@ class Github '' end + def send_batch_to_website(objects, message: 'Batch objects') + return unless valid? + + github_files = [] + objects.each do |object| + next unless object.list_of_websites.include? website + object.github_manifest.each do |manifest_item| + github_file = object.github_files.where(website: website, manifest_identifier: manifest_item[:identifier]).first_or_create + github_files << github_file + github_file.add_to_batch(self) + end + end + + if commit_batch(message) + github_files.each do |github_file| + github_file.update_column :github_path, github_file.manifest_data[:generated_path].call(github_file) + end + end + end + handle_asynchronously :send_batch_to_website, queue: 'default' + protected def pages -- GitLab