Adding data migration to populate inventories of enterprises that are already using variant overrides

This commit is contained in:
Rob Harrington
2016-02-19 11:44:42 +11:00
parent 4de0a5c220
commit b2657ddc72
3 changed files with 24 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
class InventoryItem < ActiveRecord::Base
attr_accessible :enterprise_id, :variant_id, :visible
attr_accessible :enterprise, :enterprise_id, :variant, :variant_id, :visible
belongs_to :enterprise
belongs_to :variant, class_name: "Spree::Variant"

View File

@@ -0,0 +1,22 @@
class PopulateInventories < ActiveRecord::Migration
def up
# If hubs are actively using overrides, populate their inventories with all variants they have permission to override
# Otherwise leave their inventories empty
hubs_using_overrides = Enterprise.joins("LEFT OUTER JOIN variant_overrides ON variant_overrides.hub_id = enterprises.id")
.where("variant_overrides.id IS NOT NULL").select("DISTINCT enterprises.*")
hubs_using_overrides.each do |hub|
overridable_producers = OpenFoodNetwork::Permissions.new(hub.owner).variant_override_producers
variants = Spree::Variant.where(is_master: false, product_id: Spree::Product.not_deleted.where(supplier_id: overridable_producers))
variants.each do |variant|
InventoryItem.create(enterprise: hub, variant: variant, visible: true)
end
end
end
def down
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20160204031816) do
ActiveRecord::Schema.define(:version => 20160218235221) do
create_table "account_invoices", :force => true do |t|
t.integer "user_id", :null => false