cirandas.net

ref: master

db/migrate/20141121212818_move_enterprises_layout_template_to_top_left.rb


 1
 2
 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class MoveEnterprisesLayoutTemplateToTopLeft < ActiveRecord::Migration

  ThemesGlob = '{cirandas-green,tantascores-*,modernista*,template,ees,coral*,borboleta*}'

  def up
    themes = Dir.glob "#{Rails.root}/public/designs/themes/#{ThemesGlob}"
    themes = themes.map{ |t| File.basename t }

    ActiveRecord::Base.transaction do
      Enterprise.where(theme: themes, enabled: true).find_each do |enterprise|
        boxes = enterprise.boxes
        main_box = boxes.find{ |b| b.position == 1 }
        left_box = boxes.find{ |b| b.position == 2 }
        top_box = boxes.find{ |b| b.position == 3 }
        top_box.blocks.destroy_all

        links_block = main_box.blocks.find{ |b| b.is_a? LinkListBlock }
        unless links_block
          links_block = left_box.blocks.find{ |b| b.is_a? LinkListBlock }
        end

        if enterprise.theme == 'cirandas-green'
          enterprise.theme = 'ees'
          enterprise.custom_header ||= ''
          enterprise.custom_header += '<img src="/designs/themes/ees/images/paisagem.jpg"/>'
        end

        enterprise.layout_template = 'topleft'
        enterprise.save!
        unless links_block
          puts "FAILED to find LinkListBlock on #{enterprise.identifier}"
          next
        end

        links_block.remove_from_list
        links_block.box = top_box
        links_block.insert_at 0
        links_block.save!
      end
    end

  end

  def down
    say "this migration can't be reverted"
  end

end