Class: Workarea::DynamicContentSeeds
- Inherits:
-
Object
- Object
- Workarea::DynamicContentSeeds
- Defined in:
- app/seeds/workarea/dynamic_content_seeds.rb
Constant Summary collapse
- CONTENT_CLASS_MAP =
{ 'categories' => Catalog::Category, 'products' => Catalog::Product, 'pages' => Content::Page }
Instance Method Summary collapse
- #content_blocks_from_file(path) ⇒ Object
- #perform ⇒ Object
- #render_with_seed_images(file) ⇒ Object
- #seed_images ⇒ Object
- #set_content(contentable, blocks) ⇒ Object
- #strip_extension(file) ⇒ Object
Instance Method Details
#content_blocks_from_file(path) ⇒ Object
41 42 43 44 45 |
# File 'app/seeds/workarea/dynamic_content_seeds.rb', line 41 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 |
#perform ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/seeds/workarea/dynamic_content_seeds.rb', line 9 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 |
#render_with_seed_images(file) ⇒ Object
35 36 37 38 39 |
# File 'app/seeds/workarea/dynamic_content_seeds.rb', line 35 def render_with_seed_images(file) template = IO.read(file) context = OpenStruct.new(seed_images).instance_eval { binding } ERB.new(template).result(context) end |
#seed_images ⇒ Object
52 53 54 55 56 |
# File 'app/seeds/workarea/dynamic_content_seeds.rb', line 52 def seed_images @seed_images ||= Content::Asset.all.inject({}) do |hash, asset| hash.merge(strip_extension(asset.file_name) => asset.url) end end |
#set_content(contentable, blocks) ⇒ Object
47 48 49 50 |
# File 'app/seeds/workarea/dynamic_content_seeds.rb', line 47 def set_content(contentable, blocks) content = Content.for(contentable) content.update_attributes!(blocks: blocks) end |
#strip_extension(file) ⇒ Object
58 59 60 |
# File 'app/seeds/workarea/dynamic_content_seeds.rb', line 58 def strip_extension(file) File.basename(file, File.extname(file)) end |