Changeset 129
- Timestamp:
- 09/28/06 22:22:49 (2 years ago)
- Files:
-
- branches/corex/radiant/db/migrate/setup/base.rb (modified) (1 diff)
- branches/corex/radiant/db/migrate/setup/template.rb (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/corex/radiant/db/migrate/setup/base.rb
r128 r129 19 19 20 20 def gets 21 readline.strip .downcase21 readline.strip 22 22 end 23 23 branches/corex/radiant/db/migrate/setup/template.rb
r128 r129 3 3 def run 4 4 select_template 5 feedback "Importing template data" do 6 create_records_from_template 7 end 5 create_records_from_template 8 6 end 9 7 … … 11 9 12 10 def select_template 13 templates_directory = File.join(RADIANT_ROOT, 'db', 'templates') 14 templates = Dir[File.join(templates_directory, '*.yml')] 15 templates.map! { |t| load_template(t) } 16 templates = templates.sort_by { |t| t['name'] } 11 templates = load_templates 17 12 loop do 18 templates.each_with_index do |t, i| 19 puts " #{i + 1}) #{t['name']}" 20 t['description'].each_line { |line| puts " #{line.strip}" } 21 end 13 print_selection(templates) 22 14 ask "Select a database template [1-#{templates.size}]:" 23 selection = $1.to_i if gets .strip=~ /^(\d+)$/15 selection = $1.to_i if gets =~ /^(\d+)$/ 24 16 case selection 25 17 when (1..templates.size) … … 31 23 end 32 24 end 25 26 def load_templates 27 templates_directory = File.join(RADIANT_ROOT, 'db', 'templates') 28 templates = Dir[File.join(templates_directory, '*.yml')] 29 templates.map! { |template| load_template(template) } 30 templates.sort_by { |template| template['name'] } 31 end 33 32 34 33 def load_template(template) 35 34 YAML.load_file(template) 35 end 36 37 def print_selection(templates) 38 templates.each_with_index do |template, index| 39 puts " #{index + 1}) #{template['name']}" 40 template['description'].each_line { |line| puts " #{line.strip}" } 41 end 36 42 end 37 43 … … 41 47 records.keys.each do |key| 42 48 feedback "Creating #{key.to_s.underscore.humanize}" do 43 model = Object.const_get(key.to_s.singularize)44 record_pairs = records[key].map { |name, record| [record['id'], record] }.sort { |a, b| a[0] <=> b[0] }49 model = model(key) 50 record_pairs = order_by_id(records[key]) 45 51 record_pairs.each do |id, record| 46 puts record.to_s47 52 model.new(record).save 48 53 end … … 51 56 end 52 57 end 58 59 def model(model_name) 60 Object.const_get(model_name.to_s.singularize) 61 end 62 63 def order_by_id(records) 64 records.map { |name, record| [record['id'], record] }.sort { |a, b| a[0] <=> b[0] } 65 end 53 66 54 67 end
