From 0ca3dca675a035ba382ee21619253718b898e9dc Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 2 May 2025 13:45:42 +1000 Subject: [PATCH] Per review, extract tags options from template Now it also scope the tags to the available variant, ie the logged in user will only see only tag that was used by them. --- .../admin/products_v3_controller.rb | 28 ++++++++++++++++--- .../admin/products_v3/_content.html.haml | 6 ++-- .../admin/products_v3/_filters.html.haml | 2 +- app/views/admin/products_v3/_sort.html.haml | 2 +- app/views/admin/products_v3/index.html.haml | 3 +- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/products_v3_controller.rb b/app/controllers/admin/products_v3_controller.rb index 1e9c1813fb..7b05201c47 100644 --- a/app/controllers/admin/products_v3_controller.rb +++ b/app/controllers/admin/products_v3_controller.rb @@ -10,7 +10,8 @@ module Admin def index fetch_products - render "index", locals: { producers:, categories:, tax_category_options:, flash: } + render "index", + locals: { producers:, categories:, tax_category_options:, available_tags:, flash: } session[:products_return_to_url] = request.url end @@ -30,7 +31,9 @@ module Admin @error_counts = { saved: product_set.saved_count, invalid: product_set.invalid.count } render "index", status: :unprocessable_entity, - locals: { producers:, categories:, tax_category_options:, flash: } + locals: { + producers:, categories:, tax_category_options:, available_tags:, flash: + } end end @@ -136,9 +139,22 @@ module Admin Spree::TaxCategory.order(:name).pluck(:name, :id) end + def available_tags + variants = Spree::Variant.where( + product: OpenFoodNetwork::Permissions.new(spree_current_user) + .editable_products + .merge(product_scope) + ) + + ActsAsTaggableOn::Tag.joins(:taggings).where( + taggings: { taggable_type: "Spree::Variant", taggable_id: variants } + ).distinct.order(:name).pluck(:name) + end + def fetch_products product_query = OpenFoodNetwork::Permissions.new(spree_current_user) - .editable_products.merge(product_scope).ransack(ransack_query).result + .editable_products.merge(product_scope_with_includes).ransack(ransack_query).result + @pagy, @products = pagy(product_query.order(:name), limit: @per_page, page: @page, size: [1, 2, 2, 1]) end @@ -151,7 +167,11 @@ module Admin Spree::Product.active end - scope.includes(product_query_includes).distinct + scope.distinct + end + + def product_scope_with_includes + product_scope.includes(product_query_includes) end def ransack_query diff --git a/app/views/admin/products_v3/_content.html.haml b/app/views/admin/products_v3/_content.html.haml index dba46a7e22..118b470e17 100644 --- a/app/views/admin/products_v3/_content.html.haml +++ b/app/views/admin/products_v3/_content.html.haml @@ -10,11 +10,13 @@ producer_id:, producer_options:, category_options:, - category_id: } if display_search_filter + category_id:, + available_tags:, + tags:} if display_search_filter - if products.any? .container.results .sixteen.columns - = render partial: 'sort', locals: { pagy: pagy, search_term: search_term, producer_id: producer_id, category_id: category_id } + = render partial: 'sort', locals: { pagy:, search_term:, producer_id:, category_id:, tags: } = render partial: 'table', locals: { products:, producer_options:, category_options:, tax_category_options: } - if pagy.present? && pagy.pages > 1 = render partial: 'admin/shared/stimulus_pagination', locals: { pagy: pagy } diff --git a/app/views/admin/products_v3/_filters.html.haml b/app/views/admin/products_v3/_filters.html.haml index 1ceeaf0ae2..c2e0d5cf73 100644 --- a/app/views/admin/products_v3/_filters.html.haml +++ b/app/views/admin/products_v3/_filters.html.haml @@ -23,7 +23,7 @@ - select_tag_options = { class: "fullwidth", multiple: true , data: { controller: "tom-select", "tom-select-placeholder-value": t(".select_tag"), "tom-select-options-value": '{ "maxItems": 5 , "plugins": { "remove_button": {} , "no_active_items": {}, "checkbox_options": { "checkedClassNames": ["ts-checked"], "uncheckedClassNames": ["ts-unchecked"] } } }' } } - = select_tag :tags_name_in, options_for_select(ActsAsTaggableOn::Tag.for_context("variant_tag").distinct.order(:name).pluck(:name), @tags), select_tag_options + = select_tag :tags_name_in, options_for_select(available_tags, tags), select_tag_options .submit .search-button = button_tag t(".search"), class: "secondary icon-search relaxed", name: nil diff --git a/app/views/admin/products_v3/_sort.html.haml b/app/views/admin/products_v3/_sort.html.haml index 17ae19e202..afe2724e66 100644 --- a/app/views/admin/products_v3/_sort.html.haml +++ b/app/views/admin/products_v3/_sort.html.haml @@ -3,7 +3,7 @@ - if pagy.present? = t(".pagination.products_total_html", count: pagy.count, from: pagy.from, to: pagy.to) - - if search_term.present? || producer_id.present? || category_id.present? + - if search_term.present? || producer_id.present? || category_id.present? || tags.present? %a{ href: url_for(page: 1), class: "button disruptive", data: { 'turbo-frame': "_self", 'turbo-action': "advance" } } = t(".pagination.clear_search") diff --git a/app/views/admin/products_v3/index.html.haml b/app/views/admin/products_v3/index.html.haml index 9d2d5f5dc8..38e4ed355b 100644 --- a/app/views/admin/products_v3/index.html.haml +++ b/app/views/admin/products_v3/index.html.haml @@ -15,7 +15,8 @@ = render partial: "content", locals: { products: @products, pagy: @pagy, search_term: @search_term, producer_options: producers, producer_id: @producer_id, category_options: categories, category_id: @category_id, - tax_category_options:, flashes: flash, + tax_category_options:, available_tags:, tags: @tags, + flashes: flash, display_search_filter: (@products.any? || @search_term.present? || @category_id.present?) } - %w[product variant].each do |object_type| = render partial: 'delete_modal', locals: { object_type: }