Merge pull request #13725 from rioug/13642-disable-inventory-not-using

[Variant Tags] Enable variant tag for enterprise with no inventory
This commit is contained in:
Filipe
2025-12-08 20:38:14 +00:00
committed by GitHub
12 changed files with 91 additions and 15 deletions

View File

@@ -80,7 +80,7 @@ Metrics/BlockNesting:
Exclude: Exclude:
- 'app/models/spree/payment/processing.rb' - 'app/models/spree/payment/processing.rb'
# Offense count: 48 # Offense count: 49
# Configuration parameters: CountComments, Max, CountAsOne. # Configuration parameters: CountComments, Max, CountAsOne.
Metrics/ClassLength: Metrics/ClassLength:
Exclude: Exclude:
@@ -122,6 +122,7 @@ Metrics/ClassLength:
- 'app/services/order_cycles/form_service.rb' - 'app/services/order_cycles/form_service.rb'
- 'app/services/orders/sync_service.rb' - 'app/services/orders/sync_service.rb'
- 'app/services/permissions/order.rb' - 'app/services/permissions/order.rb'
- 'app/services/products_renderer.rb'
- 'app/services/sets/product_set.rb' - 'app/services/sets/product_set.rb'
- 'engines/order_management/app/services/order_management/order/updater.rb' - 'engines/order_management/app/services/order_management/order/updater.rb'
- 'lib/open_food_network/enterprise_fee_calculator.rb' - 'lib/open_food_network/enterprise_fee_calculator.rb'

View File

@@ -49,7 +49,7 @@ module Admin
errors: @importer.errors.full_messages errors: @importer.errors.full_messages
} }
if helpers.feature?(:inventory, *spree_current_user.enterprises) if helpers.inventory_enabled?(spree_current_user.enterprises)
json[:results][:inventory_created] = @importer.inventory_created_count json[:results][:inventory_created] = @importer.inventory_created_count
json[:results][:inventory_updated] = @importer.inventory_updated_count json[:results][:inventory_updated] = @importer.inventory_updated_count
end end
@@ -175,7 +175,7 @@ module Admin
# Return an error if trying to import into inventories when inventory is disable # Return an error if trying to import into inventories when inventory is disable
def can_import_into_inventories? def can_import_into_inventories?
return true if helpers.feature?(:inventory, *spree_current_user.enterprises) || return true if helpers.inventory_enabled?(spree_current_user.enterprises) ||
params.dig(:settings, "import_into") != 'inventories' params.dig(:settings, "import_into") != 'inventories'
redirect_to admin_product_import_url, notice: I18n.t(:product_import_inventory_disable) redirect_to admin_product_import_url, notice: I18n.t(:product_import_inventory_disable)

View File

@@ -147,6 +147,10 @@ module Spree
dom_id(record, 'spree') dom_id(record, 'spree')
end end
def inventory_enabled?(enterprises)
!feature?(:variant_tag, *enterprises) && feature?(:inventory, *enterprises)
end
private private
def attribute_name_for(field_name) def attribute_name_for(field_name)

View File

@@ -185,11 +185,16 @@ module ProductImport
order('is_primary_producer ASC, name'). order('is_primary_producer ASC, name').
map { |e| @editable_enterprises[e.name] = e.id } map { |e| @editable_enterprises[e.name] = e.id }
return unless OpenFoodNetwork::FeatureToggle.enabled?(:inventory, *@current_user.enterprises) return unless inventory_enabled?
@inventory_permissions = permissions.variant_override_enterprises_per_hub @inventory_permissions = permissions.variant_override_enterprises_per_hub
end end
def inventory_enabled?
!OpenFoodNetwork::FeatureToggle.enabled?(:variant_tag, *@current_user.enterprises) &&
OpenFoodNetwork::FeatureToggle.enabled?(:inventory, *@current_user.enterprises)
end
def open_spreadsheet def open_spreadsheet
if accepted_mimetype if accepted_mimetype
Roo::Spreadsheet.open(@file, extension: accepted_mimetype, encoding: Encoding::UTF_8) Roo::Spreadsheet.open(@file, extension: accepted_mimetype, encoding: Encoding::UTF_8)

View File

@@ -44,7 +44,7 @@ class ProductsRenderer
paginated_products = paginate(results) paginated_products = paginate(results)
if options[:inventory_enabled] if inventory_enabled?
# Scope results with variant_overrides # Scope results with variant_overrides
paginated_products.each { |product| product_scoper.scope(product) } paginated_products.each { |product| product_scoper.scope(product) }
end end
@@ -123,7 +123,7 @@ class ProductsRenderer
includes(:default_price, :product). includes(:default_price, :product).
where(product_id: products) where(product_id: products)
if options[:inventory_enabled] if inventory_enabled?
# Scope results with variant_overrides # Scope results with variant_overrides
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor) scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
variants = variants.each { |v| scoper.scope(v) } variants = variants.each { |v| scoper.scope(v) }
@@ -143,4 +143,8 @@ class ProductsRenderer
vs[v.product_id] << v vs[v.product_id] << v
end end
end end
def inventory_enabled?
options[:inventory_enabled] && !options[:variant_tag_enabled]
end
end end

View File

@@ -2,7 +2,7 @@
%h3= t('.title') %h3= t('.title')
= form_for [main_app, :admin, @order_cycle] do |f| = form_for [main_app, :admin, @order_cycle] do |f|
- if feature?(:inventory, @order_cycle.coordinator) - if inventory_enabled?(@order_cycle.coordinator)
.row .row
.three.columns.alpha .three.columns.alpha
= f.label "enterprise_preferred_product_selection_from_coordinator_inventory_only", t('admin.order_cycles.edit.choose_products_from') = f.label "enterprise_preferred_product_selection_from_coordinator_inventory_only", t('admin.order_cycles.edit.choose_products_from')

View File

@@ -5,7 +5,7 @@
%h6= t('admin.product_import.index.choose_import_type') %h6= t('admin.product_import.index.choose_import_type')
%br %br
- options = { "#{t('admin.product_import.index.product_list')}" => :product_list } - options = { "#{t('admin.product_import.index.product_list')}" => :product_list }
- options = options.merge("#{t('admin.product_import.index.inventories')}" => :inventories) if feature?(:inventory, *spree_current_user.enterprises) - options = options.merge("#{t('admin.product_import.index.inventories')}" => :inventories) if inventory_enabled?(spree_current_user.enterprises)
= select_tag "settings[import_into]", = select_tag "settings[import_into]",
options_for_select(options), options_for_select(options),
{ "data-controller": "tom-select", class: "primary inline no-search", "ng-model": "settings.import_into" } { "data-controller": "tom-select", class: "primary inline no-search", "ng-model": "settings.import_into" }

View File

@@ -5,7 +5,7 @@
%i.icon-external-link %i.icon-external-link
= t('admin.product_import.index.product_list_template') = t('admin.product_import.index.product_list_template')
- if feature?(:inventory, *spree_current_user.enterprises) - if inventory_enabled?(spree_current_user.enterprises)
%a.download{href: '/inventory_template.csv'} %a.download{href: '/inventory_template.csv'}
%i.icon-external-link %i.icon-external-link
= t('admin.product_import.index.inventory_template') = t('admin.product_import.index.inventory_template')

View File

@@ -2,5 +2,5 @@
%ul#sub_nav.inline-menu %ul#sub_nav.inline-menu
= tab :products, :products_v3, url: admin_products_path = tab :products, :products_v3, url: admin_products_path
= tab :properties = tab :properties
= tab :variant_overrides, url: main_app.admin_inventory_path, match_path: '/inventory' if feature?(:inventory, *spree_current_user.enterprises) = tab :variant_overrides, url: main_app.admin_inventory_path, match_path: '/inventory' if inventory_enabled?(spree_current_user.enterprises)
= tab :import, url: main_app.admin_product_import_path, match_path: '/product_import' = tab :import, url: main_app.admin_product_import_path, match_path: '/product_import'

View File

@@ -1,3 +1,5 @@
# frozen_string_literal: true
require "flipper" require "flipper"
require "flipper/adapters/active_record" require "flipper/adapters/active_record"
require "open_food_network/feature_toggle" require "open_food_network/feature_toggle"
@@ -23,21 +25,52 @@ Flipper.register(:admins) do |actor|
actor.respond_to?(:admin?) && actor.admin? actor.respond_to?(:admin?) && actor.admin?
end end
Flipper.register(:new_2024_07_03) do |actor| Flipper.register(:new_2024_07_03) do |actor|
actor.respond_to?(:created_at?) && actor.created_at >= "2024-07-03".to_time actor.respond_to?(:created_at?) && actor.created_at >= Time.zone.parse("2024-07-03")
end end
Flipper.register(:enterprise_created_before_2025_08_11) do |actor| Flipper.register(:enterprise_created_before_2025_08_11) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise # This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise next false unless actor.actor.instance_of? Enterprise
actor.respond_to?(:created_at?) && actor.created_at < "2025-08-11".to_time actor.respond_to?(:created_at?) && actor.created_at < Time.zone.parse("2025-08-11")
end end
Flipper.register(:enterprise_created_after_2025_08_11) do |actor| Flipper.register(:enterprise_created_after_2025_08_11) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise # This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise next false unless actor.actor.instance_of? Enterprise
actor.respond_to?(:created_at?) && actor.created_at >= "2025-08-11".to_time actor.respond_to?(:created_at?) && actor.created_at >= Time.zone.parse("2025-08-11")
end end
Flipper.register(:enterprise_with_no_inventory) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise
# Uses 2025-08-11 as filter because variant tag did not exist before that, enterprise created
# after never had access to the inventory
enterprise_with_variant_override = Enterprise
.where(id: VariantOverride.joins(:hub).select(:hub_id))
.where(created_at: ..."2025-08-11")
.distinct
enterprise_with_no_variant_override = Enterprise
.where.not(id: enterprise_with_variant_override)
enterprise_with_no_variant_override.exists?(actor.id)
end
Flipper.register(:enterprise_with_inventory) do |actor|
# This group applies to enterprises only, so we return false if the actor is not an Enterprise
next false unless actor.actor.instance_of? Enterprise
# Uses 2025-08-11 as filter because variant tag did not exist before that, enterprise created
# after never had access to the inventory
enterprise_with_variant_override = Enterprise
.where(id: VariantOverride.joins(:hub).select(:hub_id))
.where(created_at: ..."2025-08-11")
.distinct
enterprise_with_variant_override.exists?(actor.id)
end
Flipper::UI.configure do |config| Flipper::UI.configure do |config|
config.descriptions_source = ->(_keys) do config.descriptions_source = ->(_keys) do
# return has to be hash of {String key => String description} # return has to be hash of {String key => String description}
@@ -59,4 +92,8 @@ Flipper::UI.configure do |config|
end end
# Add known feature toggles. This may fail if the database isn't setup yet. # Add known feature toggles. This may fail if the database isn't setup yet.
OpenFoodNetwork::FeatureToggle.setup! rescue ActiveRecord::StatementInvalid begin
OpenFoodNetwork::FeatureToggle.setup!
rescue ActiveRecord::StatementInvalid
nil
end

View File

@@ -0,0 +1,25 @@
# frozen_string_literal: true
class EnableVariantTagAndEnableInventoryForEnterpriseUsingIt < ActiveRecord::Migration[7.1]
def up
Flipper.disable_group(:variant_tag, "enterprise_created_after_2025_08_11")
Flipper.disable_group(:variant_tag, "old_enterprise_with_no_inventory")
Flipper.enable_group(:variant_tag, "enterprise_with_no_inventory")
Flipper.disable_group(:inventory, "enterprise_created_before_2025_08_11")
Flipper.enable_group(:inventory, "enterprise_with_inventory")
end
def down
Flipper.enable_group(:variant_tag, "enterprise_created_after_2025_08_11")
Flipper.enable_group(:variant_tag, "old_enterprise_with_no_inventory")
Flipper.disable_group(:variant_tag, "enterprise_with_no_inventory")
Flipper.enable_group(:inventory, "enterprise_created_before_2025_08_11")
Flipper.disable_group(:inventory, "enterprise_with_inventory")
end
end

View File

@@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2025_08_27_205335) do ActiveRecord::Schema[7.1].define(version: 2025_11_26_005628) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "pg_stat_statements" enable_extension "pg_stat_statements"
enable_extension "plpgsql" enable_extension "plpgsql"