Skip to content
Snippets Groups Projects
Commit bfb39dda authored by Arnaud Levy's avatar Arnaud Levy
Browse files

page and posts

parent 3f776fc5
No related branches found
No related tags found
No related merge requests found
...@@ -78,19 +78,15 @@ class Communication::Website::Page < ApplicationRecord ...@@ -78,19 +78,15 @@ class Communication::Website::Page < ApplicationRecord
children.each(&:save) children.each(&:save)
end end
def github_path def github_path_generated
"_pages/#{github_file}" "_pages/#{path}/index.html".gsub('//', '/')
end end
def publish_to_github def to_jekyll
github.publish kind: :pages, ApplicationController.render(
file: "#{ id }.html", template: 'admin/communication/website/pages/jekyll',
title: to_s, layout: false,
data: ApplicationController.render( assigns: { page: self }
template: 'admin/communication/website/pages/jekyll', )
layout: false,
assigns: { page: self }
)
end end
handle_asynchronously :publish_to_github
end end
...@@ -52,29 +52,15 @@ class Communication::Website::Post < ApplicationRecord ...@@ -52,29 +52,15 @@ class Communication::Website::Post < ApplicationRecord
protected protected
def github_file
"#{published_at.year}/#{published_at.month}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
end
def github_path_generated def github_path_generated
"_posts/#{published_at.year}/#{published_at.month}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html" "_posts/#{published_at.strftime "%Y/%m"}/#{published_at.strftime "%Y-%m-%d"}-#{slug}.html"
end end
def jekyll def to_jekyll
ApplicationController.render( ApplicationController.render(
template: 'admin/communication/website/posts/jekyll', template: 'admin/communication/website/posts/jekyll',
layout: false, layout: false,
assigns: { post: self } assigns: { post: self }
) )
end end
def publish_to_github
return if published_at.nil?
github.publish path: github_path_generated,
previous_path: github_path,
commit: "[post] Save #{title}",
data: jekyll
update_column :github_path, github_path_generated
end
# handle_asynchronously :publish_to_github
end end
...@@ -13,13 +13,8 @@ module Communication::Website::WithGithub ...@@ -13,13 +13,8 @@ module Communication::Website::WithGithub
@frontmatter ||= FrontMatterParser::Parser.new(:md).call(github_content) @frontmatter ||= FrontMatterParser::Parser.new(:md).call(github_content)
end end
def github_file
"#{ id }.html"
end
# Needs override
def github_path_generated def github_path_generated
'' '' # Needs override
end end
protected protected
...@@ -28,10 +23,16 @@ module Communication::Website::WithGithub ...@@ -28,10 +23,16 @@ module Communication::Website::WithGithub
@github ||= Github.with_site(website) @github ||= Github.with_site(website)
end end
# Needs override def github_commit_message
"[#{self.class.name.demodulize}] Save #{ to_s }"
end
def publish_to_github def publish_to_github
'' github.publish path: github_path_generated,
previous_path: github_path,
commit: github_commit_message,
data: to_jekyll
update_column :github_path, github_path_generated
end end
handle_asynchronously :publish_to_github handle_asynchronously :publish_to_github
end end
...@@ -24,6 +24,7 @@ class Github ...@@ -24,6 +24,7 @@ class Github
# Deprecated # Deprecated
local_path = "#{ tmp_directory }/#{ file }" local_path = "#{ tmp_directory }/#{ file }"
remote_file = "_#{ kind }/#{ file }" remote_file = "_#{ kind }/#{ file }"
commit = "Save #{ title }"
end end
Pathname(local_path).dirname.mkpath Pathname(local_path).dirname.mkpath
File.write local_path, data File.write local_path, data
...@@ -31,7 +32,6 @@ class Github ...@@ -31,7 +32,6 @@ class Github
if !previous_path.blank? && path != previous_path if !previous_path.blank? && path != previous_path
move_file previous_path, path move_file previous_path, path
end end
commit ||= "Save #{ title }"
client.create_contents repository, client.create_contents repository,
remote_file, remote_file,
commit, commit,
...@@ -93,20 +93,25 @@ class Github ...@@ -93,20 +93,25 @@ class Github
file = find_in_tree from file = find_in_tree from
return if file.nil? return if file.nil?
content = [{ content = [{
path: from,
mode: file[:mode],
type: file[:type],
sha: nil
},
{
path: to, path: to,
mode: file[:mode], mode: file[:mode],
type: file[:type], type: file[:type],
sha: file[:sha] sha: file[:sha]
}] }]
begin
new_tree = client.create_tree repository, content, base_tree: tree[:sha] new_tree = client.create_tree repository, content, base_tree: tree[:sha]
message = "Move #{from} to #{to}" message = "Move #{from} to #{to}"
commit = client.create_commit repository, message, new_tree[:sha], branch_sha commit = client.create_commit repository, message, new_tree[:sha], branch_sha
[:main, :master].each do |branch| [:main, :master].each do |branch|
client.update_branch repository, branch, commit[:sha] client.update_branch repository, branch, commit[:sha]
end end
rescue rescue
end ''
end end
def file_sha(path) def file_sha(path)
......
...@@ -4,8 +4,9 @@ ...@@ -4,8 +4,9 @@
<div class="d-flex align-items-center treeview__label border-bottom p-1"> <div class="d-flex align-items-center treeview__label border-bottom p-1">
<%= link_to children_admin_communication_website_page_path(website_id: page.website.id, id: page.id), <%= link_to children_admin_communication_website_page_path(website_id: page.website.id, id: page.id),
class: 'js-treeview-openzone d-inline-block p-2 ps-0', style: 'width: 22px', remote: true do %> class: 'js-treeview-openzone d-inline-block p-2 ps-0', style: 'width: 22px', remote: true do %>
<span class="open_btn"><i class="fas fa-folder"></i></span> <% icon_style = page.has_children? ? 'fas' : 'far' %>
<span class="close_btn"><i class="fas fa-folder-open"></i></span> <span class="open_btn"><i class="<%= icon_style %> fa-folder"></i></span>
<span class="close_btn"><i class="<%= icon_style %> fa-folder-open"></i></span>
<% end %> <% end %>
<%= link_to page, admin_communication_website_page_path(website_id: page.website.id, id: page.id) %> <%= link_to page, admin_communication_website_page_path(website_id: page.website.id, id: page.id) %>
<span class="move_btn py-2 ps-2"><i class="fas fa-sort"></i></span> <span class="move_btn py-2 ps-2"><i class="fas fa-sort"></i></span>
......
...@@ -12,28 +12,27 @@ ...@@ -12,28 +12,27 @@
<div class="card mt-5"> <div class="card mt-5">
<div class="card-header"> <div class="card-header">
<div class="float-end"> <div class="float-end">
<%= link_to t('activerecord.models.communication/website/post.all'),
admin_communication_website_posts_path(website_id: @website),
class: 'me-3' %>
<%= link_to t('create'), <%= link_to t('create'),
new_admin_communication_website_post_path(website_id: @website), new_admin_communication_website_post_path(website_id: @website),
class: button_classes %> class: button_classes %>
</div> </div>
<h2 class="card-title"><%= Communication::Website::Post.model_name.human(count: 2) %></h2> <h2 class="card-title">
<%= link_to Communication::Website::Post.model_name.human(count: 2),
admin_communication_website_posts_path(website_id: @website) %></h2>
</div> </div>
<%= render 'admin/communication/website/posts/list', posts: @website.posts.recent %> <%= render 'admin/communication/website/posts/list', posts: @website.posts.recent %>
</div> </div>
<div class="card mt-5"> <div class="card mt-5">
<div class="card-header"> <div class="card-header">
<div class="float-end"> <div class="float-end">
<%= link_to t('activerecord.models.communication/website/page.all'),
admin_communication_website_pages_path(website_id: @website),
class: 'me-3' %>
<%= link_to t('create'), <%= link_to t('create'),
new_admin_communication_website_page_path(website_id: @website), new_admin_communication_website_page_path(website_id: @website),
class: button_classes %> class: button_classes %>
</div> </div>
<h2 class="card-title"><%= Communication::Website::Page.model_name.human(count: 2) %></h2> <h2 class="card-title">
<%= link_to Communication::Website::Page.model_name.human(count: 2),
admin_communication_website_pages_path(website_id: @website) %>
</h2>
</div> </div>
<%= render 'admin/communication/website/pages/list', pages: @website.pages.recent %> <%= render 'admin/communication/website/pages/list', pages: @website.pages.recent %>
</div> </div>
......
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