From 8f71b56c263f7d8a833d44e696eed11a5240f439 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Wed, 30 Nov 2016 09:37:40 +1100 Subject: [PATCH] Using same serializer for standing order index and edit pages --- .../products_panel_controller.js.coffee | 2 +- .../standing_orders_controller.js.coffee | 6 +++-- .../standing_order_prototype.js.coffee | 2 +- .../admin/standing_orders_controller.rb | 8 ++++-- .../admin/index_standing_order_serializer.rb | 26 ------------------- .../api/admin/standing_order_serializer.rb | 13 ++++++++++ .../admin/standing_orders/_table.html.haml | 12 ++++----- .../standing_orders_controller_spec.js.coffee | 2 +- 8 files changed, 32 insertions(+), 39 deletions(-) delete mode 100644 app/serializers/api/admin/index_standing_order_serializer.rb diff --git a/app/assets/javascripts/admin/standing_orders/controllers/products_panel_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/products_panel_controller.js.coffee index d3ae0c1098..3e8f0f8ca3 100644 --- a/app/assets/javascripts/admin/standing_orders/controllers/products_panel_controller.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/controllers/products_panel_controller.js.coffee @@ -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 diff --git a/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee b/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee index abcc43839e..7aabe81f73 100644 --- a/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/controllers/standing_orders_controller.js.coffee @@ -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) -> diff --git a/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee b/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee index a901fff869..f8c45b991b 100644 --- a/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee +++ b/app/assets/javascripts/admin/standing_orders/services/standing_order_prototype.js.coffee @@ -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 }) diff --git a/app/controllers/admin/standing_orders_controller.rb b/app/controllers/admin/standing_orders_controller.rb index 2930682664..ce5560d7e7 100644 --- a/app/controllers/admin/standing_orders_controller.rb +++ b/app/controllers/admin/standing_orders_controller.rb @@ -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 diff --git a/app/serializers/api/admin/index_standing_order_serializer.rb b/app/serializers/api/admin/index_standing_order_serializer.rb deleted file mode 100644 index 943f7861d0..0000000000 --- a/app/serializers/api/admin/index_standing_order_serializer.rb +++ /dev/null @@ -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 diff --git a/app/serializers/api/admin/standing_order_serializer.rb b/app/serializers/api/admin/standing_order_serializer.rb index 0ec722baaf..c94b8c1712 100644 --- a/app/serializers/api/admin/standing_order_serializer.rb +++ b/app/serializers/api/admin/standing_order_serializer.rb @@ -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 diff --git a/app/views/admin/standing_orders/_table.html.haml b/app/views/admin/standing_orders/_table.html.haml index 3074d63952..0c21995fff 100644 --- a/app/views/admin/standing_orders/_table.html.haml +++ b/app/views/admin/standing_orders/_table.html.haml @@ -36,14 +36,14 @@   %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?" } } diff --git a/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee b/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee index a9f2a7ec32..18d15c6b58 100644 --- a/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee +++ b/spec/javascripts/unit/admin/standing_orders/controllers/standing_orders_controller_spec.js.coffee @@ -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()