Skip to content

Commit 9895710

Browse files
Andrew DunkmanAndrew Dunkman
authored andcommitted
Refactored events into collection and model objects.
1 parent fb2adaf commit 9895710

File tree

5 files changed

+95
-74
lines changed

5 files changed

+95
-74
lines changed

collections/events.coffee

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
StoredCollection = require '../data/storedCollection'
2+
3+
class Events extends StoredCollection
4+
constructor: () -> super
5+
6+
find: (criteria, callback) ->
7+
super(Events._collectionName, criteria, callback)
8+
9+
findByUser: (user, callback) ->
10+
userId = if user.attributes?._id? then user.attributes._id else user
11+
@find({ user: userId }, callback)
12+
13+
Events._collectionName = 'events'
14+
module.exports = Events

models/event.coffee

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,14 @@
1-
event_store = {}
1+
Model = require './model'
2+
Events = require '../collections/events'
23

3-
findEventsByUserId = (user_id) ->
4-
event_store[user_id] or []
4+
class Event extends Model
5+
constructor: (attributes = {}) ->
6+
super
7+
@attributes._id or= undefined
8+
@attributes.name or= undefined
9+
@attributes.codes or= []
510

6-
Event = (id, name) ->
7-
this.name = name
8-
this.id = id
9-
this.codes = []
10-
return
11+
save: (callback) ->
12+
super(Events._collectionName, callback)
1113

12-
Event.prototype.addCodes = (new_codes, callback) ->
13-
this.codes = this.codes.concat new_codes
14-
callback null, this
15-
16-
createEvent = (user_id, event_name, callback) ->
17-
events = findEventsByUserId user_id
18-
event = new Event events.length, event_name
19-
events.push event
20-
event_store[user_id] = events
21-
callback null, event
22-
23-
findFirst = (user_id, filter) ->
24-
result = findEventsByUserId(user_id).filter filter
25-
result[0] || null
26-
27-
findByName = (user_id, event_name, callback) ->
28-
callback null, findFirst user_id, (e) ->
29-
e.name == event_name
30-
31-
list = (user_id, callback) ->
32-
callback null, findEventsByUserId user_id
33-
34-
module.exports =
35-
list: list
36-
findByName: findByName
37-
create: createEvent
14+
module.exports = Event
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
should = require 'should'
2+
User = require '../../models/user'
3+
Events = require '../../collections/events'
4+
5+
callsToFind = {}
6+
7+
getEvents = (key) ->
8+
callsToFind[key] = []
9+
events = new Events()
10+
events._adapter =
11+
find: (collection, criteria, callback) ->
12+
callsToFind[key].push([collection, criteria, callback])
13+
callback(null, [])
14+
return events
15+
16+
describe('Events (collections/events)', ->
17+
describe('calling find() with criteria and a callback', ->
18+
getEvents('test1').find({ 'one': true }, ->)
19+
20+
it 'should call find() in \'events\' collection on base', ->
21+
callsToFind.test1.should.have.length(1)
22+
callsToFind.test1[0].should.have.length(3)
23+
callsToFind.test1[0][0].should.equal('events')
24+
25+
it 'should call find() with criteria on base', ->
26+
callsToFind.test1[0][1].should.eql({ 'one': true })
27+
)
28+
29+
describe('calling findByUser() with a user object', ->
30+
beforeEach ->
31+
user = new User({ _id: 33 });
32+
getEvents('test2').findByUser(user, ->)
33+
34+
it 'should call find() in \'events\' collection on base', ->
35+
callsToFind.test2.should.have.length(1)
36+
callsToFind.test2[0].should.have.length(3)
37+
callsToFind.test2[0][0].should.equal('events')
38+
39+
it 'should call find() with user id on base', ->
40+
callsToFind.test2[0][1].should.eql({ 'user': 33 })
41+
)
42+
43+
describe('calling findByUser() with a user id', ->
44+
beforeEach ->
45+
getEvents('test2').findByUser(37, ->)
46+
47+
it 'should call find() in \'events\' collection on base', ->
48+
callsToFind.test2.should.have.length(1)
49+
callsToFind.test2[0].should.have.length(3)
50+
callsToFind.test2[0][0].should.equal('events')
51+
52+
it 'should call find() with user id on base', ->
53+
callsToFind.test2[0][1].should.eql({ 'user': 37 })
54+
)
55+
)

test/models/event.spec.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
should = require 'should'
2+
Event = require '../../models/event.coffee'
3+
4+
describe('Events (models/event)', ->
5+
describe('creating an event', ->
6+
beforeEach ->
7+
@event = new Event()
8+
9+
it 'should have no name', ->
10+
should.not.exist(@event.attributes.name)
11+
12+
it 'should have zero codes', ->
13+
@event.attributes.codes.length.should.equal(0)
14+
)
15+
)

test/models/events.spec.coffee

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)