Sha256: 8ade998dcb5c5b8cb59480cc3469ddf69ddd1537f5caf59c448be7283ea7555d

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

class AddIsMasterToVariants < ActiveRecord::Migration
  def self.up
    change_table :variants do |t|
      t.boolean "is_master", :default => false
    end
    
    Variant.class_eval do
      # temporarily disable validation so we can pull off the migration
      def check_price
      end
    end
    
    # Convert the old variant structure to the new one
    Product.all(:include => {:variants => :option_values}).each do |p|
      price = p.attributes["master_price"]
    
      master_variant = p.variants.detect{|v| v.option_values.empty?} 

      # check for price anomalies
      if master_variant && master_variant.price != price 
          warn "[migration] single variant price doesn't match product master price #{price} for v = #{master_variant.inspect}"
      end

      master_variant ||= Variant.new(:product => p)
      master_variant.update_attributes(:price => price, :is_master => true)
      p.save                          # to trigger the consistency filters
    end
  end

  def self.down
    Product.all(:include => :variants).each do |p| 
      unless p.variants.empty?
        p.master.delete
      end
    end
    change_table :variants do |t|
      t.remove "is_master"
    end
  end
end



Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree-enriquez-0.9.4 db/migrate/20090809193131_add_is_master_to_variants.rb
spree-0.9.4 db/migrate/20090809193131_add_is_master_to_variants.rb
spree-0.9.3 db/migrate/20090809193131_add_is_master_to_variants.rb
spree-0.9.2 db/migrate/20090809193131_add_is_master_to_variants.rb
spree-0.9.1 db/migrate/20090809193131_add_is_master_to_variants.rb
spree-0.9.0 db/migrate/20090809193131_add_is_master_to_variants.rb