Commit 13061ed95f3c5eee110eca50eb09ed4fb89efc41

Authored by Daniela Feitosa
1 parent 7c0c84cf

Fixed migration to change type instead of destroying it

Also, added new schema

(ActionItem2007)
db/migrate/20110520150544_remove_categories_with_invalid_type.rb
@@ -2,14 +2,17 @@ class RemoveCategoriesWithInvalidType < ActiveRecord::Migration @@ -2,14 +2,17 @@ class RemoveCategoriesWithInvalidType < ActiveRecord::Migration
2 2
3 def self.remove_invalid(category) 3 def self.remove_invalid(category)
4 if category.class != ProductCategory && !category.class.ancestors.include?(ProductCategory) 4 if category.class != ProductCategory && !category.class.ancestors.include?(ProductCategory)
5 - category.destroy 5 + execute("update categories set type='ProductCategory' where id=#{category.id}")
6 else 6 else
7 category.children.map { |child| remove_invalid(child) } 7 category.children.map { |child| remove_invalid(child) }
8 end 8 end
9 end 9 end
10 10
11 def self.up 11 def self.up
12 - ProductCategory.all.map { |category| remove_invalid(category)} 12 + select_all("SELECT id from categories WHERE type = 'ProductCategory'").each do |product_category|
  13 + category = ProductCategory.find(product_category['id'])
  14 + remove_invalid(category)
  15 + end
13 end 16 end
14 17
15 def self.down 18 def self.down
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 # 9 #
10 # It's strongly recommended to check this file into your version control system. 10 # It's strongly recommended to check this file into your version control system.
11 11
12 -ActiveRecord::Schema.define(:version => 20110316171323) do 12 +ActiveRecord::Schema.define(:version => 20110520150544) do
13 13
14 create_table "action_tracker", :force => true do |t| 14 create_table "action_tracker", :force => true do |t|
15 t.integer "user_id" 15 t.integer "user_id"
test/functional/categories_controller_test.rb
@@ -170,7 +170,7 @@ class CategoriesControllerTest < Test::Unit::TestCase @@ -170,7 +170,7 @@ class CategoriesControllerTest < Test::Unit::TestCase
170 should 'use parent\'s type to determine subcategory\'s type' do 170 should 'use parent\'s type to determine subcategory\'s type' do
171 parent = ProductCategory.create!(:name => 'Sample category', :environment => Environment.default) 171 parent = ProductCategory.create!(:name => 'Sample category', :environment => Environment.default)
172 post :new, :parent_id => parent.id, :parent_type => parent.class.name, :category => {:name => 'Subcategory'} 172 post :new, :parent_id => parent.id, :parent_type => parent.class.name, :category => {:name => 'Subcategory'}
173 - sub = Category.last 173 + sub = ProductCategory.find_by_name('Subcategory')
174 assert_equal parent.class, sub.class 174 assert_equal parent.class, sub.class
175 end 175 end
176 176