Spiking out products serializers and caching

This commit is contained in:
Will Marshall
2014-07-10 12:46:25 +10:00
parent 8296a4131e
commit f57e8513d5
6 changed files with 52 additions and 0 deletions

View File

@@ -8,12 +8,16 @@ class ShopController < BaseController
end
def products
# Can we make this query less slow?
unless @products = current_order_cycle.andand
.valid_products_distributed_by(current_distributor).andand
.select { |p| !p.deleted? && p.has_stock_for_distribution?(current_order_cycle, current_distributor) }.andand
.sort_by {|p| p.name }
render json: "", status: 404
end
render status: 200,
json: ActiveModel::ArraySerializer.new(@products, each_serializer: Api::ProductSerializer,
current_order_cycle: current_order_cycle, current_distributor: current_distributor).to_json
end
def order_cycle

View File

@@ -0,0 +1,4 @@
class Api::MasterVariantSerializer < ActiveModel::Serializer
attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display, :count_on_hand, :on_demand
has_many :images, serializer: Api::TaxonImageSerializer
end

View File

@@ -0,0 +1,22 @@
class Api::ProductSerializer < ActiveModel::Serializer
# TODO
# Prices can't be cached? How?
cached
delegate :cache_key, to: :object
attributes :id, :name, :permalink, :count_on_hand, :on_demand, :group_buy,
:notes, :description, :price
has_many :variants, serializer: Api::VariantSerializer
has_many :taxons, serializer: Api::IdSerializer
has_many :properties, serializer: Api::PropertySerializer
has_one :supplier, serializer: Api::IdSerializer
has_one :primary_taxon, serializer: Api::TaxonSerializer
has_one :master, serializer: Api::MasterVariantSerializer
def price
object.master.price_with_fees(options[:current_distributor], options[:current_order_cycle])
end
end

View File

@@ -0,0 +1,3 @@
class Api::PropertySerializer < ActiveModel::Serializer
end

View File

@@ -0,0 +1,11 @@
class Api::TaxonImageSerializer < ActiveModel::Serializer
attributes :id, :alt, :small_url, :large_url
def small_url
object.attachment.url(:small, false)
end
def large_url
object.attachment.url(:large, false)
end
end

View File

@@ -0,0 +1,8 @@
class Api::VariantSerializer < ActiveModel::Serializer
attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display, :on_demand, :price
has_many :images, serializer: Api::TaxonImageSerializer
def price
0.0
end
end