Adding tags to variant overrides

This commit is contained in:
Rob Harrington
2016-04-15 11:44:19 +10:00
parent ddc7e86e6c
commit 3f8420b0e9
18 changed files with 152 additions and 63 deletions

View File

@@ -6,11 +6,20 @@ angular.module("admin.utils").directive "tagsWithTranslation", ($timeout) ->
tagsAttr: "@?"
tagListAttr: "@?"
findTags: "&"
form: '=?'
link: (scope, element, attrs) ->
$timeout ->
scope.tagsAttr ||= "tags"
scope.tagListAttr ||= "tag_list"
watchString = "object.#{scope.tagsAttr}"
scope.$watchCollection watchString, ->
compileTagList = ->
scope.object[scope.tagListAttr] = (tag.text for tag in scope.object[scope.tagsAttr]).join(",")
scope.tagAdded = ->
compileTagList()
scope.tagRemoved = ->
# For some reason the tags input doesn't mark the form
# as dirty when a tag is removed, which breaks the save bar
scope.form.$setDirty(true) if typeof scope.form isnt 'undefined'
compileTagList()

View File

@@ -6,7 +6,6 @@ angular.module("admin.variantOverrides").directive "trackInheritance", (VariantO
ngModel.$parsers.push (viewValue) ->
if ngModel.$dirty && viewValue
variantOverride = VariantOverrides.inherit(scope.hub_id, scope.variant.id)
DirtyVariantOverrides.add variantOverride
DirtyVariantOverrides.inherit scope.hub_id, scope.variant.id, scope.variantOverrides[scope.hub_id][scope.variant.id].id
scope.displayDirty()
viewValue

View File

@@ -0,0 +1,9 @@
angular.module("admin.variantOverrides").directive "trackTagList", (VariantOverrides, DirtyVariantOverrides) ->
link: (scope, element, attrs) ->
watchString = "variantOverrides[#{scope.hub_id}][#{scope.variant.id}].tag_list"
scope.$watch watchString, (newValue, oldValue) ->
if typeof newValue isnt 'undefined' && newValue != oldValue
scope.inherit = false
vo_id = scope.variantOverrides[scope.hub_id][scope.variant.id].id
DirtyVariantOverrides.set scope.hub_id, scope.variant.id, vo_id, 'tag_list', newValue
scope.displayDirty()

View File

@@ -3,8 +3,8 @@ angular.module("admin.variantOverrides").directive "ofnTrackVariantOverride", (D
link: (scope, element, attrs, ngModel) ->
ngModel.$parsers.push (viewValue) ->
if ngModel.$dirty
variantOverride = scope.variantOverrides[scope.hub_id][scope.variant.id]
scope.inherit = false
DirtyVariantOverrides.add variantOverride
vo_id = scope.variantOverrides[scope.hub_id][scope.variant.id].id
DirtyVariantOverrides.set scope.hub_id, scope.variant.id, vo_id, attrs.ofnTrackVariantOverride, viewValue
scope.displayDirty()
viewValue

View File

@@ -1,10 +1,22 @@
angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http) ->
angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http, VariantOverrides) ->
new class DirtyVariantOverrides
dirtyVariantOverrides: {}
add: (vo) ->
@dirtyVariantOverrides[vo.hub_id] ||= {}
@dirtyVariantOverrides[vo.hub_id][vo.variant_id] = vo
add: (hub_id, variant_id, vo_id) ->
@dirtyVariantOverrides[hub_id] ||= {}
@dirtyVariantOverrides[hub_id][variant_id] ||=
{ id: vo_id, variant_id: variant_id, hub_id: hub_id }
set: (hub_id, variant_id, vo_id, attr, value) ->
if attr in @requiredAttrs()
@add(hub_id, variant_id, vo_id)
@dirtyVariantOverrides[hub_id][variant_id][attr] = value
inherit: (hub_id, variant_id, vo_id) ->
@add(hub_id, variant_id, vo_id)
blankVo = angular.copy(VariantOverrides.inherit(hub_id, variant_id))
delete blankVo[attr] for attr, value of blankVo when attr not in @requiredAttrs()
@dirtyVariantOverrides[hub_id][variant_id] = blankVo
count: ->
count = 0
@@ -27,3 +39,6 @@ angular.module("admin.variantOverrides").factory "DirtyVariantOverrides", ($http
url: "/admin/variant_overrides/bulk_update"
data:
variant_overrides: @all()
requiredAttrs: ->
['id','hub_id','variant_id','sku','price','count_on_hand','on_demand','default_stock','resettable','tag_list']

View File

@@ -30,6 +30,8 @@ angular.module("admin.variantOverrides").factory "VariantOverrides", (variantOve
on_demand: null
default_stock: null
resettable: false
tag_list: ''
tags: []
updateIds: (updatedVos) ->
for vo in updatedVos

View File

@@ -1 +1 @@
angular.module("admin.variantOverrides", ["admin.indexUtils", "admin.utils", "admin.dropdown", "admin.inventoryItems"])
angular.module("admin.variantOverrides", ["admin.indexUtils", "admin.utils", "admin.dropdown", "admin.inventoryItems", 'ngTagsInput'])

View File

@@ -1,4 +1,4 @@
%tags-input{ template: 'admin/tag.html', ng: { model: 'object[tagsAttr]' } }
%tags-input{ template: 'admin/tag.html', ng: { model: 'object[tagsAttr]' }, on: { tag: { added: 'tagAdded()', removed:'tagRemoved()' } } }
%auto-complete{source: "findTags({query: $query})",
template: "admin/tag_autocomplete.html",
"min-length" => "0",

View File

@@ -1,4 +1,6 @@
class VariantOverride < ActiveRecord::Base
acts_as_taggable
belongs_to :hub, class_name: 'Enterprise'
belongs_to :variant, class_name: 'Spree::Variant'

View File

@@ -1,16 +1,28 @@
class VariantOverrideSet < ModelSet
def initialize(collection, attributes={})
super(VariantOverride, collection, attributes, nil, proc { |attrs| deletable?(attrs) } )
super(VariantOverride, collection, attributes, nil, proc { |attrs, tag_list| deletable?(attrs, tag_list) } )
end
private
def deletable?(attrs)
def deletable?(attrs, tag_list)
attrs['price'].blank? &&
attrs['count_on_hand'].blank? &&
attrs['default_stock'].blank? &&
attrs['resettable'].blank? &&
attrs['sku'].nil? &&
attrs['on_demand'].nil?
attrs['on_demand'].nil? &&
tag_list.empty?
end
def collection_to_delete
# Override of ModelSet method to allow us to check presence of a tag_list (which is not an attribute)
deleted = []
collection.delete_if { |e| deleted << e if @delete_if.andand.call(e.attributes, e.tag_list) }
deleted
end
def collection_to_keep
collection.reject { |e| @delete_if.andand.call(e.attributes, e.tag_list) }
end
end

View File

@@ -1,3 +1,12 @@
class Api::Admin::VariantOverrideSerializer < ActiveModel::Serializer
attributes :id, :hub_id, :variant_id, :sku, :price, :count_on_hand, :on_demand, :default_stock, :resettable
attributes :tag_list, :tags
def tag_list
object.tag_list.join(",")
end
def tags
object.tag_list.map{ |t| { text: t } }
end
end

View File

@@ -11,6 +11,7 @@
%col.reset{ width: "1%", ng: { show: 'columns.reset.visible' } }
%col.reset{ width: "15%", ng: { show: 'columns.reset.visible' } }
%col.inheritance{ width: "5%", ng: { show: 'columns.inheritance.visible' } }
%col.tags{ width: "30%", ng: { show: 'columns.tags.visible' } }
%col.visibility{ width: "10%", ng: { show: 'columns.visibility.visible' } }
%thead
%tr{ ng: { controller: "ColumnsCtrl" } }
@@ -22,6 +23,7 @@
%th.on_demand{ ng: { show: 'columns.on_demand.visible' } }=t('admin.on_demand?')
%th.reset{ colspan: 2, ng: { show: 'columns.reset.visible' } }=t('admin.variant_overrides.index.enable_reset?')
%th.inheritance{ ng: { show: 'columns.inheritance.visible' } }=t('admin.variant_overrides.index.inherit?')
%th.tags{ ng: { show: 'columns.tags.visible' } }=t('admin.tags')
%th.visibility{ ng: { show: 'columns.visibility.visible' } }=t('admin.variant_overrides.index.hide')
%tbody{ ng: {repeat: 'product in filteredProducts = (products | hubPermissions:hubPermissions:hub_id | inventoryProducts:hub_id:views | attrFilter:{producer_id:producerFilter} | filter:query) | limitTo:productLimit' } }
= render 'admin/variant_overrides/products_product'

View File

@@ -7,4 +7,5 @@
%td.on_demand{ ng: { show: 'columns.on_demand.visible' } }
%td.reset{ colspan: 2, ng: { show: 'columns.reset.visible' } }
%td.inheritance{ ng: { show: 'columns.inheritance.visible' } }
%td.tags{ ng: { show: 'columns.tags.visible' } }
%td.visibility{ ng: { show: 'columns.visibility.visible' } }

View File

@@ -1,4 +1,4 @@
%tr.variant{ id: "v_{{variant.id}}", ng: {repeat: 'variant in product.variants | inventoryVariants:hub_id:views'}}
%tr.variant{ id: "v_{{variant.id}}", ng: {repeat: 'variant in product.variants | inventoryVariants:hub_id:views'} }
%td.producer{ ng: { show: 'columns.producer.visible' } }
%td.product{ ng: { show: 'columns.product.visible' } }
%span{ ng: { bind: '::variant.display_name || ""'} }
@@ -17,6 +17,9 @@
%input{name: 'variant-overrides-{{ variant.id }}-default_stock', type: 'text', ng: {model: 'variantOverrides[hub_id][variant.id].default_stock'}, placeholder: '{{ variant.default_stock ? variant.default_stock : "Default stock"}}', 'ofn-track-variant-override' => 'default_stock'}
%td.inheritance{ ng: { show: 'columns.inheritance.visible' } }
%input.field{ :type => 'checkbox', name: 'variant-overrides-{{ variant.id }}-inherit', ng: { model: 'inherit' }, 'track-inheritance' => true }
%td.tags{ ng: { show: 'columns.tags.visible' } }
.tag_watcher{ 'track-tag-list' => true }
%tags_with_translation{ object: 'variantOverrides[hub_id][variant.id]', form: 'variant_overrides_form' }
%td.visibility{ ng: { show: 'columns.visibility.visible' } }
%button.icon-remove.hide.fullwidth{ :type => 'button', ng: { click: "setVisibility(hub_id,variant.id,false)" } }
= t('admin.variant_overrides.index.hide')

View File

@@ -19,6 +19,7 @@ module OpenFoodNetwork
on_demand: { name: I18n.t("admin.on_demand?"), visible: false },
reset: { name: I18n.t("#{node}.enable_reset?"), visible: false },
inheritance: { name: I18n.t("#{node}.inherit?"), visible: false },
tags: { name: I18n.t("admin.tags"), visible: false },
visibility: { name: I18n.t("#{node}.hide"), visible: false }
}
end

View File

@@ -214,7 +214,7 @@ feature %q{
end
context "with overrides" do
let!(:vo) { create(:variant_override, variant: variant, hub: hub, price: 77.77, count_on_hand: 11111, default_stock: 1000, resettable: true) }
let!(:vo) { create(:variant_override, variant: variant, hub: hub, price: 77.77, count_on_hand: 11111, default_stock: 1000, resettable: true, tag_list: ["tag1","tag2","tag3"]) }
let!(:vo_no_auth) { create(:variant_override, variant: variant, hub: hub2, price: 1, count_on_hand: 2) }
let!(:product2) { create(:simple_product, supplier: producer, variant_unit: 'weight', variant_unit_scale: 1) }
let!(:variant2) { create(:variant, product: product2, unit_value: 8, price: 1.00, on_hand: 12) }
@@ -256,6 +256,7 @@ feature %q{
first("div#columns-dropdown", :text => "COLUMNS").click
first("div#columns-dropdown div.menu div.menu_item", text: "On Demand").click
first("div#columns-dropdown div.menu div.menu_item", text: "Enable Stock Reset?").click
first("div#columns-dropdown div.menu div.menu_item", text: "Tags").click
first("div#columns-dropdown", :text => "COLUMNS").click
# Clearing values by 'inheriting'
@@ -268,6 +269,13 @@ feature %q{
fill_in "variant-overrides-#{variant.id}-price", with: ''
fill_in "variant-overrides-#{variant.id}-count_on_hand", with: ''
fill_in "variant-overrides-#{variant.id}-default_stock", with: ''
within "tr#v_#{variant.id}" do
vo.tag_list.each do |tag|
within "li.tag-item", text: "#{tag} ×" do
find("a.remove-button").trigger('click')
end
end
end
page.uncheck "variant-overrides-#{variant.id}-resettable"
page.should have_content "Changes to 2 overrides remain unsaved."

View File

@@ -8,36 +8,53 @@ describe "maintaining a list of dirty variant overrides", ->
beforeEach ->
module "admin.variantOverrides"
module ($provide) ->
$provide.value "variantOverrides", { 2: { 1: variantOverride } }
null
beforeEach inject (_DirtyVariantOverrides_) ->
DirtyVariantOverrides = _DirtyVariantOverrides_
it "adds new dirty variant overrides", ->
DirtyVariantOverrides.add variantOverride
expect(DirtyVariantOverrides.dirtyVariantOverrides).toEqual
2:
1:
variant_id: 1
hub_id: 2
price: 3
count_on_hand: 4
describe "adding a new dirty variant override", ->
it "adds new dirty variant overrides", ->
DirtyVariantOverrides.add(2,1,5)
expect(DirtyVariantOverrides.dirtyVariantOverrides).toEqual { 2: { 1: { id: 5, variant_id: 1, hub_id: 2 } } }
it "updates existing dirty variant overrides", ->
DirtyVariantOverrides.dirtyVariantOverrides =
2:
1:
variant_id: 5
hub_id: 6
price: 7
count_on_hand: 8
DirtyVariantOverrides.add variantOverride
expect(DirtyVariantOverrides.dirtyVariantOverrides).toEqual
2:
1:
variant_id: 1
hub_id: 2
price: 3
count_on_hand: 4
describe "setting the value of an attribute", ->
beforeEach ->
spyOn(DirtyVariantOverrides, "add").andCallThrough()
describe "when a record for the given VO does not exist", ->
beforeEach ->
DirtyVariantOverrides.dirtyVariantOverrides = {}
it "sets the specified attribute on a new dirty VO", ->
DirtyVariantOverrides.set(2,1,5,'count_on_hand', 10)
expect(DirtyVariantOverrides.add).toHaveBeenCalledWith(2,1,5)
expect(DirtyVariantOverrides.dirtyVariantOverrides).toEqual
2:
1:
id: 5
variant_id: 1
hub_id: 2
count_on_hand: 10
describe "when a record for the given VO exists", ->
beforeEach ->
DirtyVariantOverrides.dirtyVariantOverrides = { 2: { 1: { id: 5, variant_id: 1, hub_id: 2, price: 27 } } }
it "sets the specified attribute on a new dirty VO", ->
DirtyVariantOverrides.set(2,1,5,'count_on_hand', 10)
expect(DirtyVariantOverrides.add).toHaveBeenCalledWith(2,1,5)
expect(DirtyVariantOverrides.dirtyVariantOverrides).toEqual
2:
1:
id: 5
variant_id: 1
hub_id: 2
price: 27
count_on_hand: 10
describe "with a number of variant overrides", ->
beforeEach ->

View File

@@ -1,9 +1,9 @@
describe "VariantOverrides service", ->
VariantOverrides = $httpBackend = null
variantOverrides = [
{id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false }
{id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false}
{id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false}
{id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
{id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
{id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
]
beforeEach ->
@@ -19,10 +19,10 @@ describe "VariantOverrides service", ->
it "indexes variant overrides by hub_id -> variant_id", ->
expect(VariantOverrides.variantOverrides).toEqual
10:
100: {id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false }
200: {id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false }
100: {id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
200: {id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
20:
300: {id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false }
300: {id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
it "ensures blank data available for some products", ->
hubs = [{id: 10}, {id: 20}, {id: 30}]
@@ -34,23 +34,23 @@ describe "VariantOverrides service", ->
]
VariantOverrides.ensureDataFor hubs, products
expect(VariantOverrides.variantOverrides[10]).toEqual
100: { id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false }
200: { id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false }
300: { hub_id: 10, variant_id: 300, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
400: { hub_id: 10, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
500: { hub_id: 10, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
100: { id: 1, hub_id: 10, variant_id: 100, sku: "V100", price: 1, count_on_hand: 1, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
200: { id: 2, hub_id: 10, variant_id: 200, sku: "V200", price: 2, count_on_hand: 2, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
300: { hub_id: 10, variant_id: 300, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
400: { hub_id: 10, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
500: { hub_id: 10, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
expect(VariantOverrides.variantOverrides[20]).toEqual
100: { hub_id: 20, variant_id: 100, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
200: { hub_id: 20, variant_id: 200, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
300: { id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false }
400: { hub_id: 20, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
500: { hub_id: 20, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
100: { hub_id: 20, variant_id: 100, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
200: { hub_id: 20, variant_id: 200, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
300: { id: 3, hub_id: 20, variant_id: 300, sku: "V300", price: 3, count_on_hand: 3, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
400: { hub_id: 20, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: []}
500: { hub_id: 20, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
expect(VariantOverrides.variantOverrides[30]).toEqual
100: { hub_id: 30, variant_id: 100, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
200: { hub_id: 30, variant_id: 200, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
300: { hub_id: 30, variant_id: 300, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
400: { hub_id: 30, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
500: { hub_id: 30, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false }
100: { hub_id: 30, variant_id: 100, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
200: { hub_id: 30, variant_id: 200, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
300: { hub_id: 30, variant_id: 300, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: []}
400: { hub_id: 30, variant_id: 400, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
500: { hub_id: 30, variant_id: 500, sku: null, price: null, count_on_hand: null, on_demand: null, default_stock: null, resettable: false, tag_list : '', tags: [] }
it "updates the IDs of variant overrides", ->
VariantOverrides.variantOverrides[2] = {}