From cb9e3b43f9cc0021d01404e84c8589c36e321e96 Mon Sep 17 00:00:00 2001 From: Rob Harrington Date: Thu, 26 May 2016 23:54:54 +1000 Subject: [PATCH] Tag attributes are auto-initialized via directive if not present on object --- .../admin/order_cycles/services/order_cycle.js.coffee | 2 +- .../utils/directives/tags_with_translation.js.coffee | 9 ++++++--- app/serializers/api/admin/shipping_method_serializer.rb | 6 +++++- spec/javascripts/unit/order_cycle_spec.js.coffee | 2 +- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/admin/order_cycles/services/order_cycle.js.coffee b/app/assets/javascripts/admin/order_cycles/services/order_cycle.js.coffee index 5ab14dfdcd..65f0f67b71 100644 --- a/app/assets/javascripts/admin/order_cycles/services/order_cycle.js.coffee +++ b/app/assets/javascripts/admin/order_cycles/services/order_cycle.js.coffee @@ -48,7 +48,7 @@ angular.module('admin.orderCycles').factory 'OrderCycle', ($resource, $window, S this.order_cycle.incoming_exchanges.push({enterprise_id: new_supplier_id, incoming: true, active: true, variants: {}, enterprise_fees: []}) addDistributor: (new_distributor_id) -> - this.order_cycle.outgoing_exchanges.push({enterprise_id: new_distributor_id, incoming: false, active: true, variants: {}, enterprise_fees: [], tags: [], tag_list: ""}) + this.order_cycle.outgoing_exchanges.push({enterprise_id: new_distributor_id, incoming: false, active: true, variants: {}, enterprise_fees: []}) removeExchange: (exchange) -> if exchange.incoming diff --git a/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee b/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee index 221ee03c76..a79bc8f87e 100644 --- a/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee +++ b/app/assets/javascripts/admin/utils/directives/tags_with_translation.js.coffee @@ -10,12 +10,15 @@ angular.module("admin.utils").directive "tagsWithTranslation", ($timeout) -> link: (scope, element, attrs) -> scope.findTags = undefined unless attrs.hasOwnProperty("findTags") + compileTagList = -> + scope.object[scope.tagListAttr] = (tag.text for tag in scope.object[scope.tagsAttr]).join(",") + $timeout -> + # Initialize properties if necessary scope.tagsAttr ||= "tags" scope.tagListAttr ||= "tag_list" - - compileTagList = -> - scope.object[scope.tagListAttr] = (tag.text for tag in scope.object[scope.tagsAttr]).join(",") + scope.object[scope.tagsAttr] ||= [] + compileTagList() scope.tagAdded = -> compileTagList() diff --git a/app/serializers/api/admin/shipping_method_serializer.rb b/app/serializers/api/admin/shipping_method_serializer.rb index 9fbb864d09..e160d97fdf 100644 --- a/app/serializers/api/admin/shipping_method_serializer.rb +++ b/app/serializers/api/admin/shipping_method_serializer.rb @@ -1,5 +1,9 @@ class Api::Admin::ShippingMethodSerializer < ActiveModel::Serializer - attributes :id, :name, :tags + attributes :id, :name, :tag_list, :tags + + def tag_list + object.tag_list.join(",") + end def tags object.tag_list.map{ |t| { text: t } } diff --git a/spec/javascripts/unit/order_cycle_spec.js.coffee b/spec/javascripts/unit/order_cycle_spec.js.coffee index df227c7960..4a924917c8 100644 --- a/spec/javascripts/unit/order_cycle_spec.js.coffee +++ b/spec/javascripts/unit/order_cycle_spec.js.coffee @@ -568,7 +568,7 @@ describe 'OrderCycle services', -> it 'adds the distributor to outgoing exchanges', -> OrderCycle.addDistributor('123') expect(OrderCycle.order_cycle.outgoing_exchanges).toEqual [ - {enterprise_id: '123', incoming: false, active: true, variants: {}, enterprise_fees: [], tags: [], tag_list: ""} + {enterprise_id: '123', incoming: false, active: true, variants: {}, enterprise_fees: []} ] describe 'removing exchanges', ->