diff --git a/app/models/communication/website/imported/website.rb b/app/models/communication/website/imported/website.rb index f6062f4ed22a79b48e8f30f9c0c5e812cc5020fb..27d6630577f20a6aa1aeb084e38c2eac786804ee 100644 --- a/app/models/communication/website/imported/website.rb +++ b/app/models/communication/website/imported/website.rb @@ -63,49 +63,55 @@ class Communication::Website::Imported::Website < ApplicationRecord end def sync_pages - Communication::Website::Page.skip_callback(:save, :after, :publish_to_github) - wordpress.pages.each do |data| - page = pages.where(university: university, identifier: data['id']).first_or_initialize - page.data = data - page.save - end - # The order will treat parents before children - pages.order(:url).find_each do |page| - next if page.parent.blank? - parent = pages.where(identifier: page.parent).first - next if parent.nil? - generated_page = page.page - generated_page.parent = parent.page - generated_page.save - end - # 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 + begin + Communication::Website::Page.skip_callback(:save, :after, :publish_to_github) + wordpress.pages.each do |data| + page = pages.where(university: university, identifier: data['id']).first_or_initialize + page.data = data + page.save + end + # The order will treat parents before children + pages.order(:url).find_each do |page| + next if page.parent.blank? + parent = pages.where(identifier: page.parent).first + next if parent.nil? + generated_page = page.page + generated_page.parent = parent.page + generated_page.save end - github.commit_batch '[Page] Batch update from import' + # 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 + end + github.commit_batch '[Page] Batch update from import' + end + ensure + Communication::Website::Page.set_callback(:save, :after, :publish_to_github) end - Communication::Website::Page.set_callback(:save, :after, :publish_to_github) end def sync_posts - 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 + 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 + end end + github.commit_batch '[Post] Batch update from import' if github.valid? + ensure + Communication::Website::Post.set_callback(:save, :after, :publish_to_github) end - github.commit_batch '[Post] Batch update from import' if github.valid? - Communication::Website::Post.set_callback(:save, :after, :publish_to_github) end end diff --git a/app/models/communication/website/imported/with_rich_text.rb b/app/models/communication/website/imported/with_rich_text.rb index a1ac94cdaca424f38ac642a8ab04cc07fffff8bc..7111383447d45bc732416861e7224b130ebc89cd 100644 --- a/app/models/communication/website/imported/with_rich_text.rb +++ b/app/models/communication/website/imported/with_rich_text.rb @@ -5,16 +5,22 @@ module Communication::Website::Imported::WithRichText def rich_text_with_attachments(text) fragment = Nokogiri::HTML.fragment(text) - images = fragment.css("img[src*=\"#{website.website.uploads_url}\"]") - images.each do |image| + fragment = replace_tags_with_attachments(fragment, 'a', 'href') + fragment = replace_tags_with_attachments(fragment, 'img', 'src') + fragment.to_html + end + + def replace_tags_with_attachments(fragment, tag_name, attribute_name) + nodes = fragment.css("#{tag_name}[#{attribute_name}*=\"#{website.website.uploads_url}\"]") + nodes.each do |node| begin - url = image.attr('src') + url = node.attr(attribute_name) blob = load_blob_from_url(url) - image.replace ActionText::Attachment.from_attachable(blob).node.to_s + node.replace ActionText::Attachment.from_attachable(blob).node.to_s rescue end end - fragment.to_html + fragment end def load_blob_from_url(url) diff --git a/app/views/active_storage/blobs/_blob.html.erb b/app/views/active_storage/blobs/_blob.html.erb index 3daa58bc67b70fe52af1b1f38a6711885bc044bd..6ad1c8ba6ac06f84ce0c78ec30fdce2006851a6d 100644 --- a/app/views/active_storage/blobs/_blob.html.erb +++ b/app/views/active_storage/blobs/_blob.html.erb @@ -1,11 +1,21 @@ <figure class="attachment attachment--<%= blob.representable? ? "preview" : "file" %> attachment--<%= blob.filename.extension %>"> - <% if blob.representable? %> + <% if blob.image? %> <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> + <% elsif blob.video? %> + <video> + <source src="<%= rails_blob_path(blob) %>" type="<%= blob.content_type %>"> + </video> <% else %> - <p> - <span class="attachment__name"><%= blob.filename %></span> - <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> - </p> + <%= link_to blob, target: :blank do %> + <% if blob.representable? %> + <%= image_tag blob.representation(resize_to_limit: local_assigns[:in_gallery] ? [ 800, 600 ] : [ 1024, 768 ]) %> + <% else %> + <p> + <span class="attachment__name"><%= blob.filename %></span> + <span class="attachment__size"><%= number_to_human_size blob.byte_size %></span> + </p> + <% end %> + <% end %> <% end %> <% if caption = blob.try(:caption) %> diff --git a/app/views/admin/communication/website/pages/jekyll.html.erb b/app/views/admin/communication/website/pages/jekyll.html.erb index 16629fafabcfdfb45fb52529582a35d6bfc107dd..8d5582b042996db27d8b22135a353d1d868c50c9 100644 --- a/app/views/admin/communication/website/pages/jekyll.html.erb +++ b/app/views/admin/communication/website/pages/jekyll.html.erb @@ -1,6 +1,7 @@ --- title: "<%= @page.title %>" permalink: "<%= @page.path %>" +identifier: "<%= @page.id %>" parent: "<%= @page.parent_id %>" description: > <%= prepare_for_github @page.description %>