mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-06 07:29:16 +00:00
Translating existing order-related rabl templates accross to AMS
This commit is contained in:
14
app/serializers/api/admin/basic_order_cycle_serializer.rb
Normal file
14
app/serializers/api/admin/basic_order_cycle_serializer.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
class Api::Admin::BasicOrderCycleSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :first_order, :last_order
|
||||
|
||||
has_many :suppliers, serializer: Api::Admin::IdNameSerializer
|
||||
has_many :distributors, serializer: Api::Admin::IdNameSerializer
|
||||
|
||||
def first_order
|
||||
object.orders_open_at.strftime("%F")
|
||||
end
|
||||
|
||||
def last_order
|
||||
(object.orders_close_at + 1.day).strftime("%F")
|
||||
end
|
||||
end
|
||||
15
app/serializers/api/admin/line_item_serializer.rb
Normal file
15
app/serializers/api/admin/line_item_serializer.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class Api::Admin::LineItemSerializer < ActiveModel::Serializer
|
||||
attributes :id, :quantity, :max_quantity, :supplier, :units_product, :units_variant
|
||||
|
||||
def supplier
|
||||
Api::Admin::IdNameSerializer.new(object.product.supplier).serializable_hash
|
||||
end
|
||||
|
||||
def units_product
|
||||
Api::Admin::UnitsProductSerializer.new(object.product).serializable_hash
|
||||
end
|
||||
|
||||
def units_variant
|
||||
Api::Admin::UnitsVariantSerializer.new(object.variant).serializable_hash
|
||||
end
|
||||
end
|
||||
31
app/serializers/api/admin/order_serializer.rb
Normal file
31
app/serializers/api/admin/order_serializer.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
class Api::Admin::OrderSerializer < ActiveModel::Serializer
|
||||
attributes :id, :number, :full_name, :email, :phone, :completed_at, :line_items
|
||||
|
||||
has_one :distributor, serializer: Api::Admin::IdNameSerializer
|
||||
has_one :order_cycle, serializer: Api::Admin::BasicOrderCycleSerializer
|
||||
|
||||
def full_name
|
||||
object.billing_address.nil? ? "" : ( object.billing_address.full_name || "" )
|
||||
end
|
||||
|
||||
def email
|
||||
object.email || ""
|
||||
end
|
||||
|
||||
def phone
|
||||
object.billing_address.nil? ? "a" : ( object.billing_address.phone || "" )
|
||||
end
|
||||
|
||||
def completed_at
|
||||
object.completed_at.blank? ? "" : object.completed_at.strftime("%F %T")
|
||||
end
|
||||
|
||||
def line_items
|
||||
# we used to have a scope here, but we are at the point where a user which can edit an order
|
||||
# should be able to edit all of the line_items as well, making the scope redundant
|
||||
ActiveModel::ArraySerializer.new(
|
||||
object.line_items.order('id ASC'),
|
||||
{each_serializer: Api::Admin::LineItemSerializer}
|
||||
)
|
||||
end
|
||||
end
|
||||
3
app/serializers/api/admin/units_product_serializer.rb
Normal file
3
app/serializers/api/admin/units_product_serializer.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class Api::Admin::UnitsProductSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :group_buy_unit_size, :variant_unit
|
||||
end
|
||||
8
app/serializers/api/admin/units_variant_serializer.rb
Normal file
8
app/serializers/api/admin/units_variant_serializer.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Api::Admin::UnitsVariantSerializer < ActiveModel::Serializer
|
||||
attributes :id, :unit_text, :unit_value
|
||||
|
||||
def unit_text
|
||||
options_text = object.options_text
|
||||
object.product.name + (options_text.empty? ? "" : ": #{options_text}")
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user