class ComfortableMexicanSofa::Seeds::Importer

Attributes

from[RW]
path[RW]
seed_ids[RW]
site[RW]
to[RW]

Public Class Methods

new(from, to = from) click to toggle source

`from` and `to` indicate site identifier and folder name

# File lib/comfortable_mexican_sofa/seeds.rb, line 20
def initialize(from, to = from)
  self.from         = from
  self.to           = to
  self.site         = Comfy::Cms::Site.where(identifier: to).first!
  self.seed_ids     = []

  unless ::File.exist?(path = ::File.join(ComfortableMexicanSofa.config.seeds_path, from))
    raise Error, "Folder for import: '#{path}' is not found"
  end
end

Public Instance Methods

import!(classes = nil) click to toggle source

if passed nil will use default seed classes

# File lib/comfortable_mexican_sofa/seeds.rb, line 32
def import!(classes = nil)
  classes ||= SEED_CLASSES
  classes.each do |klass|
    klass = "ComfortableMexicanSofa::Seeds::#{klass}::Importer"
    klass.constantize.new(from, to).import!
  end
end

Private Instance Methods

category_names_to_ids(record, names) click to toggle source
# File lib/comfortable_mexican_sofa/seeds.rb, line 60
def category_names_to_ids(record, names)
  [names].flatten.map do |name|
    category = site.categories.find_or_create_by(
      label:            name,
      categorized_type: record.class.to_s
    )
    category.id
  end
end
fresh_seed?(object, file_path) click to toggle source
# File lib/comfortable_mexican_sofa/seeds.rb, line 56
def fresh_seed?(object, file_path)
  object.new_record? || ::File.mtime(file_path) > object.updated_at
end
parse_file_content(file_path) click to toggle source

Splitting file content in sections delimited by headers that look like this:

[header]
some content
[header 2]
some more content
# File lib/comfortable_mexican_sofa/seeds.rb, line 47
def parse_file_content(file_path)
  text = ::File.read(file_path)
  tokens = text.split(%r{^\[(.*?)\]\r?\n})
  tokens.shift # first item should be blank
  tokens.in_groups_of(2).each_with_object({}) do |pair, h|
    h[pair[0]] = pair[1]
  end
end