Class: Scribo::SiteImportService

Inherits:
ApplicationService show all
Defined in:
app/services/scribo/site_import_service.rb

Constant Summary collapse

IGNORED_FILES =
[%r[^__MACOSX/], %r[/\.DS_Store], %r[^/_site], /^node_modules/].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationService

#call

Constructor Details

#initialize(path, scribable: nil, properties: {}) ⇒ SiteImportService

Returns a new instance of SiteImportService.



13
14
15
16
17
18
# File 'app/services/scribo/site_import_service.rb', line 13

def initialize(path, scribable: nil, properties: {})
  super()
  @path = path
  @scribable = scribable
  @properties_override = properties
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'app/services/scribo/site_import_service.rb', line 9

def path
  @path
end

#properties_overrideObject (readonly)

Returns the value of attribute properties_override.



9
10
11
# File 'app/services/scribo/site_import_service.rb', line 9

def properties_override
  @properties_override
end

#scribableObject (readonly)

Returns the value of attribute scribable.



9
10
11
# File 'app/services/scribo/site_import_service.rb', line 9

def scribable
  @scribable
end

Instance Method Details

#performObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/scribo/site_import_service.rb', line 20

def perform
  Dir.mktmpdir do |dir|
    Dir.chdir(dir) do
      unzip(dir)

      all_files = Dir.glob('**/*')
      all_files = all_files.reject { |p| p == '_config.yml' } if properties_override.present?
      all_files.each do |name|
        parent = if File.dirname(name) == name
                   nil
                 else
                   site.contents.located(File.dirname(name), restricted: false).first
                 end
        content = parent.children.find_by(path: File.basename(name))  if parent  
        content = site.contents.find_by(path: File.basename(name), parent_id: nil)  if parent.nil? && content.nil?
        content = site.contents.create!(path: File.basename(name), parent: parent) if content.nil? 
        if File.directory?(name)
          content.kind = 'folder'
        else
          File.open(name) do |f|
            content.kind = Scribo::Utility.kind_for_path(name)
            content.data_with_frontmatter = f.read
          end
        end
        content.save!
      end
    end
  end
  site.contents.create(path: '_config.yml', data: YAML.dump(site.properties)) if properties_override.present?
  site
end