Extract logic from controller to renderer service

Re-using the filter_visible method for both products and variants
This commit is contained in:
luisramos0
2020-01-24 10:31:10 +00:00
parent 685abccb61
commit 4596399bc2
2 changed files with 25 additions and 15 deletions

View File

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

View File

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