Skip to content
Newer
Older
100644 58 lines (49 sloc) 1.39 KB
80781ac @seancribbs Import from old repository.
seancribbs authored
1 class MergeBehaviorsAndPages < ActiveRecord::Migration
2 class OldPage < ActiveRecord::Base
3 set_table_name 'pages'
4 end
5
6 @@page_map = {
7 "Page Missing" => "FileNotFoundPage"
8 }
9
10 @@behavior_map = @@page_map.invert
11
12 def self.up
13 announce "converting behavior names to class names"
14 OldPage.find(:all).each do |page|
15 unless page.behavior_id.blank?
16 page.behavior_id = page_name(page.behavior_id)
17 page.save!
18 end
19 end
20 rename_column :pages, :behavior_id, :type
21 end
22
23 def self.down
24 rename_column :pages, :type, :behavior_id
25 OldPage.reset_column_information
26 announce "converting class names back to behavior names"
27 OldPage.find(:all).each do |page|
28 unless page.behavior_id.blank?
29 page.behavior_id = behavior_name(page.behavior_id)
30 page.save!
31 end
32 end
33 end
34
35 def self.page_name(behavior_name)
36 if @@page_map.has_key?(behavior_name)
37 @@page_map[behavior_name]
38 else
39 name = behavior_name.scan(/\w+/).map { |word| word.capitalize }.join
40 name = $1 if name =~ /^(.*?)Behavior$/
41 name += "Page" unless name =~ /Page$/
42 name
43 end
44 end
45
46 def self.behavior_name(page_name)
47 if @@behavior_map.has_key?(page_name)
48 @@behavior_map[page_name]
49 else
50 name = page_name.gsub(/(^.|[A-Z])/, ' \1').strip
51 name = $1 if name =~ /^(.*)Page$/
52 name
53 end
54 end
55
56
57 end
Something went wrong with that request. Please try again.