openfoodfoundation/openfoodnetwork

View on GitHub
db/migrate/20190313142540_add_shipping_category_to_shipping_methods_and_products.rb

Summary

Maintainability
A
0 mins
Test Coverage
class AddShippingCategoryToShippingMethodsAndProducts < ActiveRecord::Migration[4.2]
  def up
    # This is different from the equivalent Spree migration
    #   Here we are creating the default shipping category even if there are already shipping categories
    default_category = Spree::ShippingCategory.create!(name: "Default")

    Spree::ShippingMethod.all.each do |method|
      method.shipping_categories << default_category if method.shipping_categories.blank?
    end

    Spree::Product.where(shipping_category_id: nil).update_all(shipping_category_id: default_category.id)
  end
end