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

force publish website

parent 1fa8a082
No related branches found
No related tags found
No related merge requests found
......@@ -31,6 +31,11 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati
add_breadcrumb Communication::Website::Imported::Website.model_name.human
end
def publish
@website.force_publish!
redirect_to admin_communication_website_path(@website), notice: t('admin.will_be_published_html', model: @website.to_s)
end
def edit
breadcrumb
add_breadcrumb t('edit')
......
......@@ -51,6 +51,10 @@ class Communication::Website < ApplicationRecord
[nil, Education::School.name, Research::Journal.name]
end
def to_s
"#{name}"
end
def domain_url
"https://#{domain}"
end
......@@ -81,7 +85,57 @@ class Communication::Website < ApplicationRecord
all_pages
end
def to_s
"#{name}"
def force_publish!
publish_authors
publish_categories
publish_pages
publish_posts
end
handle_asynchronously :force_publish!, queue: 'default'
def publish_authors!
publish_objects(Communication::Website::Author, authors)
end
def publish_categories!
publish_objects(Communication::Website::Category, categories)
end
def publish_pages!
publish_objects_with_blobs(Communication::Website::Page, pages)
end
def publish_posts!
publish_objects_with_blobs(Communication::Website::Post, posts)
end
protected
def publish_objects(model, objects)
begin
had_callback = model.__callbacks[:save].find { |c| c.matches?(:after, :publish_to_github) }
model.skip_callback(:save, :after, :publish_to_github) if had_callback
github = Github.with_site self
return unless github.valid?
objects.each do |object|
github.add_to_batch path: object.github_path_generated,
previous_path: object.github_path,
data: object.to_jekyll
yield(github, object) if block_given?
end
github.commit_batch "[#{model.name.demodulize}] Batch update from import"
ensure
model.set_callback(:save, :after, :publish_to_github) if had_callback
end
end
def publish_objects_with_blobs(model, objects)
publish_objects(model, objects) { |github, object|
object.active_storage_blobs.each do |blob|
blob.analyze unless blob.analyzed?
github.add_to_batch path: object.blob_github_path_generated(blob),
data: object.blob_to_jekyll(blob)
end
}
end
end
......@@ -55,20 +55,34 @@ class Communication::Website::Imported::Website < ApplicationRecord
end
def sync_authors
wordpress.authors.each do |data|
author = authors.where(university: university, identifier: data['id']).first_or_initialize
author.data = data
author.save
begin
Communication::Website::Author.skip_callback(:save, :after, :publish_to_github)
wordpress.authors.each do |data|
author = authors.where(university: university, identifier: data['id']).first_or_initialize
author.data = data
author.save
end
# Batch update all changes (1 query only, good for github API limits)
website.publish_authors!
ensure
Communication::Website::Author.set_callback(:save, :after, :publish_to_github)
end
end
def sync_categories
wordpress.categories.each do |data|
category = categories.where(university: university, identifier: data['id']).first_or_initialize
category.data = data
category.save
begin
Communication::Website::Category.skip_callback(:save, :after, :publish_to_github)
wordpress.categories.each do |data|
category = categories.where(university: university, identifier: data['id']).first_or_initialize
category.data = data
category.save
end
sync_tree(categories)
# Batch update all changes (1 query only, good for github API limits)
website.publish_categories!
ensure
Communication::Website::Category.set_callback(:save, :after, :publish_to_github)
end
sync_tree(categories)
end
def sync_media
......@@ -89,20 +103,7 @@ class Communication::Website::Imported::Website < ApplicationRecord
end
sync_tree(pages)
# Batch update all changes (1 query only, good for github API limits)
github = Github.with_site website
if github.valid?
website.pages.find_each do |page|
github.add_to_batch path: page.github_path_generated,
previous_path: page.github_path,
data: page.to_jekyll
page.active_storage_blobs.each do |blob|
blob.analyze unless blob.analyzed?
github.add_to_batch path: page.blob_github_path_generated(blob),
data: page.blob_to_jekyll(blob)
end
end
github.commit_batch '[Page] Batch update from import'
end
website.publish_pages!
ensure
Communication::Website::Page.set_callback(:save, :after, :publish_to_github)
end
......@@ -111,24 +112,13 @@ class Communication::Website::Imported::Website < ApplicationRecord
def sync_posts
begin
Communication::Website::Post.skip_callback(:save, :after, :publish_to_github)
github = Github.with_site website
wordpress.posts.each do |data|
post = posts.where(university: university, identifier: data['id']).first_or_initialize
post.data = data
post.save
generated_post = post.post
if github.valid?
github.add_to_batch path: generated_post.github_path_generated,
previous_path: generated_post.github_path,
data: generated_post.to_jekyll
generated_post.active_storage_blobs.each do |blob|
blob.analyze unless blob.analyzed?
github.add_to_batch path: generated_post.blob_github_path_generated(blob),
data: generated_post.blob_to_jekyll(blob)
end
end
end
github.commit_batch '[Post] Batch update from import' if github.valid?
# Batch update all changes (1 query only, good for github API limits)
website.publish_posts!
ensure
Communication::Website::Post.set_callback(:save, :after, :publish_to_github)
end
......
......@@ -116,6 +116,13 @@
</div>
</div>
<% content_for :action_bar_left do %>
<%= link_to t('communication.website.force_publication'),
publish_admin_communication_website_path(@website),
method: :post,
class: button_classes %>
<% end %>
<% content_for :action_bar_right do %>
<% if @website.imported? %>
<%= link_to t('communication.website.imported.show'),
......
......@@ -3,6 +3,7 @@ namespace :communication do
member do
get :import
post :import
post :publish
end
resources :pages, controller: 'website/pages' 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