Class: Workarea::HelpSeeds

Inherits:
Object
  • Object
show all
Defined in:
app/seeds/workarea/help_seeds.rb

Instance Method Summary collapse

Instance Method Details

#find_assets_for_article(article_path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/seeds/workarea/help_seeds.rb', line 32

def find_assets_for_article(article_path)
  assets_path = article_path.join('assets')
  return {} unless assets_path.directory?

  assets_path.children.reduce({}) do |memo, asset_path|
    asset_reference = asset_path.basename.to_s.split('.').first
    asset = Help::Asset.create!(file: asset_path)

    memo[asset_reference] = asset.url
    memo
  end
end

#find_markdown_for(article_path, file, assets) ⇒ Object



45
46
47
48
49
# File 'app/seeds/workarea/help_seeds.rb', line 45

def find_markdown_for(article_path, file, assets)
  summary_path = article_path.join(file)
  return nil unless File.exist?(summary_path)
  render_markdown_with_assets(summary_path, assets)
end

#find_thumbnail_for(article_path) ⇒ Object



51
52
53
54
55
56
57
# File 'app/seeds/workarea/help_seeds.rb', line 51

def find_thumbnail_for(article_path)
  %w(png jpg jpeg gif).each do |format|
    thumbnail_path = article_path.join("thumbnail.#{format}")
    return thumbnail_path if File.exist?(thumbnail_path)
  end
  nil
end

#performObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/seeds/workarea/help_seeds.rb', line 3

def perform
  puts 'Adding help articles...'

  help = Core::Engine.root.join('data', 'help')
  categories = help.children.select(&:directory?)

  categories.each do |category_path|
    category = category_path.basename.to_s.titleize

    category_path.children.each do |article_path|
      next unless article_path.directory?

      new_article = Help::Article.new(
        name: article_path.basename.to_s.titleize,
        category: category
      )

      unless Help::Article.where(id: new_article.id).exists?
        assets = find_assets_for_article(article_path)

        new_article.thumbnail = find_thumbnail_for(article_path)
        new_article.summary = find_markdown_for(article_path, 'summary.md', assets)
        new_article.body = find_markdown_for(article_path, 'body.md', assets)
        new_article.save!
      end
    end
  end
end

#render_markdown_with_assets(raw_path, assets) ⇒ Object



59
60
61
62
63
# File 'app/seeds/workarea/help_seeds.rb', line 59

def render_markdown_with_assets(raw_path, assets)
  template = IO.read(raw_path)
  context = OpenStruct.new(assets).instance_eval { binding }
  ERB.new(template).result(context)
end