Translating existing order-related rabl templates accross to AMS

This commit is contained in:
Rob Harrington
2015-05-20 11:45:48 +10:00
parent 7f80c02c0e
commit 823adf3272
5 changed files with 71 additions and 0 deletions

View 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

View 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

View 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

View File

@@ -0,0 +1,3 @@
class Api::Admin::UnitsProductSerializer < ActiveModel::Serializer
attributes :id, :name, :group_buy_unit_size, :variant_unit
end

View 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