Sha256: c8c1e5cc00dd2004f44b5664e65732aaad090bdffcff9b1bc37715dda8f82a76

Contents?: true

Size: 1.99 KB

Versions: 62

Compression:

Stored size: 1.99 KB

Contents

module Workarea
  class HelpSeeds
    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

    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

    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

    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

    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
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.26 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.45 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.25 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.23 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.44 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.22 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.43 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.21 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.42 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.20 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.41 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.19 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.40 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.18 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.39 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.17 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.38 app/seeds/workarea/help_seeds.rb
workarea-core-3.5.16 app/seeds/workarea/help_seeds.rb
workarea-core-3.4.37 app/seeds/workarea/help_seeds.rb