Sha256: 842e400461ad75cdd7260cc76fed18f1803467e3062936a1f4c607e97283f62e

Contents?: true

Size: 1.8 KB

Versions: 62

Compression:

Stored size: 1.8 KB

Contents

module Workarea
  class DynamicContentSeeds
    CONTENT_CLASS_MAP = {
      'categories' => Catalog::Category,
      'products' => Catalog::Product,
      'pages' => Content::Page
    }

    def perform
      puts 'Adding dynamic content...'

      if File.directory?("#{Rails.root}/data/content")
        CONTENT_CLASS_MAP.each do |dir, klass|
          if File.directory?("#{Rails.root}/data/content/#{dir}")
            Dir["#{Rails.root}/data/content/#{dir}/*.json"].each do |file|
              slug, blocks = content_blocks_from_file(file)
              model = begin
                        klass.find_by(slug: slug)
                      rescue Mongoid::Errors::DocumentNotFound
                        klass.create!(name: slug.titleize)
                      end

              set_content(model, blocks)
            end
          end
        end

        Dir["#{Rails.root}/data/content/*.json"].each do |file|
          slug, blocks = content_blocks_from_file(file)
          set_content(slug.underscore, blocks)
        end
      end
    end

    def render_with_seed_images(file)
      template = IO.read(file)
      context = OpenStruct.new(seed_images).instance_eval { binding }
      ERB.new(template).result(context)
    end

    def content_blocks_from_file(path)
      slug = path.split('/').last.split('.').first.dasherize
      blocks = JSON.parse(render_with_seed_images(path))
      return slug, blocks
    end

    def set_content(contentable, blocks)
      content = Content.for(contentable)
      content.update_attributes!(blocks: blocks)
    end

    def seed_images
      @seed_images ||= Content::Asset.all.inject({}) do |hash, asset|
        hash.merge(strip_extension(asset.file_name) => asset.url)
      end
    end

    def strip_extension(file)
      File.basename(file, File.extname(file))
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

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