From 41a1dab1236b364cb6a78ec99768a2500376c8d4 Mon Sep 17 00:00:00 2001 From: Stanley Liu Date: Mon, 17 Aug 2020 16:12:41 +0100 Subject: [PATCH 1/5] crude monkey patch to allow custom categories to exported to and from pages --- config/initializers/comfy_patching.rb | 123 +++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/config/initializers/comfy_patching.rb b/config/initializers/comfy_patching.rb index e0b561eda..cf49fc51c 100644 --- a/config/initializers/comfy_patching.rb +++ b/config/initializers/comfy_patching.rb @@ -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 @@ -57,4 +57,125 @@ 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") + 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(' ') + 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) + + # 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 + end + end + page.page_categories = new_categories + + 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 \ No newline at end of file From dbebcc1b7b1a415bfff8ff910885c9c817219fb1 Mon Sep 17 00:00:00 2001 From: Stanley Liu Date: Tue, 18 Aug 2020 09:11:08 +0100 Subject: [PATCH 2/5] added comments to delineate when custom code begins and ends --- config/initializers/comfy_patching.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/initializers/comfy_patching.rb b/config/initializers/comfy_patching.rb index cf49fc51c..b176baa58 100644 --- a/config/initializers/comfy_patching.rb +++ b/config/initializers/comfy_patching.rb @@ -132,6 +132,8 @@ def import_page(path, parent) 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 @@ -143,7 +145,10 @@ def import_page(path, parent) new_categories << category end end + page.page_categories = new_categories + # CUSTOM CODE + page.fragments_attributes = fragments_attributes From 5bd5011b37d03af366fc6197a912653ad4f27a80 Mon Sep 17 00:00:00 2001 From: Stanley Liu Date: Tue, 18 Aug 2020 11:20:46 +0100 Subject: [PATCH 3/5] added more helpful comments --- config/initializers/comfy_patching.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/config/initializers/comfy_patching.rb b/config/initializers/comfy_patching.rb index b176baa58..0a06cab0e 100644 --- a/config/initializers/comfy_patching.rb +++ b/config/initializers/comfy_patching.rb @@ -75,11 +75,13 @@ def fragments_data(record, page_path) 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 @@ -147,7 +149,7 @@ def import_page(path, parent) end page.page_categories = new_categories - # CUSTOM CODE + # END OF CUSTOM CODE page.fragments_attributes = fragments_attributes From f7571ab33763573dec8aa9c418f1eff2f8b33cf7 Mon Sep 17 00:00:00 2001 From: Stanley Liu Date: Tue, 18 Aug 2020 13:52:12 +0100 Subject: [PATCH 4/5] added a condition to handle the possibility of no categories for a page --- config/initializers/comfy_patching.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/comfy_patching.rb b/config/initializers/comfy_patching.rb index 0a06cab0e..2cfb1c0fe 100644 --- a/config/initializers/comfy_patching.rb +++ b/config/initializers/comfy_patching.rb @@ -148,7 +148,7 @@ def import_page(path, parent) end end - page.page_categories = new_categories + page.page_categories = new_categories unless new_categories.empty? # END OF CUSTOM CODE From f4980c87682f8600c741543304c3c745f7e7beeb Mon Sep 17 00:00:00 2001 From: Stanley Liu Date: Tue, 18 Aug 2020 13:59:45 +0100 Subject: [PATCH 5/5] handling instance when category might be nil --- config/initializers/comfy_patching.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/comfy_patching.rb b/config/initializers/comfy_patching.rb index 2cfb1c0fe..10c4b5c27 100644 --- a/config/initializers/comfy_patching.rb +++ b/config/initializers/comfy_patching.rb @@ -144,7 +144,7 @@ def import_page(path, parent) 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 + new_categories << category unless category.nil? end end