mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
Merge branch 'caching' into new_cart
Conflicts: app/serializers/api/variant_serializer.rb
This commit is contained in:
4
app/serializers/api/master_variant_serializer.rb
Normal file
4
app/serializers/api/master_variant_serializer.rb
Normal 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
|
||||
41
app/serializers/api/product_serializer.rb
Normal file
41
app/serializers/api/product_serializer.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
class Api::ProductSerializer < ActiveModel::Serializer
|
||||
# TODO
|
||||
# Prices can't be cached? How?
|
||||
def serializable_hash
|
||||
cached_serializer_hash.merge uncached_serializer_hash
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def cached_serializer_hash
|
||||
Api::CachedProductSerializer.new(object, @options).serializable_hash
|
||||
end
|
||||
|
||||
def uncached_serializer_hash
|
||||
Api::UncachedProductSerializer.new(object, @options).serializable_hash
|
||||
end
|
||||
end
|
||||
|
||||
class Api::UncachedProductSerializer < ActiveModel::Serializer
|
||||
attributes :price
|
||||
|
||||
def price
|
||||
object.master.price_with_fees(options[:current_distributor], options[:current_order_cycle])
|
||||
end
|
||||
end
|
||||
|
||||
class Api::CachedProductSerializer < ActiveModel::Serializer
|
||||
cached
|
||||
delegate :cache_key, to: :object
|
||||
|
||||
attributes :id, :name, :permalink, :count_on_hand, :on_demand, :group_buy,
|
||||
:notes, :description
|
||||
|
||||
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
|
||||
end
|
||||
3
app/serializers/api/property_serializer.rb
Normal file
3
app/serializers/api/property_serializer.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class Api::PropertySerializer < ActiveModel::Serializer
|
||||
|
||||
end
|
||||
11
app/serializers/api/taxon_image_serializer.rb
Normal file
11
app/serializers/api/taxon_image_serializer.rb
Normal 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
|
||||
@@ -1,7 +1,8 @@
|
||||
class Api::VariantSerializer < ActiveModel::Serializer
|
||||
attributes :id, :is_master, :count_on_hand, :name_to_display, :on_demand,
|
||||
:price
|
||||
|
||||
attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display, :on_demand
|
||||
attributes :price
|
||||
has_many :images, serializer: Api::ImageSerializer
|
||||
|
||||
def price
|
||||
|
||||
Reference in New Issue
Block a user