From 1580d539dfe3e962034ec66de70b140d30a3f776 Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Wed, 11 Sep 2024 00:56:52 +0500 Subject: [PATCH] 11200: coniditonally hide producer column --- app/helpers/admin/products_helper.rb | 4 ++++ app/models/column_preference.rb | 12 +++++++++++- app/models/spree/user.rb | 1 + app/views/admin/products_v3/_table.html.haml | 2 +- lib/open_food_network/column_preference_defaults.rb | 13 +++++++++++-- 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/helpers/admin/products_helper.rb b/app/helpers/admin/products_helper.rb index 4686e4720a1..d3b06806ba3 100644 --- a/app/helpers/admin/products_helper.rb +++ b/app/helpers/admin/products_helper.rb @@ -37,5 +37,9 @@ def products_return_to_url(url_filters) "#{admin_products_path}#{url_filters.empty? ? '' : "#?#{url_filters.to_query}"}" end + + def hide_producer_column?(producer_options) + spree_current_user.column_preferences.products.empty? && producer_options.one? + end end end diff --git a/app/models/column_preference.rb b/app/models/column_preference.rb index 3e86f364402..093d2d5f30b 100644 --- a/app/models/column_preference.rb +++ b/app/models/column_preference.rb @@ -15,10 +15,11 @@ class ColumnPreference < ApplicationRecord validates :column_name, presence: true, inclusion: { in: proc { |p| valid_columns_for(p.action_name) } } + scope :products, -> { where(action_name: 'products_v3_index') } def self.for(user, action_name) stored_preferences = where(user_id: user.id, action_name:) - default_preferences = __send__("#{action_name}_columns") + default_preferences = get_default_preferences(action_name, user) filter(default_preferences, user, action_name) default_preferences.each_with_object([]) do |(column_name, default_attributes), preferences| stored_preference = stored_preferences.find_by(column_name:) @@ -52,4 +53,13 @@ def self.filter(default_preferences, user, action_name) default_preferences.delete(:schedules) end + + def self.get_default_preferences(action_name, user) + case action_name + when 'products_v3_index' + products_v3_index_columns(user) + else + __send__("#{action_name}_columns") + end + end end diff --git a/app/models/spree/user.rb b/app/models/spree/user.rb index bef76099337..9eae2b50744 100644 --- a/app/models/spree/user.rb +++ b/app/models/spree/user.rb @@ -42,6 +42,7 @@ class User < ApplicationRecord has_many :credit_cards, dependent: :destroy has_many :report_rendering_options, class_name: "::ReportRenderingOptions", dependent: :destroy has_many :webhook_endpoints, dependent: :destroy + has_many :column_preferences, dependent: :destroy has_one :oidc_account, dependent: :destroy accepts_nested_attributes_for :enterprise_roles, allow_destroy: true diff --git a/app/views/admin/products_v3/_table.html.haml b/app/views/admin/products_v3/_table.html.haml index b45eabab72a..71e777ba13a 100644 --- a/app/views/admin/products_v3/_table.html.haml +++ b/app/views/admin/products_v3/_table.html.haml @@ -13,7 +13,7 @@ = hidden_field_tag :producer_id, @producer_id = hidden_field_tag :category_id, @category_id - %table.products{ 'data-column-preferences-target': "table" } + %table.products{ 'data-column-preferences-target': "table", class: (hide_producer_column?(producer_options) ? 'hide-producer' : '') } %colgroup -# The `min-width` property works in Chrome but not Firefox so is considered progressive enhancement. %col.col-image{ width:"44px" }= # (image size + padding) diff --git a/lib/open_food_network/column_preference_defaults.rb b/lib/open_food_network/column_preference_defaults.rb index f851efc7948..9da9ef36e9c 100644 --- a/lib/open_food_network/column_preference_defaults.rb +++ b/lib/open_food_network/column_preference_defaults.rb @@ -77,7 +77,9 @@ def products_index_columns } end - def products_v3_index_columns + def products_v3_index_columns(user) + producer_visibility = display_producer_column?(user) + I18n.with_options scope: 'admin.products_page.columns' do { image: { name: t(:image), visible: true }, @@ -87,7 +89,7 @@ def products_v3_index_columns unit_scale: { name: t(:unit_scale), visible: true }, price: { name: t(:price), visible: true }, on_hand: { name: t(:on_hand), visible: true }, - producer: { name: t(:producer), visible: true }, + producer: { name: t(:producer), visible: producer_visibility }, category: { name: t(:category), visible: true }, tax_category: { name: t(:tax_category), visible: true }, inherits_properties: { name: t(:inherits_properties), visible: true }, @@ -134,5 +136,12 @@ def subscriptions_index_columns shipping_method: { name: I18n.t("admin.shipping_method"), visible: false } } end + + def display_producer_column?(user) + producers = OpenFoodNetwork::Permissions.new(user) + .managed_product_enterprises.is_primary_producer + + producers.many? + end end end