mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Adding all the Angular filters required to make this work
This commit is contained in:
14
app/assets/javascripts/darkswarm/filters/active.js.coffee
Normal file
14
app/assets/javascripts/darkswarm/filters/active.js.coffee
Normal file
@@ -0,0 +1,14 @@
|
||||
Darkswarm.filter 'active', ()->
|
||||
(objects, options)->
|
||||
objects ||= []
|
||||
options ?= null
|
||||
|
||||
if options.open and !options.closed
|
||||
objects.filter (obj)->
|
||||
obj.active
|
||||
else if options.closed and !options.open
|
||||
objects.filter (obj)->
|
||||
!obj.active
|
||||
else
|
||||
objects
|
||||
|
||||
13
app/assets/javascripts/darkswarm/filters/shipping.js.coffee
Normal file
13
app/assets/javascripts/darkswarm/filters/shipping.js.coffee
Normal file
@@ -0,0 +1,13 @@
|
||||
Darkswarm.filter 'shipping', ()->
|
||||
(objects, options)->
|
||||
objects ||= []
|
||||
options ?= null
|
||||
|
||||
if options.pickup and !options.delivery
|
||||
objects.filter (obj)->
|
||||
obj.pickup
|
||||
else if options.delivery and !options.pickup
|
||||
objects.filter (obj)->
|
||||
obj.delivery
|
||||
else
|
||||
objects
|
||||
10
app/assets/javascripts/darkswarm/filters/taxons.js.coffee
Normal file
10
app/assets/javascripts/darkswarm/filters/taxons.js.coffee
Normal file
@@ -0,0 +1,10 @@
|
||||
Darkswarm.filter 'taxons', (Matcher)->
|
||||
# Filter anything that responds to object.taxons, and/or object.primary_taxon
|
||||
(objects, text) ->
|
||||
objects ||= []
|
||||
text ?= ""
|
||||
objects.filter (obj)->
|
||||
Matcher.match([obj.primary_taxon?.name || ""], text) || obj.taxons.some (taxon)->
|
||||
Matcher.match [taxon.name], text
|
||||
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
describe 'filtering by active', ->
|
||||
filterByActive = null
|
||||
objects = [
|
||||
{
|
||||
active: true
|
||||
}
|
||||
{
|
||||
active: false
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
inject ($filter) ->
|
||||
filterByActive = $filter('active')
|
||||
|
||||
it "filters to active", ->
|
||||
expect(filterByActive(objects, {closed: false, open: true})[0]).toBe objects[0]
|
||||
|
||||
it "filters to inactive", ->
|
||||
expect(filterByActive(objects, {closed: true, open: false})[0]).toBe objects[1]
|
||||
|
||||
it "doesn't filter if needed", ->
|
||||
expect(filterByActive(objects, {closed: false, open: false})).toBe objects
|
||||
|
||||
it "filters to all", ->
|
||||
expect(filterByActive(objects, {closed: true, open: true})).toBe objects
|
||||
@@ -0,0 +1,30 @@
|
||||
describe 'filtering by shipping method', ->
|
||||
filterByShippingMethod = null
|
||||
objects = [
|
||||
{
|
||||
delivery: true
|
||||
pickup: false
|
||||
}
|
||||
{
|
||||
delivery: false
|
||||
pickup: true
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
inject ($filter) ->
|
||||
filterByShippingMethod = $filter('shipping')
|
||||
|
||||
it "filters to pickup", ->
|
||||
expect(filterByShippingMethod(objects, {pickup: true, delivery: false})[0]).toBe objects[1]
|
||||
|
||||
it "filters to delivery", ->
|
||||
expect(filterByShippingMethod(objects, {pickup: false, delivery: true})[0]).toBe objects[0]
|
||||
|
||||
it "filters to both", ->
|
||||
expect(filterByShippingMethod(objects, {pickup: true, delivery: true})).toBe objects
|
||||
|
||||
it "filters to none", ->
|
||||
expect(filterByShippingMethod(objects, {pickup: false, delivery: false})).toBe objects
|
||||
@@ -0,0 +1,27 @@
|
||||
describe 'filtering by taxons', ->
|
||||
filterByTaxons = null
|
||||
objects = [
|
||||
{
|
||||
taxons: []
|
||||
primary_taxon:
|
||||
name: "frogs"
|
||||
}
|
||||
{
|
||||
taxons: [
|
||||
{name: "kittens"}
|
||||
{name: "puppies"}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
beforeEach ->
|
||||
module 'Darkswarm'
|
||||
inject ($filter) ->
|
||||
filterByTaxons = $filter('taxons')
|
||||
|
||||
it "filters by primary taxon", ->
|
||||
expect(filterByTaxons(objects, "frogs")[0]).toBe objects[0]
|
||||
|
||||
it "filters by taxons", ->
|
||||
expect(filterByTaxons(objects, "kittens")[0]).toBe objects[1]
|
||||
Reference in New Issue
Block a user