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

github_manifest

parent 6b3a96d7
No related branches found
No related tags found
No related merge requests found
......@@ -2,13 +2,14 @@
#
# Table name: communication_website_github_files
#
# id :uuid not null, primary key
# about_type :string not null
# github_path :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
# id :uuid not null, primary key
# about_type :string not null
# github_path :string
# manifest_identifier :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
#
# Indexes
#
......@@ -80,9 +81,9 @@ class Communication::Website::GithubFile < ApplicationRecord
def github_params
{
path: about.github_path_generated,
path: manifest_data[:generated_path],
previous_path: github_path,
data: about.to_jekyll(self)
data: manifest_data[:data].call(self)
}
end
......@@ -106,10 +107,6 @@ class Communication::Website::GithubFile < ApplicationRecord
"[#{about.class.name.demodulize}] Save #{about.to_s}"
end
def github_blob_commit_message(blob)
"[Medium] Save ##{blob.id}"
end
def github_remove_commit_message
"[#{about.class.name.demodulize}] Remove #{about.to_s}"
end
......@@ -117,4 +114,10 @@ class Communication::Website::GithubFile < ApplicationRecord
def github_blob_remove_commit_message(blob)
"[Medium] Remove ##{blob.id}"
end
def manifest_data
@manifest_data ||= about.github_manifest.detect { |item|
item[:identifier] == manifest_identifier
}
end
end
......@@ -60,7 +60,7 @@ class Communication::Website::Imported::Website < ApplicationRecord
def sync_authors
begin
Communication::Website::Author.skip_callback(:save, :after, :publish_github_files)
skip_publish_callback(Communication::Website::Author)
wordpress.authors.each do |data|
author = authors.where(university: university, identifier: data['id']).first_or_initialize
author.data = data
......@@ -69,13 +69,13 @@ class Communication::Website::Imported::Website < ApplicationRecord
# Batch update all changes (1 query only, good for github API limits)
website.publish_authors!
ensure
Communication::Website::Author.set_callback(:save, :after, :publish_github_files)
set_publish_callback(Communication::Website::Author)
end
end
def sync_categories
begin
Communication::Website::Category.skip_callback(:save, :after, :publish_github_files)
skip_publish_callback(Communication::Website::Category)
wordpress.categories.each do |data|
category = categories.where(university: university, identifier: data['id']).first_or_initialize
category.data = data
......@@ -85,7 +85,7 @@ class Communication::Website::Imported::Website < ApplicationRecord
# Batch update all changes (1 query only, good for github API limits)
website.publish_categories!
ensure
Communication::Website::Category.set_callback(:save, :after, :publish_github_files)
set_publish_callback(Communication::Website::Category)
end
end
......@@ -99,7 +99,7 @@ class Communication::Website::Imported::Website < ApplicationRecord
def sync_pages
begin
Communication::Website::Page.skip_callback(:save, :after, :publish_github_files)
skip_publish_callback(Communication::Website::Page)
wordpress.pages.each do |data|
page = pages.where(university: university, identifier: data['id']).first_or_initialize
page.data = data
......@@ -109,13 +109,13 @@ class Communication::Website::Imported::Website < ApplicationRecord
# Batch update all changes (1 query only, good for github API limits)
website.publish_pages!
ensure
Communication::Website::Page.set_callback(:save, :after, :publish_github_files)
set_publish_callback(Communication::Website::Page)
end
end
def sync_posts
begin
Communication::Website::Post.skip_callback(:save, :after, :publish_github_files)
skip_publish_callback(Communication::Website::Post)
wordpress.posts.each do |data|
post = posts.where(university: university, identifier: data['id']).first_or_initialize
post.data = data
......@@ -124,7 +124,7 @@ class Communication::Website::Imported::Website < ApplicationRecord
# Batch update all changes (1 query only, good for github API limits)
website.publish_posts!
ensure
Communication::Website::Post.set_callback(:save, :after, :publish_github_files)
set_publish_callback(Communication::Website::Post)
end
end
......@@ -139,4 +139,13 @@ class Communication::Website::Imported::Website < ApplicationRecord
generated_element.save
end
end
def skip_publish_callback(model)
model.skip_callback(:commit, :after, :publish_github_files, on: [:create, :update])
end
def set_publish_callback(model)
model.set_callback(:commit, :after, :publish_github_files, on: [:create, :update])
end
end
......@@ -25,18 +25,32 @@ module WithGithubFiles
)
end
def github_manifest
[
{
identifier: "primary",
generated_path: github_path_generated,
data: -> (github_file) { to_jekyll(github_file) }
}
]
end
protected
def create_github_files
list_of_websites.each do |website|
github_files.find_or_create_by(website: website)
github_manifest.each do |manifest_item|
github_files.find_or_create_by(website: website, manifest_identifier: manifest_item[:identifier])
end
end
end
def publish_github_files
list_of_websites.each do |website|
github_file = github_files.find_or_create_by(website: website)
github_file.publish
github_manifest.each do |manifest_item|
github_file = github_files.find_or_create_by(website: website, manifest_identifier: manifest_item[:identifier])
github_file.publish
end
end
end
......
class AddManifestIdentifierToCommunicationWebsiteGithubFiles < ActiveRecord::Migration[6.1]
def change
add_column :communication_website_github_files, :manifest_identifier, :string
end
end
......@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2021_12_06_102444) do
ActiveRecord::Schema.define(version: 2021_12_09_110929) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
......@@ -126,6 +126,7 @@ ActiveRecord::Schema.define(version: 2021_12_06_102444) do
t.uuid "website_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "manifest_identifier"
t.index ["about_type", "about_id"], name: "index_communication_website_github_files_on_about"
t.index ["website_id"], name: "index_communication_website_github_files_on_website_id"
end
......
......@@ -2,13 +2,14 @@
#
# Table name: communication_website_github_files
#
# id :uuid not null, primary key
# about_type :string not null
# github_path :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
# id :uuid not null, primary key
# about_type :string not null
# github_path :string
# manifest_identifier :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
#
# Indexes
#
......@@ -19,7 +20,6 @@
#
# fk_rails_... (website_id => communication_websites.id)
#
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
github_path: MyString
......
......@@ -2,13 +2,14 @@
#
# Table name: communication_website_github_files
#
# id :uuid not null, primary key
# about_type :string not null
# github_path :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
# id :uuid not null, primary key
# about_type :string not null
# github_path :string
# manifest_identifier :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid not null
# website_id :uuid not null
#
# Indexes
#
......
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