Sha256: ce73f894cacb789170103289708c6cca22a2b58f24b2eee2899b0ae2de649d2b

Contents?: true

Size: 1.29 KB

Versions: 17

Compression:

Stored size: 1.29 KB

Contents

class AddCountOnHandToVariantsAndProducts < ActiveRecord::Migration
  def self.up
    add_column :variants, :count_on_hand, :integer, :default => 0, :null => false
    add_column :products, :count_on_hand, :integer, :default => 0, :null => false
    
    # In some cases needed to reflect changes in table structure
    Variant.reset_column_information
    Product.reset_column_information
    
    say_with_time 'Transfering inventory units with status on_hand to variants table...' do 
      Variant.all.each do |v|
        v.update_attribute(:count_on_hand, v.inventory_units.with_state("on_hand").size)
        InventoryUnit.destroy_all(:variant_id => v.id, :state => "on_hand")
      end
    end
    
    say_with_time 'Updating products count on hand' do
      Product.all.each do |p|
        product_count_on_hand = p.has_variants? ?
            p.variants.inject(0) {|acc, v| acc + v.count_on_hand} :
            (p.master ? p.master.count_on_hand : 0)
        p.update_attribute(:count_on_hand, product_count_on_hand)
      end
    end
  end

  def self.down
    Variant.all.each do |v|
      v.count_on_hand.times do
        InventoryUnit.create(:variant => variant, :state => 'on_hand')
      end
    end  
    remove_column :variants, :count_on_hand
    remove_column :products, :count_on_hand
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
spree-0.11.4 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree-0.11.3 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.30.2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.40.4 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_essential_press-0.1.0.pre3 test/dummy/db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_essential_press-0.1.0.pre2 test/dummy/db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.40.3 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.40.2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.40.1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.40.0 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.30.1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.30.0 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree-0.11.2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree-0.11.1 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree_core-0.30.0.beta1 lib/generators/templates/db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree-0.11.0 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb
spree-0.10.2 db/migrate/20090923100315_add_count_on_hand_to_variants_and_products.rb