Sha256: 6ea57997230ffccdb6a137ab833e2c0f93cbe179307ca5524e6101b69242ec1a

Contents?: true

Size: 1.59 KB

Versions: 17

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module ComfortableMexicanSofa::Seeds::Snippet
  class Importer < ComfortableMexicanSofa::Seeds::Importer

    def initialize(from, to = from)
      super
      self.path = ::File.join(ComfortableMexicanSofa.config.seeds_path, from, "snippets/")
    end

    def import!
      Dir.glob("#{path}/*.html").each do |path|
        identifier = File.basename(path, ".html")

        # reading file content in, resulting in a hash
        content_hash = parse_file_content(path)

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

        snippet = site.snippets.where(identifier: identifier).first_or_initialize

        if fresh_seed?(snippet, path)
          category_ids = category_names_to_ids(snippet, attrs.delete("categories"))

          snippet.attributes = attrs.merge(
            category_ids: category_ids,
            content:      content_hash["content"]
          )

          if snippet.save
            message = "[CMS SEEDS] Imported Snippet \t #{snippet.identifier}"
            ComfortableMexicanSofa.logger.info(message)
          else
            message = "[CMS SEEDS] Failed to import Snippet \n#{snippet.errors.inspect}"
            ComfortableMexicanSofa.logger.warn(message)
          end
        end

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

      # cleaning up
      site.snippets.where("id NOT IN (?)", seed_ids).destroy_all
    end

  end
end

Version data entries

17 entries across 17 versions & 6 rubygems

Version Path
comfortable_mexican_sofa-2.0.19 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
kcy1-1.0 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
comfortable_mexican_sofa-2.0.17 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
comfortable_mexican_sofa-2.0.16 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
comfortable_mexican_sofa-2.0.15 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
comfortable_mexican_sofa-2.0.14 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb
comfortable_mexican_sofa-2.0.13 lib/comfortable_mexican_sofa/seeds/snippet/importer.rb