Skip to content

Commit

Permalink
Cleanup controller, move to new domain, display list of recent projects
Browse files Browse the repository at this point in the history
  • Loading branch information
1602 committed Apr 4, 2012
1 parent acba0a2 commit ab29938
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 105 deletions.
40 changes: 11 additions & 29 deletions app/controllers/doc_controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var fs = require('fs');
var path = require('path');
var Project = require('makedoc').Project;

load('application');

Expand All @@ -15,11 +14,6 @@ skipBeforeFilter('protect from forgery');

action('make', function () {
var git = params.user + '/' + params.repo;
var p = new Project;
p.title = params.repo;
p.repo = git;
p.out = app.root + '/public/' + git + '/';
p.layoutHTML = getDocLayout();

if (!path.existsSync(app.root + '/public/' + git)) {
fs.mkdirSync(app.root + '/public/' + git);
Expand All @@ -29,12 +23,14 @@ action('make', function () {
}, 1000);
}

p.download('lib', function () {
p.makeDocumentation();
// makePic(git);
redirect('/' + git + '/');
var r = new Repository(git);
r.generageDocumentation(function (stats) {
if (req.params.format === 'json') {
send(stats);
} else {
redirect('/' + git + '/');
}
});

});

action('update', function () {
Expand All @@ -47,30 +43,16 @@ action('update', function () {
fs.unlinkSync(projectPath + '/' + file);
});

var p = new Project;
p.title = params.repo;
p.repo = git;
p.out = app.root + '/public/' + git + '/';
p.layoutHTML = getDocLayout();
p.download('lib', function () {
p.makeDocumentation();
var r = new Repository(git);
r.generageDocumentation(function (stats) {
if (req.params.format === 'json') {
redirect('/' + git + '/stats.json');
// send(fs.readFileSync(app.root + '/public/' + git + '/stats.json'));
send(stats);
} else {
redirect('/' + git);
redirect('/' + git + '/');
}
// makePic(git);
});
});

function getDocLayout() {
return fs.readFileSync(app.root + '/app/views/layouts/doc_layout.html')
.toString()
.replace(/PROJECT OWNER/g, params.user)
.replace(/PROJECT NAME/g, params.repo);
}

function makePic(git) {
var dirName = app.root + '/public/' + git;
var data = require(dirName + '/stats.json');
Expand Down
34 changes: 12 additions & 22 deletions app/controllers/projects_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,19 @@ action(function whoami() {
});

action(function index() {
User.projects(req.params.user, function (err, projects) {
if (projects) {
var pros = [];
projects.forEach && projects.forEach(function (project) {
var projPath = req.params.user + '/' + project.name;
var projectStatsFile = app.root + '/public/' + projPath + '/stats.json';
if (project.language === 'JavaScript') {
project.path = '/' + projPath;
if (path.existsSync(projectStatsFile)) {
project.stats = JSON.parse(fs.readFileSync(projectStatsFile).toString());
}
pros.push(project);
}
});
render({
title: req.params.user + "'s projects",
projects: pros.sort(byWatchers)
});
}
var user = this.user = req.params.user;
this.title = user + "'s projects";
User.projectsWithStats(user, function (err, projects) {
render({ projects: projects });
});
});

function byWatchers(p1, p2) {
return p2.watchers - p1.watchers;
}
action(function recent() {
ProjectStatsHistory.all({
limit: 5,
order: 'id DESC'
}, function (err, projects) {
send(projects);
});
});

38 changes: 38 additions & 0 deletions app/models/repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var fs = require('fs');
var Project = require('makedoc').Project;

module.exports = Repository;

function Repository(git) {
var g = git.split('/');
this.repo = git;
this.user = g[0];
this.title = g[1];
};

Repository.prototype.generageDocumentation = function (cb) {
var p = new Project;
p.title = this.title;
p.repo = this.repo;
p.out = app.root + '/public/' + this.repo + '/';
p.layoutHTML = this.getDocLayout();
p.download('lib', function () {
p.makeDocumentation();
if (p.stats.coverage > 5) {
ProjectStatsHistory.create({
repo: p.repo,
stats: JSON.stringify(p.stats),
updatedAt: Date.now()
});
}
cb(p.stats);
});
};

Repository.prototype.getDocLayout = function getDocLayout() {
return fs.readFileSync(app.root + '/app/views/layouts/doc_layout.html')
.toString()
.replace(/PROJECT OWNER/g, this.user)
.replace(/PROJECT NAME/g, this.repo);
};

28 changes: 26 additions & 2 deletions app/models/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var path = require('path');
var fs = require('fs');
var https = require('https');

User.findOrCreate = function (data, done) {
User.findOrCreate = function findOrCreate(data, done) {

/* GITHUB */
if (data.githubId) {
Expand All @@ -21,7 +23,7 @@ User.findOrCreate = function (data, done) {

};

User.projects = function (username, cb) {
User.projects = function projects(username, cb) {
if (User.projects._cache[username]) return cb(null, User.projects._cache[username]);
var req = https.get({
host: 'api.github.com',
Expand All @@ -44,6 +46,28 @@ User.projects = function (username, cb) {
});
};

User.projectsWithStats = function projectsWithStats(user, cb) {
User.projects(user, function (err, projects) {
var pros = [];
projects.forEach && projects.forEach(function (project) {
var projPath = user + '/' + project.name;
var projectStatsFile = app.root + '/public/' + projPath + '/stats.json';
if (project.language === 'JavaScript') {
project.path = '/' + projPath;
if (path.existsSync(projectStatsFile)) {
project.stats = JSON.parse(fs.readFileSync(projectStatsFile).toString());
}
pros.push(project);
}
});
cb(err, pros.sort(byWatchers));
});

function byWatchers(p1, p2) {
return p2.pushed_at > p1.pushed_at ? 1 : -1;
}
};

User.projects._cache = {};

// clean cache each hour
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application_layout.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<head>
<title><%= title %></title>
<%- stylesheet_link_tag('bootstrap', 'style') %>
<%- javascript_include_tag('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', 'bootstrap', 'rails', 'application') %>
<%- csrf_meta_tag() %>
<style>
.navbar .container { width: 940px;}
Expand Down Expand Up @@ -49,7 +50,6 @@
</footer>
</div>
</body>
<%- javascript_include_tag('http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js', 'bootstrap', 'rails', 'application') %>
<script type="text/javascript">
var _gaq = _gaq || [];
Expand Down
12 changes: 6 additions & 6 deletions app/views/projects/index.ejs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="page-header"><h1>JavaScript Projects owned by <%- link_to(request.params.user, 'https://github.com/'+ request.params.user) %></h1></div>
<div class="page-header"><h1>JavaScript Projects owned by <%- link_to(user, 'https://github.com/'+ user) %></h1></div>

<% if (projects.length) { %>
<table class="table table-bordered">
Expand All @@ -17,7 +17,7 @@
<a href="<%- project.path %>"><%- project.name %></a>
</h6>Watchers: <%- project.watchers %>, forks: <%- project.forks %>
</td>
<td id="<%- project.path.replace(/[^a-z]/gi, '_') %>">
<td id="<%- project.name.replace(/[^a-z]/gi, '_') %>">
<% if (project.stats) { %>
<strong>Docs coverage <%- project.stats.coverage || 0 %>%</strong><br/>
Classes: <%- project.stats.classes %>,
Expand All @@ -37,15 +37,15 @@
class: project.stats ? 'btn btn-inverse' : 'btn',
'data-remote': 'true',
'data-method': 'POST',
'jsonp': 'handleStats("' + project.path.replace(/[^a-z]/gi, '_') + '")'
'jsonp': 'handleStats("' + project.name.replace(/[^a-z]/gi, '_') + '")'
}) %>
</td>
</tr>
<% }); %>
</tbody>
</table>
<% } else { %>
<div class="alert"><strong class="warning">WTF?</strong> I don't see any javascript projects for github user "<%= request.params.user %>".</div>
<div class="alert"><strong class="warning">WTF?</strong> I don't see any javascript projects for github user <%- link_to(user, 'https://github.com/' + user) %>.</div>
<% } %>

<script>
Expand All @@ -57,13 +57,13 @@
', packages: ' + data.packages +
', methods: ' + data.totalMethods +
', lines: ' + data.codeLinesTotal
).next('td').find('a').toggleClass('disabled');
);
};
}
$(function () {
$('a[data-remote]')
.live('ajax:loading', function () {
.live('ajax:before', function () {
$(this).addClass('disabled');
})
.live('ajax:success', function () {
Expand Down
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exports.routes = function (map) {
map.get('/recent-projects.json', 'projects#recent');
map.get('/my-own-projects', 'projects#whoami');
map.get('/:user', 'projects#index');
map.post('/:user/:repo/update.:format?', 'doc#update');
Expand Down
6 changes: 6 additions & 0 deletions db/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ define('User', function () {
property('avatar');
});

define('ProjectStatsHistory', function () {
property('repo');
property('stats');
property('updatedAt', Number);
});

Loading

0 comments on commit ab29938

Please sign in to comment.