Module: ComfyPress::Fixtures

Defined in:
lib/comfypress/fixtures.rb

Class Method Summary collapse

Class Method Details

.export_all(from_site, to_folder = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/comfypress/fixtures.rb', line 11

def self.export_all(from_site, to_folder = nil)
  export_site        from_site, to_folder
  export_layouts     from_site, to_folder
  export_pages       from_site, to_folder
  export_snippets    from_site, to_folder
  export_menus       from_site, to_folder
  export_menu_items  from_site, to_folder
  export_tabs        from_site, to_folder
  export_carousel    from_site, to_folder
  export_files       from_site, to_folder
  save_files         from_site, to_folder
end


448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/comfypress/fixtures.rb', line 448

def self.export_carousel(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'carousel')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  site.slides.each do |slide|
    FileUtils.mkdir_p(slide_path = File.join(path, slide.id.to_s))
    open(File.join(slide_path, "_#{slide.id}.yml"), 'w') do |f|
      f.write({
        'file_path' => slide.file.to_s,
        'position' => slide.position
      }.to_yaml)
    end
  end
end

.export_files(from_site, to_folder = nil) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/comfypress/fixtures.rb', line 465

def self.export_files(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'files')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  site.files.each do |file|
    FileUtils.mkdir_p(file_path = File.join(path, file.id.to_s))
    open(File.join(file_path, "_#{file.id}.yml"), 'w') do |f|
      f.write({
        'label' => file.label,
        'description' => file.description,
        'file_path' => file.file.to_s,
        'position' => file.position
      }.to_yaml)
    end
  end
end

.export_layouts(from_site, to_folder = nil) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/comfypress/fixtures.rb', line 299

def self.export_layouts(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'layouts')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)
  
  site.layouts.each do |layout|
    layout_path = File.join(path, layout.ancestors.reverse.collect{|l| l.identifier}, layout.identifier)
    FileUtils.mkdir_p(layout_path)
    
    open(File.join(layout_path, "_#{layout.identifier}.yml"), 'w') do |f|
      f.write({
        'label'       => layout.label,
        'app_layout'  => layout.app_layout,
        'parent'      => layout.parent.try(:identifier),
        'position'    => layout.position
      }.to_yaml)
    end
    open(File.join(layout_path, 'content.html'), 'w') do |f|
      f.write(layout.content)
    end
    open(File.join(layout_path, 'css.css'), 'w') do |f|
      f.write(layout.css)
    end
    open(File.join(layout_path, 'js.js'), 'w') do |f|
      f.write(layout.js)
    end
  end
end

.export_menu_items(from_site, to_folder = nil) ⇒ Object



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/comfypress/fixtures.rb', line 390

def self.export_menu_items(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'menu_items')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  site.menu_items.each do |menu_item|
    FileUtils.mkdir_p(menu_item_path = File.join(path, menu_item.id.to_s))
    open(File.join(menu_item_path, "_#{menu_item.id}.yml"), 'w') do |f|
      f.write({
        'label' => menu_item.label,
        'page' => menu_item.page_id,
        'link' => menu_item.link,
        'menu_item_type' => menu_item.menu_item_type,
        'menu' => menu_item.menu.identifier,
        'position' => menu_item.position
      }.to_yaml)
    end
  end
end

.export_menus(from_site, to_folder = nil) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/comfypress/fixtures.rb', line 376

def self.export_menus(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'menus')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  site.menus.each do |menu|
    FileUtils.mkdir_p(menu_path = File.join(path, menu.identifier))
    open(File.join(menu_path, "_#{menu.identifier}.yml"), 'w') do |f|
      f.write({'label' => menu.label}.to_yaml)
    end
  end
end

.export_pages(from_site, to_folder = nil) ⇒ Object



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/comfypress/fixtures.rb', line 329

def self.export_pages(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'pages')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)
  
  site.pages.each do |page|
    page.slug = 'index' if page.slug.blank?
    page_path = File.join(path, page.ancestors.reverse.collect{|p| p.slug.blank?? 'index' : p.slug}, page.slug)
    FileUtils.mkdir_p(page_path)
    
    open(File.join(page_path, "_#{page.slug}.yml"), 'w') do |f|
      f.write({
        'label'         => page.label,
        'layout'        => page.layout.try(:identifier),
        'parent'        => page.parent && (page.parent.slug.present?? page.parent.slug : 'index'),
        'target_page'   => page.target_page.try(:slug),
        'is_published'  => page.is_published,
        'position'      => page.position,
        'menu'          => Cms::Menu.where(id: page.menu_id).first.try(:identifier)
      }.to_yaml)
    end
    page.blocks_attributes.each do |block|
      open(File.join(page_path, "#{block[:identifier]}.html"), 'w') do |f|
        f.write(block[:content])
      end
    end
  end
end

.export_site(from_site, to_folder = nil) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/comfypress/fixtures.rb', line 433

def self.export_site(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
   = Cms::File.where(site.logo_id).first
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'site')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  open(File.join(path, "_#{site.identifier}.yml"), 'w') do |f|
    f.write({
      'logo' => .file_file_name,
      'menu' => Cms::Menu.where(id: site.menu_id).first.try(:identifier)
    }.to_yaml)
  end
end

.export_snippets(from_site, to_folder = nil) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/comfypress/fixtures.rb', line 359

def self.export_snippets(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'snippets')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)
  
  site.snippets.each do |snippet|
    FileUtils.mkdir_p(snippet_path = File.join(path, snippet.identifier))
    open(File.join(snippet_path, "_#{snippet.identifier}.yml"), 'w') do |f|
      f.write({'label' => snippet.label}.to_yaml)
    end
    open(File.join(snippet_path, 'content.html'), 'w') do |f|
      f.write(snippet.content)
    end
  end
end

.export_tabs(from_site, to_folder = nil) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/comfypress/fixtures.rb', line 411

def self.export_tabs(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'tabs')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  site.tabs.each do |tab|
    FileUtils.mkdir_p(tab_path = File.join(path, tab.id.to_s))
    open(File.join(tab_path, "_#{tab.id}.yml"), 'w') do |f|
      f.write({
        'label' => tab.label,
        'page' => tab.page.parent && (tab.page.slug.present?? tab.page.slug : 'index'),
        'position' => tab.position,
        'is_active' => tab.is_active
      }.to_yaml)
      open(File.join(tab_path, 'content.html'), 'w') do |f|
        f.write(tab.content)
      end
    end
  end
end

.import_all(to_site, from_folder = nil, force_import = false) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/comfypress/fixtures.rb', line 3

def self.import_all(to_site, from_folder = nil, force_import = false)
  import_layouts  to_site, from_folder, nil, true, nil, [], force_import
  import_pages    to_site, from_folder, nil, true, nil, [], force_import
  import_snippets to_site, from_folder, force_import
  import_menus to_site, from_folder, force_import
  # import_menu_items to_site, from_folder, force_import
end

.import_layouts(to_site, from_folder = nil, path = nil, root = true, parent = nil, layout_ids = [], force_import = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/comfypress/fixtures.rb', line 24

def self.import_layouts(to_site, from_folder = nil, path = nil, root = true, parent = nil, layout_ids = [], force_import = false)
  site = Cms::Site.find_or_create_by_identifier(to_site)
  unless path ||= find_fixtures_path((from_folder || to_site), 'layouts')
    ComfyPress.logger.warn('Cannot find Layout fixtures')
    return []
  end
  
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
    identifier = path.split('/').last
    layout = site.layouts.find_by_identifier(identifier) || site.layouts.new(:identifier => identifier)
    
    # updating attributes
    if File.exists?(file_path = File.join(path, "_#{identifier}.yml"))
      if layout.new_record? || File.mtime(file_path) > layout.updated_at || force_import
        attributes = YAML.load_file(file_path).try(:symbolize_keys!) || { }
        layout.label      = attributes[:label] || identifier.titleize
        layout.app_layout = attributes[:app_layout] || parent.try(:app_layout)
        layout.position   = attributes[:position] if attributes[:position]
      end
    elsif layout.new_record?
      layout.label      = identifier.titleize
      layout.app_layout = parent.try(:app_layout) 
    end
    
    # updating content
    if File.exists?(file_path = File.join(path, 'content.html'))
      if layout.new_record? || File.mtime(file_path) > layout.updated_at || force_import
        layout.content = File.open(file_path).read
      end
    end
    if File.exists?(file_path = File.join(path, 'css.css'))
      if layout.new_record? || File.mtime(file_path) > layout.updated_at || force_import
        layout.css = File.open(file_path).read
      end
    end
    if File.exists?(file_path = File.join(path, 'js.js'))
      if layout.new_record? || File.mtime(file_path) > layout.updated_at || force_import
        layout.js = File.open(file_path).read
      end
    end
    
    # saving
    layout.parent = parent
    if layout.changed?
      if layout.save
        ComfyPress.logger.warn("[Fixtures] Saved Layout {#{layout.identifier}}")
      else
        ComfyPress.logger.error("[Fixtures] Failed to save Layout {#{layout.errors.inspect}}")
        next
      end
    end
    layout_ids << layout.id
    
    # checking for nested fixtures
    layout_ids += import_layouts(to_site, from_folder, path, false, layout, layout_ids)
  end
  
  # removing all db entries that are not in fixtures
  if root
    site.layouts.where('id NOT IN (?)', layout_ids.uniq).each{ |l| l.destroy } 
    ComfyPress.logger.warn('Imported Layouts!')
  end
  
  # returning ids of layouts in fixtures
  layout_ids.uniq
end

.import_menu_items(to_site, from_folder = nil, force_import = false) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/comfypress/fixtures.rb', line 254

def self.import_menu_items(to_site, from_folder = nil, force_import = false)
  site = Cms::Site.find_or_create_by_identifier(to_site)
  unless path = find_fixtures_path((from_folder || to_site), 'menu_items')
    ComfyPress.logger.warn('Cannot find Menu Items fixtures')
    return []
  end
  
  menu_item_ids = []
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
    id = path.split('/').last
    menu_item = site.menu_items.new
    
    # updating attributes
    if File.exists?(file_path = File.join(path, "_#{id}.yml"))
      if menu_item.new_record? || File.mtime(file_path) > menu_item.updated_at || force_import
        attributes = YAML.load_file(file_path).try(:symbolize_keys!) || { }
        menu_item.label = attributes[:label] || id.titleize
        menu_item.menu_item_type = attributes[:menu_item_type] || 'link'
        menu_item.position = attributes[:position] || 0
        menu_item.link = attributes[:link] || ''
        menu_item.page_id = site.pages.where(full_path: attributes[:link]).first || nil
        binding.pry
        menu_item.menu_id = site.menus.where(identifier: attributes[:menu]).first || nil
      end
    elsif menu_item.new_record?
      menu_item.label = id.titleize
    end
    
    # saving
    if menu_item.changed?
      if menu_item.save
        ComfyPress.logger.warn("[Fixtures] Saved Menu Item {#{menu_item.label}}")
      else
        ComfyPress.logger.warn("[Fixtures] Failed to save Menu Item {#{menu_item.errors.inspect}}")
        next
      end
    end
    menu_item_ids << menu_item.id
  end
  # binding.pry
  # removing all db entries that are not in fixtures
  site.menu_items.where('id NOT IN (?)', menu_item_ids).each{ |s| s.destroy }
  ComfyPress.logger.warn('Imported Menu Items!')
end

.import_menus(to_site, from_folder = nil, force_import = false) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/comfypress/fixtures.rb', line 215

def self.import_menus(to_site, from_folder = nil, force_import = false)
  site = Cms::Site.find_or_create_by_identifier(to_site)
  unless path = find_fixtures_path((from_folder || to_site), 'menus')
    ComfyPress.logger.warn('Cannot find Menu fixtures')
    return []
  end
  
  menu_ids = []
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
    identifier = path.split('/').last
    menu = site.menus.find_by_identifier(identifier) || site.menus.new(:identifier => identifier)
    
    # updating attributes
    if File.exists?(file_path = File.join(path, "_#{identifier}.yml"))
      if menu.new_record? || File.mtime(file_path) > menu.updated_at || force_import
        attributes = YAML.load_file(file_path).try(:symbolize_keys!) || { }
        menu.label = attributes[:label] || identifier.titleize
      end
    elsif menu.new_record?
      menu.label = identifier.titleize
    end
    
    # saving
    if menu.changed?
      if menu.save
        ComfyPress.logger.warn("[Fixtures] Saved Menu {#{menu.identifier}}")
      else
        ComfyPress.logger.warn("[Fixtures] Failed to save Menu {#{menu.errors.inspect}}")
        next
      end
    end
    menu_ids << menu.id
  end
  
  # removing all db entries that are not in fixtures
  site.menus.where('id NOT IN (?)', menu_ids).each{ |s| s.destroy }
  ComfyPress.logger.warn('Imported Menus!')
end

.import_pages(to_site, from_folder = nil, path = nil, root = true, parent = nil, page_ids = [], force_import = false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/comfypress/fixtures.rb', line 91

def self.import_pages(to_site, from_folder = nil, path = nil, root = true, parent = nil, page_ids = [], force_import = false)
  site = Cms::Site.find_or_create_by_identifier(to_site)
  unless path ||= find_fixtures_path((from_folder || to_site), 'pages')
    ComfyPress.logger.warn('Cannot find Page fixtures')
    return []
  end
  
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
    slug = path.split('/').last
    page = if parent
      parent.children.find_by_slug(slug) || site.pages.new(:parent => parent, :slug => slug)
    else
      site.pages.root || site.pages.new(:slug => slug)
    end
    
    # updating attributes
    if File.exists?(file_path = File.join(path, "_#{slug}.yml"))
      if page.new_record? || File.mtime(file_path) > page.updated_at || force_import
        attributes = YAML.load_file(file_path).try(:symbolize_keys!) || { }
        page.label = attributes[:label] || slug.titleize
        page.layout = site.layouts.find_by_identifier(attributes[:layout]) || parent.try(:layout)
        page.target_page = site.pages.find_by_full_path(attributes[:target_page])
        page.is_published = attributes[:is_published].nil?? true : attributes[:is_published]
        page.position = attributes[:position] if attributes[:position]
      end
    elsif page.new_record?
      page.label = slug.titleize
      page.layout = parent.try(:layout)
    end
    
    # updating content
    blocks_to_clear = page.blocks.collect(&:identifier)
    blocks_attributes = [ ]
    Dir.glob("#{path}/*.html").each do |file_path|
      identifier = file_path.split('/').last.gsub(/\.html$/, '')
      blocks_to_clear.delete(identifier)
      if page.new_record? || File.mtime(file_path) > page.updated_at || force_import
        blocks_attributes << {
          :identifier => identifier,
          :content    => File.open(file_path).read
        }
      end
    end
    
    # clearing removed blocks
    blocks_to_clear.each do |identifier|
      blocks_attributes << {
        :identifier => identifier,
        :content    => nil
      }
    end
    
    # saving
    page.blocks_attributes = blocks_attributes if blocks_attributes.present?
    if page.changed? || blocks_attributes.present?
      if page.save
        ComfyPress.logger.warn("[Fixtures] Saved Page {#{page.full_path}}")
      else
        ComfyPress.logger.warn("[Fixtures] Failed to save Page {#{page.errors.inspect}}")
        next
      end
    end
    page_ids << page.id
    
    # checking for nested fixtures
    page_ids += import_pages(to_site, from_folder, path, false, page, page_ids)
  end
  
  # removing all db entries that are not in fixtures
  if root
    site.pages.where('id NOT IN (?)', page_ids.uniq).each{ |p| p.destroy }
    ComfyPress.logger.warn('Imported Pages!')
  end
  
  # returning ids of layouts in fixtures
  page_ids.uniq
end

.import_snippets(to_site, from_folder = nil, force_import = false) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/comfypress/fixtures.rb', line 169

def self.import_snippets(to_site, from_folder = nil, force_import = false)
  site = Cms::Site.find_or_create_by_identifier(to_site)
  unless path = find_fixtures_path((from_folder || to_site), 'snippets')
    ComfyPress.logger.warn('Cannot find Snippet fixtures')
    return []
  end
  
  snippet_ids = []
  Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
    identifier = path.split('/').last
    snippet = site.snippets.find_by_identifier(identifier) || site.snippets.new(:identifier => identifier)
    
    # updating attributes
    if File.exists?(file_path = File.join(path, "_#{identifier}.yml"))
      if snippet.new_record? || File.mtime(file_path) > snippet.updated_at || force_import
        attributes = YAML.load_file(file_path).try(:symbolize_keys!) || { }
        snippet.label = attributes[:label] || identifier.titleize
      end
    elsif snippet.new_record?
      snippet.label = identifier.titleize
    end
    
    # updating content
    if File.exists?(file_path = File.join(path, 'content.html'))
      if snippet.new_record? || File.mtime(file_path) > snippet.updated_at || force_import
        snippet.content = File.open(file_path).read
      end
    end
    
    # saving
    if snippet.changed?
      if snippet.save
        ComfyPress.logger.warn("[Fixtures] Saved Snippet {#{snippet.identifier}}")
      else
        ComfyPress.logger.warn("[Fixtures] Failed to save Snippet {#{snippet.errors.inspect}}")
        next
      end
    end
    snippet_ids << snippet.id
  end
  
  # removing all db entries that are not in fixtures
  site.snippets.where('id NOT IN (?)', snippet_ids).each{ |s| s.destroy }
  ComfyPress.logger.warn('Imported Snippets!')
end

.save_files(from_site, to_folder = nil) ⇒ Object



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/comfypress/fixtures.rb', line 484

def self.save_files(from_site, to_folder = nil)
  return unless site = Cms::Site.find_by_identifier(from_site)
  path = File.join(ComfyPress.config.fixtures_path, (to_folder || site.identifier), 'uploaded_files')
  FileUtils.rm_rf(path)
  FileUtils.mkdir_p(path)

  site_files = site.files.append(site.slides).flatten

  site_files.each do |file|
    full_file_path = File.join(Rails.root, 'public', file.file.to_s)
    backup_file_path = File.join(path, file.file.to_s)
    FileUtils.mkdir_p File.dirname(backup_file_path)
    FileUtils.copy_entry full_file_path, backup_file_path, verbose: true
  end
end