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
radiantcms-couchrest_model-0.2.1 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.2 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.9 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.8 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.7 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.6 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.5 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.4 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.3 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.2 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1.1 db/migrate/010_merge_behaviors_and_pages.rb
radiantcms-couchrest_model-0.1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.7.2 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.9.1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.8.2 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.9.0.rc2 db/migrate/010_merge_behaviors_and_pages.rb
radiant-rc-0.9.0 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.8.1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.6.1 db/migrate/010_merge_behaviors_and_pages.rb
radiant-0.6.2 db/migrate/010_merge_behaviors_and_pages.rb