Class: ComfortableMediaSurfer::Seeds::File::Importer
- Defined in:
- lib/comfortable_media_surfer/seeds/file/importer.rb
Instance Attribute Summary
Attributes inherited from Importer
#from, #path, #seed_ids, #site, #to
Instance Method Summary collapse
- #import! ⇒ Object
-
#initialize(from, to = from) ⇒ Importer
constructor
A new instance of Importer.
Constructor Details
#initialize(from, to = from) ⇒ Importer
Returns a new instance of Importer.
5 6 7 8 |
# File 'lib/comfortable_media_surfer/seeds/file/importer.rb', line 5 def initialize(from, to = from) super self.path = ::File.join(ComfortableMediaSurfer.config.seeds_path, from, 'files/') end |
Instance Method Details
#import! ⇒ Object
10 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 |
# File 'lib/comfortable_media_surfer/seeds/file/importer.rb', line 10 def import! Dir["#{path}[^_]*"].each do |file_path| filename = ::File.basename(file_path) file = site.files. .where('active_storage_blobs.filename' => filename).references(:blob).first || site.files.new if File.exist?(attrs_path = File.join(path, "_#{filename}.yml")) && fresh_seed?(file, attrs_path) attrs = YAML.safe_load_file(attrs_path) category_ids = category_names_to_ids(file, attrs.delete('categories')) file.attributes = attrs.merge( category_ids: category_ids ) save(file, file_path) end if fresh_seed?(file, file_path) File.open(file_path) do |file_handler| file.file = { io: file_handler, filename: filename, content_type: MimeMagic.by_magic(file_handler) } save(file, file_path) end end seed_ids << file.id end # cleaning up site.files.where('id NOT IN (?)', seed_ids).destroy_all end |