Skip to content

Commit 51dbe85

Browse files
author
Dustin Farris
committed
Implement date and datetime transforms
1 parent 8cde939 commit 51dbe85

File tree

9 files changed

+176
-0
lines changed

9 files changed

+176
-0
lines changed

karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = function(karma) {
88
"adapter_tests.js",
99
"adapter_embedded_tests.js",
1010
"adapter_polymorphic_tests.js"
11+
'transforms_tests.js'
1112
],
1213

1314
logLevel: karma.LOG_ERROR,

src/initializer.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Ember.Application.initializer({
2+
name: 'DjangoDatetimeTransforms',
3+
4+
initialize: function(container, application) {
5+
application.register('transform:date', DS.DjangoDateTransform);
6+
application.register('transform:datetime', DS.DjangoDatetimeTransform);
7+
}
8+
});

src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
require("./serializer");
22
require("./adapter");
3+
require('./transforms/date');
4+
require('./transforms/datetime');
5+
require("./initializer");
36
require("./version");

src/transforms/date.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
DS.DjangoDateTransform = DS.Transform.extend({
2+
deserialize: function(serialized) {
3+
if (typeof serialized === 'string') {
4+
return new Date(Ember.Date.parse(serialized));
5+
} else if (typeof serialized === 'number') {
6+
return new Date(serialized);
7+
} else if (Ember.isEmpty(serialized)) {
8+
return serialized;
9+
} else {
10+
return null;
11+
}
12+
},
13+
serialize: function(date) {
14+
if (date instanceof Date && date.toString() !== 'Invalid Date') {
15+
return date.toISOString().slice(0, 10);
16+
} else {
17+
return null;
18+
}
19+
}
20+
});

src/transforms/datetime.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
DS.DjangoDatetimeTransform = DS.Transform.extend({
2+
deserialize: function(serialized) {
3+
if (typeof serialized === 'string') {
4+
return new Date(Ember.Date.parse(serialized));
5+
} else if (typeof serialized === 'number') {
6+
return new Date(serialized);
7+
} else if (Ember.isEmpty(serialized)) {
8+
return serialized;
9+
} else {
10+
return null;
11+
}
12+
},
13+
serialize: function(datetime) {
14+
if (datetime instanceof Date && datetime.toString() !== 'Invalid Date') {
15+
return datetime.toJSON();
16+
} else {
17+
return null;
18+
}
19+
}
20+
});

tests/app.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,43 @@ App.Post = App.Message.extend({});
159159
App.Comment = App.Message.extend({});
160160

161161

162+
App.Obituary = DS.Model.extend({
163+
publishOn: DS.attr('date'),
164+
timeOfDeath: DS.attr('datetime'),
165+
});
166+
167+
168+
App.NewObituaryController = Ember.Controller.extend({
169+
publishOn: '',
170+
timeOfDeath: '',
171+
172+
actions: {
173+
174+
createObituary: function() {
175+
176+
var newObituary = this.store.createRecord('obituary', {
177+
publishOn: new Date(this.get('publishOn')),
178+
timeOfDeath: new Date(this.get('timeOfDeath')),
179+
});
180+
181+
return newObituary.save();
182+
}
183+
}
184+
});
185+
186+
187+
App.ObituariesRoute = Ember.Route.extend({
188+
189+
model: function() {
190+
return this.store.find('obituary');
191+
}
192+
});
193+
194+
195+
App.ObituariesController = Ember.ArrayController.extend({
196+
});
197+
198+
162199
App.OthersRoute = Ember.Route.extend({
163200
model: function() {
164201
return this.store.find('other');
@@ -335,6 +372,8 @@ App.Router.map(function() {
335372
this.resource("tag", { path : "/tag/:tag_id" });
336373
this.resource("user", { path : "/user/:user_id" });
337374
this.resource("preserialized", { path: "/preserialized" });
375+
this.resource('obituaries');
376+
this.route('new-obituary');
338377
});
339378

340379
App.ApplicationAdapter = DS.DjangoRESTAdapter.extend({
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<h2>Add an Obituary</h2>
2+
3+
<form {{action 'createObituary'}} on='submit'>
4+
<div>
5+
<label>Publish On</label>
6+
{{input class='publish-on' value=publishOn}}
7+
</div>
8+
<div>
9+
<label>Time of Death</label>
10+
{{input class='time-of-death' value=timeOfDeath}}
11+
</div>
12+
<div>
13+
<label>Memorandum</label>
14+
{{input class='memorandum' value=memorandum}}
15+
</div>
16+
<div>
17+
<button class="submit" type="submit">Submit</button>
18+
</div>
19+
</form>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<ul class="obituaries">
2+
<li>Hi</li>
3+
{{#each}}
4+
<li class="obituary">
5+
<p class="publish-on">{{publishOn}}</p>
6+
<p class="time-of-death">{{timeOfDeath}}</p>
7+
</li>
8+
{{/each}}
9+
</ul>

tests/transforms_tests.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module('transforms integration tests', {
2+
setup: function() {
3+
ajaxHash = null;
4+
App.reset();
5+
},
6+
teardown: function() {
7+
$.mockjaxClear();
8+
}
9+
});
10+
11+
test('date attribute serializes properly', function() {
12+
stubEndpointForHttpRequest('/api/new-obituary', {}, 'POST', 201);
13+
visit('/new-obituary');
14+
fillIn('.publish-on', '2012/08/29');
15+
click('button.submit');
16+
17+
andThen(function() {
18+
equal(
19+
ajaxHash.data,
20+
'{"publish_on":"2012-08-29","time_of_death":null}'
21+
);
22+
});
23+
});
24+
25+
test('datetime attribute serializes properly', function() {
26+
stubEndpointForHttpRequest('/api/new-obituary', {}, 'POST', 201);
27+
visit('/new-obituary');
28+
fillIn('.time-of-death', '2014-11-19T17:38:00.000Z');
29+
click('button.submit');
30+
31+
andThen(function() {
32+
equal(
33+
ajaxHash.data,
34+
'{"publish_on":null,"time_of_death":"2014-11-19T17:38:00.000Z"}'
35+
);
36+
});
37+
});
38+
39+
test('date attribute deserializes properly', function() {
40+
var response = '[{"id":1,"publish_on":"2012-08-29","time_of_death":null}]';
41+
stubEndpointForHttpRequest('/api/obituaries/', response, 'GET', 200);
42+
visit('/obituaries/');
43+
44+
andThen(function() {
45+
equal(find('.publish-on').text(), 'Tue Aug 28 2012 20:00:00 GMT-0400 (EDT)');
46+
});
47+
});
48+
49+
test('datetime attribute deserializes properly', function() {
50+
var response = '[{"id":1,"publish_on":null,"time_of_death":"2014-11-19T17:38:00.000Z"}]';
51+
stubEndpointForHttpRequest('/api/obituaries/', response, 'GET', 200);
52+
visit('/obituaries/');
53+
54+
andThen(function() {
55+
equal(find('.time-of-death').text(), 'Wed Nov 19 2014 12:38:00 GMT-0500 (EST)');
56+
});
57+
});

0 commit comments

Comments
 (0)