Sha256: ec1cacbc2777f1b03a3778b7c17339ed24abb0643872c980d313856363526594

Contents?: true

Size: 1.39 KB

Versions: 51

Compression:

Stored size: 1.39 KB

Contents

class MergeBehaviorsAndPages < ActiveRecord::Migration
  class OldPage < ActiveRecord::Base
    set_table_name 'pages'
  end
  
  @@page_map = {
    "Page Missing" => "FileNotFoundPage"
  }
  
  @@behavior_map = @@page_map.invert
  
  def self.up
    announce "converting behavior names to class names"
    OldPage.find(:all).each do |page|
      unless page.behavior_id.blank?
        page.behavior_id = page_name(page.behavior_id)
        page.save!
      end
    end
    rename_column :pages, :behavior_id, :type
  end

  def self.down
    rename_column :pages, :type, :behavior_id
    OldPage.reset_column_information
    announce "converting class names back to behavior names"
    OldPage.find(:all).each do |page|
      unless page.behavior_id.blank?
        page.behavior_id = behavior_name(page.behavior_id)
        page.save!
      end
    end
  end
  
  def self.page_name(behavior_name)
    if @@page_map.has_key?(behavior_name)
      @@page_map[behavior_name]
    else
      name = behavior_name.scan(/\w+/).map { |word| word.capitalize }.join
      name = $1 if name =~ /^(.*?)Behavior$/
      name += "Page" unless name =~ /Page$/
      name
    end
  end
  
  def self.behavior_name(page_name)
    if @@behavior_map.has_key?(page_name)
      @@behavior_map[page_name]
    else
      name = page_name.gsub(/(^.|[A-Z])/, ' \1').strip
      name = $1 if name =~ /^(.*)Page$/
      name
    end
  end
  

end

Version data entries

51 entries across 51 versions & 6 rubygems

Version Path
trusty-cms-1.0.0 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.4 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.3 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.2 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.0 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.0.rc1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.0.beta db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.1.0.alpha db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.0 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.0.rc5 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.0.rc4 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.0.rc3 db/migrate/010_merge_behaviors_and_pages.rb
kajam-1.0.3.rc2 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.0.rc2 db/migrate/010_merge_behaviors_and_pages.rb
radiant-1.0.0.rc1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-rails3-0.1 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.2.4 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.2.2 db/migrate/010_merge_behaviors_and_pages.rb