Class: ComfortableMexicanSofa::Seeds::File::Importer

Inherits:
Importer
  • Object
show all
Defined in:
lib/comfortable_mexican_sofa/seeds/file/importer.rb

Instance Attribute Summary

Attributes inherited from Importer

#from, #path, #seed_ids, #site, #to

Instance Method Summary collapse

Constructor Details

#initialize(from, to = from) ⇒ Importer

Returns a new instance of Importer.



6
7
8
9
# File 'lib/comfortable_mexican_sofa/seeds/file/importer.rb', line 6

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

Instance Method Details

#import!Object



11
12
13
14
15
16
17
18
19
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
51
52
53
54
55
56
57
58
59
60
# File 'lib/comfortable_mexican_sofa/seeds/file/importer.rb', line 11

def import!
  Dir["#{path}[^_]*"].each do |file_path|
    filename = ::File.basename(file_path)

    file = site.files.with_attached_attachment
      .where("active_storage_blobs.filename" => filename).references(:blob).first ||
      site.files.new

    # We need to track actual file and its attributes
    fresh_file = false

    if File.exist?(attrs_path = File.join(path, "_#{filename}.yml"))
      if fresh_seed?(file, attrs_path)
        fresh_file = true

        attrs = YAML.safe_load(File.read(attrs_path))
        category_ids = category_names_to_ids(file, attrs.delete("categories"))
        file.attributes = attrs.merge(
          category_ids: category_ids
        )
      end
    end

    if fresh_seed?(file, file_path)
      fresh_file = true

      file_handler = File.open(file_path)
      file.file = {
        io:           file_handler,
        filename:     filename,
        content_type: MimeMagic.by_magic(file_handler)
      }
    end

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

    seed_ids << file.id
  end

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