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.
This commit is contained in:
Gaetan Craig-Riou
2025-05-02 13:45:42 +10:00
parent ba4b241240
commit 0ca3dca675
5 changed files with 32 additions and 9 deletions

View File

@@ -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

View File

@@ -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 }

View File

@@ -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

View File

@@ -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")

View File

@@ -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: }