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

batch objects

parent fd80b2d8
No related branches found
No related tags found
No related merge requests found
...@@ -10,13 +10,20 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web ...@@ -10,13 +10,20 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web
def reorder def reorder
parent_id = params[:parentId].blank? ? nil : params[:parentId] parent_id = params[:parentId].blank? ? nil : params[:parentId]
ids = params[:ids] || [] ids = params[:ids] || []
pages = []
github_files = []
ids.each.with_index do |id, index| ids.each.with_index do |id, index|
page = @website.pages.find(id) page = @website.pages.find(id)
pages << page
pages.concat(page.descendents) if parent_id != page.parent_id
page.update( page.update(
parent_id: parent_id, parent_id: parent_id,
position: index + 1 position: index + 1,
skip_github_publication: true
) )
end end
github = Github.with_website @website
github.send_batch_to_website(pages, message: '[Page] Reorder pages.')
end end
def children def children
......
...@@ -2,13 +2,15 @@ module WithGithubFiles ...@@ -2,13 +2,15 @@ module WithGithubFiles
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
attr_accessor :skip_github_publication
has_many :github_files, has_many :github_files,
class_name: "Communication::Website::GithubFile", class_name: "Communication::Website::GithubFile",
as: :about, as: :about,
dependent: :destroy dependent: :destroy
after_save :create_github_files 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? after_save_commit :unpublish_github_files, if: :should_unpublish_github_files?
end end
...@@ -39,6 +41,10 @@ module WithGithubFiles ...@@ -39,6 +41,10 @@ module WithGithubFiles
] ]
end end
def list_of_websites
respond_to?(:websites) ? websites : [website]
end
protected protected
def create_github_files def create_github_files
...@@ -63,26 +69,10 @@ module WithGithubFiles ...@@ -63,26 +69,10 @@ module WithGithubFiles
end end
def publish_github_files_with_descendents def publish_github_files_with_descendents
target_objects = [self, descendents].flatten
list_of_websites.each do |current_website| list_of_websites.each do |current_website|
website_github = Github.with_website current_website github = Github.with_website current_website
next unless website_github.valid? github.send_batch_to_website(target_objects, message: "[#{self.class.name.demodulize}] Save #{to_s} & descendents")
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
end end
end end
...@@ -97,8 +87,4 @@ module WithGithubFiles ...@@ -97,8 +87,4 @@ module WithGithubFiles
def should_unpublish_github_files? def should_unpublish_github_files?
respond_to?(:published?) && saved_change_to_published? && !published? respond_to?(:published?) && saved_change_to_published? && !published?
end end
def list_of_websites
respond_to?(:websites) ? websites : [website]
end
end end
class Github class Github
attr_reader :access_token, :repository attr_reader :website, :access_token, :repository
def self.with_website(website) def self.with_website(website)
new website&.access_token, website&.repository new website
end end
def initialize(access_token, repository) def initialize(website)
@access_token = access_token @website = website
@repository = repository @access_token = website&.access_token
@repository = website&.repository
end end
def valid? def valid?
...@@ -85,6 +86,27 @@ class Github ...@@ -85,6 +86,27 @@ class Github
'' ''
end 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 protected
def pages def pages
......
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