diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb index 7b37bf23b8168ca608b02ee73d7a25ed8968fad5..b03cf1d366d035be4c153d971e478bd43d009f66 100644 --- a/app/controllers/admin/communication/website/pages_controller.rb +++ b/app/controllers/admin/communication/website/pages_controller.rb @@ -1,14 +1,13 @@ 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 diff --git a/app/models/communication/website.rb b/app/models/communication/website.rb index a66d7b30c2894bb03b76571bded2a066076641de..1c88b3614b2a140cd5ebe87d360b0d94f8d0fa73 100644 --- a/app/models/communication/website.rb +++ b/app/models/communication/website.rb @@ -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 diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index e1b80d24a52b7f829eb93fede601bf82d68891d2..cb29403773e27d5a8ce1f1ee1e09d6707f396c48 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -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 diff --git a/app/services/github.rb b/app/services/github.rb index 7999d7af98384a415c7e313c4ca4b04bb73182f0..07694d47fad69774a1944a53317c186665f9226e 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -1,6 +1,10 @@ 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| diff --git a/app/views/admin/communication/website/pages/index.html.erb b/app/views/admin/communication/website/pages/index.html.erb index 68b0968f7628475af859badbf763e45c6afd16f3..829bfcfff9baf12f699bdba5c11efbc5da44b212 100644 --- a/app/views/admin/communication/website/pages/index.html.erb +++ b/app/views/admin/communication/website/pages/index.html.erb @@ -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 %> diff --git a/app/views/admin/communication/website/pages/jekyll.html.erb b/app/views/admin/communication/website/pages/jekyll.html.erb new file mode 100644 index 0000000000000000000000000000000000000000..d4e12cef2a567cd9b2321f54bc2c0a6b8ad73d92 --- /dev/null +++ b/app/views/admin/communication/website/pages/jekyll.html.erb @@ -0,0 +1,7 @@ +--- +title: "<%= @page.title %>" +permalink: "<%= @page.path %>" +parent: "<%= @page.parent_id %>" +description: "<%= @page.description %>" +--- +<%= @page.content_without_frontmatter.html_safe %>