Using same serializer for standing order index and edit pages

This commit is contained in:
Rob Harrington
2016-11-30 09:37:40 +11:00
parent e8ee1dad04
commit 8f71b56c26
8 changed files with 32 additions and 39 deletions

View File

@@ -1,3 +1,3 @@
angular.module("admin.standingOrders").controller "ProductsPanelController", ($scope) ->
$scope.standingOrder = $scope.object
$scope.distributor_id = $scope.standingOrder.shop.id
$scope.distributor_id = $scope.standingOrder.shop_id

View File

@@ -1,12 +1,14 @@
angular.module("admin.standingOrders").controller "StandingOrdersController", ($scope, StandingOrders, Columns, shops) ->
angular.module("admin.standingOrders").controller "StandingOrdersController", ($scope, StandingOrders, Columns, shops, ShippingMethods, PaymentMethods) ->
$scope.columns = Columns.columns
$scope.shops = shops
$scope.shop_id = if shops.length == 1 then shops[0].id else null
$scope.shippingMethodsByID = ShippingMethods.byID
$scope.paymentMethodsByID = PaymentMethods.byID
$scope.$watch "shop_id", ->
if $scope.shop_id?
# CurrentShop.shop = $filter('filter')($scope.shops, {id: $scope.shop_id})[0]
$scope.standingOrders = StandingOrders.index("q[shop_id_eq]": $scope.shop_id, ams_prefix: 'index')
$scope.standingOrders = StandingOrders.index("q[shop_id_eq]": $scope.shop_id)
$scope.itemCount = (standingOrder) ->
standingOrder.standing_line_items.reduce (sum, sli) ->

View File

@@ -1,7 +1,7 @@
angular.module("admin.standingOrders").factory 'StandingOrderPrototype', ($http, InfoDialog, StatusMessage) ->
errors: {}
buildItem: (item, ams_prefix) ->
buildItem: (item) ->
return false unless item.variant_id > 0
return false unless item.quantity > 0
data = angular.extend({}, item, { shop_id: @shop_id, schedule_id: @schedule_id })

View File

@@ -9,8 +9,12 @@ module Admin
respond_to :json
def index
respond_to do |format|
format.html
format.html do
@payment_methods = Spree::PaymentMethod.managed_by(spree_current_user)
@shipping_methods = Spree::ShippingMethod.managed_by(spree_current_user)
end
format.json { render_as_json @collection, ams_prefix: params[:ams_prefix] }
end
end
@@ -48,7 +52,7 @@ module Admin
def collection
if request.format.json?
permissions.editable_standing_orders.ransack(params[:q]).result
.preload([:shop,:customer,:payment_method,:shipping_method,:standing_line_items])
.preload([:shop,:customer,:schedule,:standing_line_items, :ship_address, :bill_address])
else
StandingOrder.where("1=0")
end

View File

@@ -1,26 +0,0 @@
class Api::Admin::IndexStandingOrderSerializer < ActiveModel::Serializer
attributes :id, :item_count, :begins_on, :ends_on, :edit_path, :shop_id, :schedule_id
has_one :shop, serializer: Api::Admin::IdNameSerializer
has_one :customer, serializer: Api::Admin::IdEmailSerializer # Remove IdEmailSerializer if no longer user here
has_one :schedule, serializer: Api::Admin::IdNameSerializer
has_one :payment_method, serializer: Api::Admin::IdNameSerializer
has_one :shipping_method, serializer: Api::Admin::IdNameSerializer
has_many :standing_line_items, serializer: Api::Admin::StandingLineItemSerializer
def item_count
object.standing_line_items.sum(&:quantity)
end
def begins_on
object.begins_at.strftime('%a, %b %d, %Y')
end
def ends_on
object.ends_at.andand.strftime('%a, %b %d, %Y') || I18n.t(:ongoing)
end
def edit_path
edit_admin_standing_order_path(object)
end
end

View File

@@ -1,5 +1,6 @@
class Api::Admin::StandingOrderSerializer < ActiveModel::Serializer
attributes :id, :shop_id, :customer_id, :schedule_id, :payment_method_id, :shipping_method_id, :begins_at, :ends_at
attributes :customer_email, :schedule_name, :edit_path
has_many :standing_line_items, serializer: Api::Admin::StandingLineItemSerializer
has_one :bill_address, serializer: Api::AddressSerializer
@@ -12,4 +13,16 @@ class Api::Admin::StandingOrderSerializer < ActiveModel::Serializer
def ends_at
object.ends_at.andand.strftime('%F')
end
def customer_email
object.customer.andand.email
end
def schedule_name
object.schedule.andand.name
end
def edit_path
edit_admin_standing_order_path(object)
end
end

View File

@@ -36,14 +36,14 @@
&nbsp;
%tbody.panel-ctrl{ object: 'standingOrder', ng: { repeat: "standingOrder in standingOrders | filter:query" } }
%tr.standing_order{ :id => "so_{{standingOrder.id}}", class: { even: "'even'", odd: "'odd'" } }
%td.customer.text-center{ ng: { show: 'columns.customer.visible', bind: '::standingOrder.customer.email' } }
%td.schedule.text-center{ ng: { show: 'columns.schedule.visible', bind: '::standingOrder.schedule.name' } }
%td.customer.text-center{ ng: { show: 'columns.customer.visible', bind: '::standingOrder.customer_email' } }
%td.schedule.text-center{ ng: { show: 'columns.schedule.visible', bind: '::standingOrder.schedule_name' } }
%td.items.panel-toggle.text-center{ name: 'products', ng: { show: 'columns.items.visible' } }
%h5{ ng: { bind: 'itemCount(standingOrder)' } }
%td.begins_on.text-center{ ng: { show: 'columns.begins_on.visible', bind: '::standingOrder.begins_on' } }
%td.ends_on.text-center{ ng: { show: 'columns.ends_on.visible', bind: '::standingOrder.ends_on' } }
%td.payment_method{ ng: { show: 'columns.payment_method.visible', bind: '::standingOrder.payment_method.name' } }
%td.shipping_method{ ng: { show: 'columns.shipping_method.visible', bind: '::standingOrder.shipping_method.name' } }
%td.begins_on.text-center{ ng: { show: 'columns.begins_on.visible', bind: '::standingOrder.begins_at' } }
%td.ends_on.text-center{ ng: { show: 'columns.ends_on.visible', bind: '::standingOrder.ends_at' } }
%td.payment_method{ ng: { show: 'columns.payment_method.visible', bind: '::paymentMethodsByID[standingOrder.payment_method_id].name' } }
%td.shipping_method{ ng: { show: 'columns.shipping_method.visible', bind: '::shippingMethodsByID[standingOrder.shipping_method_id].name' } }
%td.actions
%a.edit-standing-order.icon-edit.no-text{ ng: { href: '{{standingOrder.edit_path}}'} }
-# %a.delete-standing-order.icon-trash.no-text{ ng: { href: '{{standingOrder.delete_path}}'}, data: { method: 'delete', confirm: "Are you sure?" } }

View File

@@ -31,7 +31,7 @@ describe "StandingOrdersCtrl", ->
standingOrders = [standingOrder]
beforeEach ->
http.expectGET('/admin/standing_orders.json?ams_prefix=index&q%5Bshop_id_eq%5D=3').respond 200, standingOrders
http.expectGET('/admin/standing_orders.json?q%5Bshop_id_eq%5D=3').respond 200, standingOrders
scope.$apply -> scope.shop_id = 3
http.flush()