Skip to content

Commit 8935e57

Browse files
committed
Base model now has a constructor that takes attributes and a toJSON that jsons them
1 parent f801ee1 commit 8935e57

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

models/model.coffee

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
StoredObject = require '../data/storedObject'
22

33
class Model extends StoredObject
4+
constructor: (attributes) ->
5+
@attributes = attributes || {}
6+
47
save: (collection, callback) ->
5-
super collection, @toJSON(), callback
8+
super(collection, @toJSON(), callback)
69

710
toJSON: () ->
8-
{}
11+
JSON.parse(JSON.stringify(@attributes))
912

1013
module.exports = Model

test/models/model_test.coffee

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,41 @@ vows.describe('Base model (models/model)').addBatch(
1919
'': {
2020
topic: () -> getTopic()
2121

22-
'when toJSON() is called': {
23-
topic: (instance) -> instance.toJSON()
22+
'when toJSON() is called with no attributes': {
23+
topic: (instance) ->
24+
instance.toJSON()
2425

2526
'it should return an empty object': (jsonObject) ->
2627
jsonObject.should.eql({})
2728
}
2829

30+
'when toJSON() is called with attributes': {
31+
topic: (instance) ->
32+
instance.attributes = { 'two': false }
33+
instance.toJSON()
34+
35+
'it should return the attributes object': (jsonObject) ->
36+
jsonObject.should.eql({ 'two': false })
37+
}
38+
2939
'when save() is called': {
3040
topic: (instance) ->
41+
instance.attributes = {
42+
'one': true
43+
}
3144
instance.save('testCollection', @callback)
3245

3346
'it should call save() on the base stored object': (err, attributes) ->
3447
callCount.should.equal(1)
3548
args.should.have.length(1)
36-
37-
'it should pass all arguments to the base object': (err, attributes) ->
49+
50+
'it should pass model.attributes to the save()': (err, attributes) ->
51+
args[0].should.have.length(3)
52+
args[0][1].should.eql({ 'one': true })
53+
54+
'it should pass collection and callback to base object': (err, attributes) ->
3855
args[0].should.have.length(3)
3956
args[0][0].should.equal('testCollection')
40-
args[0][1].should.eql({})
4157
args[0][2].should.equal(@callback)
4258
}
4359
}

0 commit comments

Comments
 (0)