mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Adding data migration to populate inventories of enterprises that are already using variant overrides
This commit is contained in:
@@ -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"
|
||||
|
||||
22
db/migrate/20160218235221_populate_inventories.rb
Normal file
22
db/migrate/20160218235221_populate_inventories.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user