Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to load a previous revision of a gist #340

Merged
merged 4 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/adapters/gist-revision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ApplicationAdapter from './application';

export default ApplicationAdapter.extend({
urlForQuery: function(query) {
const host = this.host || "";
return `${host}/gists/${query.gistId}/${query.revId}`;
}
});
4 changes: 4 additions & 0 deletions app/gist/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,16 @@ export default Ember.Controller.extend({
const numColumns = this.get('realNumColumns');

let j = 0;
const len = files.get('length');
for (let i = 1; i <= numColumns; ++i) {
if (!this.getColumnFile(i)) {
if (files) {
j = 0;
while (!this.isOpen(files.objectAt(j))) {
j++;
if (j >= len) {
return;
}
}
let file = files.objectAt(j);
if (file) {
Expand Down
19 changes: 19 additions & 0 deletions app/gist/edit/revision/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import GistEditRoute from "../route";

export default GistEditRoute.extend({

controllerName: 'gist',
templateName: 'gist',

model(params) {
this.get('store').unloadAll('gistFile');
const gistParams = this.paramsFor('gist.edit');

return this.get('store').query('gist-revision', {
gistId: gistParams.gistId,
revId: params.revId
}).then((response) => {
return response.get('firstObject');
});
}
});
2 changes: 1 addition & 1 deletion app/gist/edit/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default GistRoute.extend({
model (params) {
this.get('store').unloadAll('gistFile');

return this.get('store').find('gist', params.id);
return this.get('store').find('gist', params.gistId);
},

setupController() {
Expand Down
5 changes: 5 additions & 0 deletions app/mirage/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export function testConfig() {
return db.gists.find(id);
});

this.get('/gists/:id/:rev_id', function(db, request) {
let id = request.params.id;
return db["gist-revisions"].find(id);
});

this.get('/user', function(db) {
return db.users.find(1);
});
Expand Down
125 changes: 125 additions & 0 deletions app/mirage/factories/gist-revision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/**
* This is a factory definition for a gist revision.
*/
import GistFactory from './gist';

export default GistFactory.extend({
url() {
return `https://api.github.com/gists/${this.id}/${this.revId}`;
}
});

/* Sample
{
"url": "https://api.github.com/gists/a9d365962ab98c2e12cd/3421e6dde701325b5da67ba06dc150b7ad7e0547",
"forks_url": "https://api.github.com/gists/a9d365962ab98c2e12cd/forks",
"commits_url": "https://api.github.com/gists/a9d365962ab98c2e12cd/commits",
"id": "a9d365962ab98c2e12cd",
"git_pull_url": "https://gist.github.com/a9d365962ab98c2e12cd.git",
"git_push_url": "https://gist.github.com/a9d365962ab98c2e12cd.git",
"html_url": "https://gist.github.com/a9d365962ab98c2e12cd",
"files": {
"application.controller.js": {
"filename": "application.controller.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/Gaurav0/a9d365962ab98c2e12cd/raw/a5591f634f02783af0de26c2a0799e2a6c2deb1d/application.controller.js",
"size": 247,
"truncated": false,
"content": "import Ember from 'ember';\nimport JsonProxy from '../json-proxy';\n\nexport default Ember.Controller.extend({\n appName: 'Ember Twiddle',\n \n init(...args) {\n this._super(...args);\n this.set('somevar', JsonProxy.build([1, 2, 3]));\n\t}\n});\n"
},
"application.template.hbs": {
"filename": "application.template.hbs",
"type": "text/plain",
"language": "Handlebars",
"raw_url": "https://gist.githubusercontent.com/Gaurav0/a9d365962ab98c2e12cd/raw/95de1498ec47fced63bb38e5a2ec69ef9f1620a5/application.template.hbs",
"size": 108,
"truncated": false,
"content": "<h1>Welcome to {{appName}}</h1>\n<br>\n<ul>\n{{#each somevar as |num|}}\n <li>{{num}}</li>\n{{/each}}\n</ul>\n<br>"
},
"json-proxy.js": {
"filename": "json-proxy.js",
"type": "application/javascript",
"language": "JavaScript",
"raw_url": "https://gist.githubusercontent.com/Gaurav0/a9d365962ab98c2e12cd/raw/7f218e6a2788227292b99fca84772fb4885a3287/json-proxy.js",
"size": 188,
"truncated": false,
"content": "import Ember from \"ember\";\n\nvar JsonProxy = Ember.ArrayProxy.extend({});\nJsonProxy.build = function(ary) {\n return Ember.ArrayProxy.create({ content: ary });\n};\n\nexport default JsonProxy;"
},
"twiddle.json": {
"filename": "twiddle.json",
"type": "application/json",
"language": "JSON",
"raw_url": "https://gist.githubusercontent.com/Gaurav0/a9d365962ab98c2e12cd/raw/87a00ec9acd16432438a70288db99b87371a9aeb/twiddle.json",
"size": 511,
"truncated": false,
"content": "{\n \"version\": \"0.6.0\",\n \"EmberENV\": {\n \"FEATURES\": {}\n },\n \"options\": {\n \"enable-testing\": false\n },\n \"dependencies\": {\n \"jquery\": \"https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js\",\n \"ember\": \"https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.3.1/ember.debug.js\",\n \"ember-data\": \"https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/2.3.3/ember-data.js\",\n \"ember-template-compiler\": \"https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember-template-compiler.js\"\n }\n}\n"
}
},
"public": true,
"created_at": "2016-02-25T22:26:03Z",
"updated_at": "2016-02-25T22:28:29Z",
"description": "JsonProxy",
"comments": 0,
"user": null,
"comments_url": "https://api.github.com/gists/a9d365962ab98c2e12cd/comments",
"owner": {
"login": "Gaurav0",
"id": 313960,
"avatar_url": "https://avatars.githubusercontent.com/u/313960?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/Gaurav0",
"html_url": "https://github.com/Gaurav0",
"followers_url": "https://api.github.com/users/Gaurav0/followers",
"following_url": "https://api.github.com/users/Gaurav0/following{/other_user}",
"gists_url": "https://api.github.com/users/Gaurav0/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Gaurav0/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Gaurav0/subscriptions",
"organizations_url": "https://api.github.com/users/Gaurav0/orgs",
"repos_url": "https://api.github.com/users/Gaurav0/repos",
"events_url": "https://api.github.com/users/Gaurav0/events{/privacy}",
"received_events_url": "https://api.github.com/users/Gaurav0/received_events",
"type": "User",
"site_admin": false
},
"forks": [

],
"history": [
{
"user": {
"login": "Gaurav0",
"id": 313960,
"avatar_url": "https://avatars.githubusercontent.com/u/313960?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/Gaurav0",
"html_url": "https://github.com/Gaurav0",
"followers_url": "https://api.github.com/users/Gaurav0/followers",
"following_url": "https://api.github.com/users/Gaurav0/following{/other_user}",
"gists_url": "https://api.github.com/users/Gaurav0/gists{/gist_id}",
"starred_url": "https://api.github.com/users/Gaurav0/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/Gaurav0/subscriptions",
"organizations_url": "https://api.github.com/users/Gaurav0/orgs",
"repos_url": "https://api.github.com/users/Gaurav0/repos",
"events_url": "https://api.github.com/users/Gaurav0/events{/privacy}",
"received_events_url": "https://api.github.com/users/Gaurav0/received_events",
"type": "User",
"site_admin": false
},
"version": "3421e6dde701325b5da67ba06dc150b7ad7e0547",
"committed_at": "2016-02-25T22:26:03Z",
"change_status": {
"total": 42,
"additions": 42,
"deletions": 0
},
"url": "https://api.github.com/gists/a9d365962ab98c2e12cd/3421e6dde701325b5da67ba06dc150b7ad7e0547"
}
],
"truncated": false
}

*/



7 changes: 1 addition & 6 deletions app/models/gist-revision.js
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
import DS from 'ember-data';

export default DS.Model.extend({
committedAt: DS.attr('date'),
shortId: DS.attr('string')
});
export { default } from "./gist";
3 changes: 2 additions & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ const Router = Ember.Router.extend({
Router.map(function() {
this.route('gist', {path: '/'}, function() {
this.route('new', {path: '/'});
this.route('edit', {path: '/:id'}, function() {
this.route('edit', {path: '/:gistId'}, function() {
this.route('copy');
this.route('revision', {path: '/:revId'});
});
});
this.route('twiddles');
Expand Down
7 changes: 7 additions & 0 deletions app/serializers/gist-revision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import GistSerializer from "./gist";

export default GistSerializer.extend({
normalizeQueryResponse: function(store, primaryModelClass, payload, id, requestType) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiousity, why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm querying for a record using two parameters. Ember Data expects an array back when you query. So I wrap the payload in an array.

return this._super(store, primaryModelClass, [payload], id, requestType);
}
});
12 changes: 4 additions & 8 deletions app/serializers/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ export default ApplicationSerializer.extend({
},

normalizeSingleResponse: function(store, primaryModelClass, payload, id, requestType) {
if (primaryModelClass.modelName === "gist") {
this.normalizeGist(payload, false);
}
this.normalizeGist(payload, false);

return this._super(store, primaryModelClass, payload, id, requestType);
},

normalizeArrayResponse: function(store, primaryModelClass, payload, id, requestType) {
if (primaryModelClass.modelName === "gist") {
payload.forEach(function(hash) {
this.normalizeGist(hash, true);
}.bind(this));
}
payload.forEach(function(hash) {
this.normalizeGist(hash, true);
}.bind(this));

return this._super(store, primaryModelClass, payload, id, requestType);
},
Expand Down
1 change: 1 addition & 0 deletions tests/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"currentPath",
"currentRouteName",
"runGist",
"runRevision",
"waitForLoadedIFrame",
"outputPane",
"outputContents"
Expand Down
31 changes: 31 additions & 0 deletions tests/acceptance/git-revision-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'ember-twiddle/tests/helpers/start-app';

module('Acceptance | gist-revision', {
beforeEach: function() {
this.application = startApp();
},

afterEach: function() {
Ember.run(this.application, 'destroy');
}
});

test('Able to load a previous revision of a gist', function(assert) {

const files = [
{
filename: "application.template.hbs",
content: "Hello, World!"
}
];

runRevision(files);

andThen(function() {
const outputDiv = 'div';

assert.equal(outputContents(outputDiv), 'Hello, World!', 'Previous version of a gist is displayed');
});
});
13 changes: 13 additions & 0 deletions tests/helpers/run-gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default function(app, options) {
const gist_id = options.gist_id || "35de43cb81fc35ddffb2";
const commit = options.commit || "f354c6698b02fe3243656c8dc5aa0303cc7ae81c";
const initialRoute = options.initialRoute || "/";
const isGitRevision = options.type === "revision";
let files = options.files || [];

files.push({
Expand Down Expand Up @@ -43,13 +44,25 @@ export default function(app, options) {

server.create('user', {login: login});
const owner = server.create('owner', {login: login});

if (isGitRevision) {
server.create('gist-revision', {
id: gist_id,
revId: commit,
owner: owner,
files: gistFiles
});
}
server.create('gist', {
id: gist_id,
owner: owner,
files: gistFiles
});

let url = "/" + gist_id;
if (isGitRevision) {
url += "/" + commit;
}
if (initialRoute !== "/") {
url += "?route=" + initialRoute;
}
Expand Down
14 changes: 14 additions & 0 deletions tests/helpers/run-revision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Ember from "ember";
import runGist from "./run-gist";

const { isArray } = Ember;

export default function(app, options) {
if (isArray(options)) {
options = {files: options};
}

options.type = "revision";

return runGist(app, options);
}
3 changes: 2 additions & 1 deletion tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
setResolver
} from 'ember-qunit';
import runGist from "./helpers/run-gist";
import runRevision from "./helpers/run-revision";
import waitForLoadedIFrame from './helpers/wait-for-loaded-iframe';

setResolver(resolver);
Expand All @@ -20,5 +21,5 @@ Ember.Test.registerHelper('outputContents', function(app, selector) {
});

Ember.Test.registerAsyncHelper('runGist', runGist);

Ember.Test.registerAsyncHelper('runRevision', runRevision);
Ember.Test.registerAsyncHelper('waitForLoadedIFrame', waitForLoadedIFrame);