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

update

parent 56688e18
No related branches found
No related tags found
No related merge requests found
class Admin::Communication::Website::PagesController < Admin::Communication::Website::ApplicationController
load_and_authorize_resource class: Communication::Website::Page
def index
@pages = @website.pages
@pages = @website.pages.order(:path)
breadcrumb
end
def show
id = "#{params[:id]}.html"
@page = Communication::Website::Page.find(id, @website)
breadcrumb
add_breadcrumb @page
end
def new
......@@ -48,8 +47,9 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web
def breadcrumb
super
add_breadcrumb Communication::Website::Page.model_name.human(count: 2), admin_communication_website_pages_path
# breadcrumb_for @page
add_breadcrumb Communication::Website::Page.model_name.human(count: 2),
admin_communication_website_pages_path
breadcrumb_for @page
end
def page_params
......
......@@ -25,16 +25,12 @@
class Communication::Website < ApplicationRecord
belongs_to :university
belongs_to :about, polymorphic: true, optional: true
# has_many :pages, foreign_key: :communication_website_id
has_many :pages, foreign_key: :communication_website_id
def self.about_types
[nil, Research::Journal.name]
end
def pages
Communication::Website::Page.for_website self
end
def domain_url
"https://#{ domain }"
end
......
......@@ -31,63 +31,53 @@
# fk_rails_... (university_id => universities.id)
#
# class Communication::Website::Page < ApplicationRecord
# belongs_to :university
# belongs_to :website, foreign_key: :communication_website_id
# belongs_to :parent, class_name: 'Communication::Website::Page', optional: true
# belongs_to :about, polymorphic: true, optional: true
#
# validates :title, presence: true
#
# before_save :make_path
#
# def to_s
# "#{ title }"
# end
#
# protected
#
# def make_path
# self.path = "#{parent&.path}/#{slug}"
# end
# end
class Communication::Website::Page < ApplicationRecord
belongs_to :university
belongs_to :website, foreign_key: :communication_website_id
belongs_to :parent, class_name: 'Communication::Website::Page', optional: true
class Communication::Website::Page
extend ActiveModel::Naming
extend ActiveModel::Translation
validates :title, presence: true
attr_accessor :id, :title, :permalink, :content, :raw
before_save :make_path
after_save :publish_to_github
def self.for_website(website)
return [] if website.repository.blank?
github = Github.new website.access_token, website.repository
github.pages
def content
@content ||= github.read_file_at "_pages/#{id}.html"
end
def self.find(id, website)
return [] if website.repository.blank?
github = Github.new website.access_token, website.repository
github.page_with_id(id)
def content_without_frontmatter
frontmatter.content
end
def description
""
def to_s
"#{ title }"
end
def slug
""
protected
def github
@github ||= Github.with_site(website)
end
def path
permalink
def frontmatter
@frontmatter ||= FrontMatterParser::Parser.new(:md).call(content)
end
def persisted?
true
def make_path
self.path = "#{parent&.path}/#{slug}"
end
def to_s
"#{ title }"
def publish_to_github
return if website&.repository.blank?
data = ApplicationController.render(
template: 'admin/communication/website/pages/jekyll',
layout: false,
assigns: { page: self }
)
github.publish local_directory: "tmp/pages",
local_file: "#{id}.md",
data: data,
remote_file: "_pages/#{id}.html",
commit_message: "Save page #{ title }"
end
end
class Github
attr_reader :access_token, :repository
def self.with_site(site)
new site.access_token, site.repository
end
def initialize(access_token, repository)
@access_token = access_token
@repository = repository
......@@ -24,6 +28,13 @@ class Github
end
def read_file_at(path)
data = client.content repository, path: path
Base64.decode64 data.content
rescue
''
end
def pages
list = client.contents repository, path: '_pages'
list.map do |hash|
......
......@@ -4,7 +4,7 @@
<thead>
<tr>
<th><%= Communication::Website::Page.human_attribute_name('title') %></th>
<th><%= Communication::Website::Page.human_attribute_name('permalink') %></th>
<th><%= Communication::Website::Page.human_attribute_name('path') %></th>
<th></th>
</tr>
</thead>
......@@ -12,8 +12,10 @@
<% @pages.each do |page| %>
<tr>
<td><%= link_to page, admin_communication_website_page_path(page.id) %></td>
<td><%= page.permalink %></td>
<td><%= page.path %></td>
<td class="text-end">
<%= edit_link page %>
<%= destroy_link page %>
</td>
</tr>
<% end %>
......
---
title: "<%= @page.title %>"
permalink: "<%= @page.path %>"
parent: "<%= @page.parent_id %>"
description: "<%= @page.description %>"
---
<%= @page.content_without_frontmatter.html_safe %>
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