Pull product listing fee calculations out of serializer loop

This commit is contained in:
Rohan Mitchell
2015-06-04 16:27:32 +10:00
parent 3846d16822
commit ef3155a16a
3 changed files with 17 additions and 13 deletions

View File

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

View File

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

View File

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