Skip to content

Commit

Permalink
11200: coniditonally hide producer column
Browse files Browse the repository at this point in the history
  • Loading branch information
chahmedejaz committed Sep 10, 2024
1 parent 9cfcab4 commit 1580d53
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/helpers/admin/products_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion app/models/column_preference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:)
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions app/models/spree/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/products_v3/_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions lib/open_food_network/column_preference_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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 },
Expand Down Expand Up @@ -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

0 comments on commit 1580d53

Please sign in to comment.