mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Extract logic from controller to renderer service
Re-using the filter_visible method for both products and variants
This commit is contained in:
@@ -28,24 +28,22 @@ module Api
|
||||
private
|
||||
|
||||
def render_variant_count
|
||||
variants_relation = Spree::Variant.
|
||||
not_master.
|
||||
where(product_id: products.select(&:id))
|
||||
|
||||
if @order_cycle.present? &&
|
||||
@order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
variants_relation = variants_relation.visible_for(@order_cycle.coordinator)
|
||||
end
|
||||
|
||||
render text: {
|
||||
count: variants_relation.count
|
||||
count: variants.count
|
||||
}.to_json
|
||||
end
|
||||
|
||||
def variants
|
||||
renderer.exchange_variants(@incoming, @enterprise)
|
||||
end
|
||||
|
||||
def products
|
||||
ExchangeProductsRenderer.
|
||||
new(@order_cycle, spree_current_user).
|
||||
exchange_products(@incoming, @enterprise)
|
||||
renderer.exchange_products(@incoming, @enterprise)
|
||||
end
|
||||
|
||||
def renderer
|
||||
@renderer ||= ExchangeProductsRenderer.
|
||||
new(@order_cycle, spree_current_user)
|
||||
end
|
||||
|
||||
def paginated_products
|
||||
|
||||
@@ -12,6 +12,14 @@ class ExchangeProductsRenderer
|
||||
end
|
||||
end
|
||||
|
||||
def exchange_variants(incoming, enterprise)
|
||||
variants_relation = Spree::Variant.
|
||||
not_master.
|
||||
where(product_id: exchange_products(incoming, enterprise).select(&:id))
|
||||
|
||||
filter_visible(variants_relation)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def products_for_incoming_exchange(enterprise)
|
||||
@@ -21,12 +29,16 @@ class ExchangeProductsRenderer
|
||||
def supplied_products(enterprises_query_matcher)
|
||||
products_relation = Spree::Product.where(supplier_id: enterprises_query_matcher)
|
||||
|
||||
filter_visible(products_relation)
|
||||
end
|
||||
|
||||
def filter_visible(relation)
|
||||
if @order_cycle.present? &&
|
||||
@order_cycle.prefers_product_selection_from_coordinator_inventory_only?
|
||||
products_relation = products_relation.visible_for(@order_cycle.coordinator)
|
||||
relation = relation.visible_for(@order_cycle.coordinator)
|
||||
end
|
||||
|
||||
products_relation
|
||||
relation
|
||||
end
|
||||
|
||||
def products_for_outgoing_exchange
|
||||
|
||||
Reference in New Issue
Block a user