diff --git a/app/models/inventory_item.rb b/app/models/inventory_item.rb index 05198b6dec..2648e38341 100644 --- a/app/models/inventory_item.rb +++ b/app/models/inventory_item.rb @@ -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" diff --git a/db/migrate/20160218235221_populate_inventories.rb b/db/migrate/20160218235221_populate_inventories.rb new file mode 100644 index 0000000000..2b9d1be8b6 --- /dev/null +++ b/db/migrate/20160218235221_populate_inventories.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ac02e75ff4..5e62d66530 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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