Skip to content

Commit

Permalink
Merge pull request #483 from unepwcmc/export-custom-categories
Browse files Browse the repository at this point in the history
Export custom categories
  • Loading branch information
Levia authored Aug 18, 2020
2 parents 0aed033 + f4980c8 commit 9abab36
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion config/initializers/comfy_patching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

Comfy::Cms::Layout.class_eval do
has_many :layouts_categories, foreign_key: 'layout_id'
has_many :layouts_categories, foreign_key: 'layout_id', dependent: :destroy
has_many :layout_categories, through: :layouts_categories, foreign_key: 'layout_id'

accepts_nested_attributes_for :layouts_categories
Expand Down Expand Up @@ -57,4 +57,132 @@ def delete_orphan_categories
layout_categories.destroy_all
end
end

ComfortableMexicanSofa::Seeds::Page::Exporter.class_eval do
def fragments_data(record, page_path)
record.fragments.collect do |frag|
header = "#{frag.tag} #{frag.identifier}"
content =
case frag.tag
when "datetime", "date"
frag.datetime
when "checkbox"
frag.boolean
when "file", "files"
frag.attachments.map do |attachment|
::File.open(::File.join(page_path, attachment.filename.to_s), "wb") do |f|
f.write(attachment.download)
end
attachment.filename
end.join("\n")
# CUSTOM CODE - adding a case for categories to manually populate the content
when "categories"
layout_category = Comfy::Cms::LayoutCategory.where(label: frag.identifier).first
record.page_categories.where(layout_category: layout_category).map do |category|
category.label
end.join(' ')
# END OF CUSTOM CODE
else
frag.content
end

{ header: header, content: content }
end
end

end

ComfortableMexicanSofa::Seeds::Page::Importer.class_eval do
def import_page(path, parent)
slug = path.split("/").last

# setting page record
page =
if parent.present?
child = site.pages.where(slug: slug).first_or_initialize
child.parent = parent
child
else
site.pages.root || site.pages.new(slug: slug)
end

content_path = File.join(path, "content.html")

# If file is newer than page record we'll process it
if fresh_seed?(page, content_path)

# reading file content in, resulting in a hash
fragments_hash = parse_file_content(content_path)

# parsing attributes section
attributes_yaml = fragments_hash.delete("attributes")
attrs = YAML.safe_load(attributes_yaml)

# applying attributes
layout = site.layouts.find_by(identifier: attrs.delete("layout")) || parent.try(:layout)
category_ids = category_names_to_ids(page, attrs.delete("categories"))
target_page = attrs.delete("target_page")

page.attributes = attrs.merge(
layout: layout,
category_ids: category_ids
)

# applying fragments
old_frag_identifiers = page.fragments.pluck(:identifier)

new_frag_identifiers, fragments_attributes =
construct_fragments_attributes(fragments_hash, page, path)


# CUSTOM CODE
# Destroy existing categories tied to page
page.page_categories.destroy_all

# Set the page categories
new_categories = []
fragments_attributes.select { |attr| attr[:tag] == 'categories'}.each do |cat|
cat[:content].split(' ').each do |label|
category = Comfy::Cms::PageCategory.where(label: label).first
new_categories << category unless category.nil?
end
end

page.page_categories = new_categories unless new_categories.empty?
# END OF CUSTOM CODE


page.fragments_attributes = fragments_attributes

if page.save
message = "[CMS SEEDS] Imported Page \t #{page.full_path}"
ComfortableMexicanSofa.logger.info(message)

# defering target page linking
if target_page.present?
self.target_pages ||= {}
self.target_pages[page.id] = target_page
end

# cleaning up old fragments
page.fragments.where(identifier: old_frag_identifiers - new_frag_identifiers).destroy_all

else
message = "[CMS SEEDS] Failed to import Page \n#{page.errors.inspect}"
ComfortableMexicanSofa.logger.warn(message)
end
end

import_translations(path, page)

# Tracking what page from seeds we're working with. So we can remove pages
# that are no longer in seeds
seed_ids << page.id

# importing child pages (if there are any)
Dir["#{path}*/"].each do |page_path|
import_page(page_path, page)
end
end
end
end

0 comments on commit 9abab36

Please sign in to comment.