Sha256: 75189d1d92421592d78f956a9b40fae481b03762115485dfeb9fe2e96fa2f197

Contents?: true

Size: 1.81 KB

Versions: 17

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module ComfortableMexicanSofa::Seeds::Layout
  class Importer < ComfortableMexicanSofa::Seeds::Importer

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

    def import!(path = self.path, parent = nil)
      Dir["#{path}*/"].each do |layout_path|
        import_layout(layout_path, parent)
      end

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

  private

    def import_layout(path, parent)
      identifier = path.split("/").last

      # reading file content in, resulting in a hash
      content_path = File.join(path, "content.html")
      content_hash = parse_file_content(content_path)

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

      layout = site.layouts.where(identifier: identifier).first_or_initialize
      layout.parent = parent

      if fresh_seed?(layout, content_path)
        layout.attributes = attrs.merge(
          app_layout: attrs["app_layout"] || parent.try(:app_layout),
          content:    content_hash["content"],
          js:         content_hash["js"],
          css:        content_hash["css"]
        )

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

      seed_ids << layout.id

      # importing child layouts (if there are any)
      Dir["#{path}*/"].each do |layout_path|
        import_layout(layout_path, layout)
      end
    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/layout/importer.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
kcy1-1.0 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
comfortable_mexican_sofa-2.0.17 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
comfortable_mexican_sofa-2.0.16 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
comfortable_mexican_sofa-2.0.15 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
comfortable_mexican_sofa-2.0.14 lib/comfortable_mexican_sofa/seeds/layout/importer.rb
comfortable_mexican_sofa-2.0.13 lib/comfortable_mexican_sofa/seeds/layout/importer.rb