Sha256: 8baa5c1e6e4a8d167eb883a9e8da015ee6818909370baabc03ca7e441b74afc0

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module Occams::Seeds::Snippet
  class Importer < Occams::Seeds::Importer
    def initialize(from, to = from)
      super
      self.path = ::File.join(Occams.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}"
            Occams.logger.info(message)
          else
            message = "[CMS SEEDS] Failed to import Snippet \n#{snippet.errors.inspect}"
            Occams.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

8 entries across 8 versions & 1 rubygems

Version Path
occams-1.0.7.1 lib/occams/seeds/snippet/importer.rb
occams-1.0.7 lib/occams/seeds/snippet/importer.rb
occams-1.0.6.1 lib/occams/seeds/snippet/importer.rb
occams-1.0.6 lib/occams/seeds/snippet/importer.rb
occams-1.0.5 lib/occams/seeds/snippet/importer.rb
occams-1.0.4 lib/occams/seeds/snippet/importer.rb
occams-1.0.3 lib/occams/seeds/snippet/importer.rb
occams-1.0.2 lib/occams/seeds/snippet/importer.rb