mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-28 01:53:25 +00:00
Pull product listing fee calculations out of serializer loop
This commit is contained in:
@@ -12,13 +12,17 @@ class ShopController < BaseController
|
||||
def products
|
||||
if @products = products_for_shop
|
||||
|
||||
enterprise_fee_calculator = OpenFoodNetwork::EnterpriseFeeCalculator.new current_distributor, current_order_cycle
|
||||
|
||||
render status: 200,
|
||||
json: ActiveModel::ArraySerializer.new(@products,
|
||||
each_serializer: Api::ProductSerializer,
|
||||
current_order_cycle: current_order_cycle,
|
||||
current_distributor: current_distributor,
|
||||
variants: variants_for_shop_by_id,
|
||||
master_variants: master_variants_for_shop_by_id).to_json
|
||||
master_variants: master_variants_for_shop_by_id,
|
||||
enterprise_fee_calculator: enterprise_fee_calculator,
|
||||
).to_json
|
||||
|
||||
else
|
||||
render json: "", status: 404
|
||||
|
||||
@@ -35,13 +35,14 @@ class Api::CachedProductSerializer < ActiveModel::Serializer
|
||||
attributes :properties_with_values
|
||||
|
||||
has_many :variants, serializer: Api::VariantSerializer
|
||||
has_many :taxons, serializer: Api::IdSerializer
|
||||
has_many :images, serializer: Api::ImageSerializer
|
||||
|
||||
has_one :supplier, serializer: Api::IdSerializer
|
||||
has_one :primary_taxon, serializer: Api::TaxonSerializer
|
||||
has_one :master, serializer: Api::VariantSerializer
|
||||
|
||||
has_one :primary_taxon, serializer: Api::TaxonSerializer
|
||||
has_many :taxons, serializer: Api::IdSerializer
|
||||
|
||||
has_many :images, serializer: Api::ImageSerializer
|
||||
has_one :supplier, serializer: Api::IdSerializer
|
||||
|
||||
def properties_with_values
|
||||
object.properties_including_inherited
|
||||
end
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
class Api::VariantSerializer < ActiveModel::Serializer
|
||||
attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display,
|
||||
attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display,
|
||||
:options_text, :on_demand, :price, :fees, :price_with_fees, :product_name
|
||||
|
||||
def price_with_fees
|
||||
object.price_with_fees(options[:current_distributor], options[:current_order_cycle])
|
||||
end
|
||||
|
||||
def price
|
||||
object.price
|
||||
end
|
||||
|
||||
def fees
|
||||
object.fees_by_type_for(options[:current_distributor], options[:current_order_cycle])
|
||||
options[:enterprise_fee_calculator].indexed_fees_by_type_for(object)
|
||||
end
|
||||
|
||||
def price_with_fees
|
||||
object.price + options[:enterprise_fee_calculator].indexed_fees_for(object)
|
||||
end
|
||||
|
||||
def product_name
|
||||
object.product.name
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user