Sorting projects

Updated over 7 years ago by Thomas Ochman
After a recent update we are sorting projects based om their status and `commit count`.

@projects = Project.order('status ASC').order('commit_count DESC NULLS LAST').search(params[:search], params[:page]).includes(:user)
This needs to be refactored and I would suggest that we move the logic to the model and use a scope rather then doing the ordering in the controller. (The `NULLS LAST` is important in order to det all `nil` values of `commit_count` to not interfere with the ordering.)

Anyway, another suggestion for displaying projects could be after the last date a commit was made to the repository. 

We are already using the Octokit gem as a wrapper for the GitHub API so I'm thinking let's try to play around with this to get the data that we need.

How about: 
last_commit = Octokit.commits("AgileVentures/Websiteone").last
 => {:sha=>"466f1e396b639a716e064ed1e54a8c7e63d56995", :commit=>{:author=>{:name=>"Thomas Ochman", :email=>"", :date=>2015-03-12 09:20:02 UTC}, :committer=>{:name=>"Thomas Ochman", :email=>"", :date=>2015-03-12 09:20:02 UTC}, :message=>"added method to make sure the 'github_url' has 'https://' protocol", :tree=>{:sha=>"26b76d57fef17952f6fb6ec67f9b9f68e95e6fe9", :url=>""}, :url=>"", :comment_count=>0}, :url=>"", :html_url=>"", :comments_url=>"", :author=>nil, :committer=>nil, :parents=>[{:sha=>"3e4710ad282149ebf280a9a4a43140cea80ef8ec", :url=>"", :html_url=>""}]} 
Consequently, passing in arguments we can narrow down the search:
last_commit = Octokit.commits("AgileVentures/Websiteone", "develop").last <= checking the 'develop' branch.
last_commit = Octokit.commits("AgileVentures/Websiteone", "master", since: ( - 1.month)).last <= checking commits from the last month.
For the purpose of ordering on `projects#index` I don't think we need to modify our query. 
Now, getting the date of that commit can be done by: 
 => 2015-03-12 09:20:02 UTC
and store it as an attribute in the db and used for ordering....

Formatting the output  can be done by:
 => "March 12, 2015 09:20" 
If you want to try this in `irb` you haw to install the gem, open irb and require it as well as `active_support` and possibly `active_support/core_ext`
require 'octokit'
require 'active_support'
(require 'active_support/core_ext')
Let's talk about if this is a good way to move forward on `project#index` and if we want to implement something like this in the app. 


comments powered by Disqus