Skip to content

Instantly share code, notes, and snippets.

@BooneTeam
Last active December 19, 2015 13:19
Show Gist options
  • Save BooneTeam/5961286 to your computer and use it in GitHub Desktop.
Save BooneTeam/5961286 to your computer and use it in GitHub Desktop.
Github Functionality for DBConnect This Allows a user to drag their top five Github repositories urls and display them.
repos = [];
$(document).ready(function(){
$('#git_error').hide();
$.each($( "#github_list_showing tr td"),function(index,value){
repos.push($(value).data());
});
$( ".all_repos tr" ).draggable({helper: 'clone'});
$( "#github_list_showing" ).droppable({
drop: function(event,ui) {
if($(this).find('tr').length >= 6){
$(this).draggable({ disabled: true });
$('#git_error').show();
$('#git_error').css({"color" : "red"});
setTimeout(function(){$('#git_error').hide();},5000);
}
else { $(this).append(ui.draggable);
repos.push($(this).find('td:last').data());
}
}
});
$( "#github_list_showing tr" ).draggable({helper: 'clone'});
$( ".all_repos" ).droppable({
drop: function(event,ui) {
$(this).append(ui.draggable);
repos.pop($(this).find('td:last').data());
}
});
$("form.git_save").submit(function(e){
e.preventDefault();
repos.shift();
$.post("/users/"+this.id+"/edit/github", { selected: repos });
});
});
class Github
API_CLIENT_ID = ENV['GITHUB_API_ID']
API_CLIENT_SECRET = ENV['GITHUB_API_SECRET']
def initialize(user)
@user = user
create_token
end
include HTTParty
headers "User-Agent" => ENV['GITHUB_USER_AGENT']
basic_auth ENV['GITHUB_AUTH_USER'], ENV['GITHUB_AUTH_PASS']
def create_token
endpoint = "https://api.github.com/user"
users = "https://api.github.com/users/#{@user.github_handle}/repos"
post_body = {:client_id => API_CLIENT_ID,
:client_secret => API_CLIENT_SECRET,
}
authentication = self.class.post endpoint, :body => post_body.to_json
response = self.class.get users
if response.response['status'] == "404 Not Found"
@github = []
else
@github = response
end
end
def users_repos
users = "https://api.github.com/users/#{@user.github_handle}/repos"
end
def repo_urls
repo_urls = @github.map{|x| x['html_url'] }
end
def repo_names
repo_names = @github.map{|x| x['name'] }
end
def zip_repo_url_names
repo_names.zip(repo_urls)
end
def gets
self.class.get users
end
end
#each user has a github selection associatied with them.
class GitHubSelection < ActiveRecord::Base
attr_accessible :name, :url
belongs_to :user
end
#this was the route that ajax is posting to
post '/users/:user_id/edit/github', to: "users#git_list", :as => "user_github"
# This is part of the HTML of the page that allowed users to edit their top five repos
<div id="repo_area">
<% if @repos %>
<table class="all_repos">
<% @repos.each do |repo| %>
<tr>
<td data-url=<%=repo[1]%> data-name=<%=repo[0]%>><%= repo[0] %></td>
</tr>
<%end%>
</table>
<% end %>
<div id='my_top_5'>
<table id="github_list_showing" class="selected_repos">
<tr>
<td>Drag your top 5 repos that you are proud of for employers to see on your profile.</td>
</tr>
<% if @current_top_5 %>
<% @current_top_5.each do |repo| %>
<tr>
<td data-url=<%=repo.url %> data-name=<%=repo.name%>><%="#{repo.name}"%></td>
</tr>
<% end %>
<% end %>
</table>
<%= form_for @user, :url => user_github_path(@user), method: 'post', html: { :class => 'git_save', :id=>current_user.id } do |f| %>
<%= f.submit "Submit", :disable_with => 'Top 5 Saved' %>
<p id='git_error'>Sorry Brah Only 5 Top Repos Allowed</p>
</div>
</div>
<% end %>
# These are relevant snippets from the UsersController
def git_list
@user = User.find(params[:user_id])
@user.git_hub_selections.each {|selections| selections.destroy }
params[:selected].each do |repo|
selection = GitHubSelection.create(:name => repo[1]['name'], :url => repo[1]['url'])
@user.git_hub_selections << selection
@user.save
end
end
def show
@interest = Interest.new
@user = User.find(params[:id])
@current_top_5 = @user.git_hub_selections
if @user.groupable_type == "Cohort" && @user.github_handle
github = Github.new(@user)
@repos = github.zip_repo_url_names
else
@repos = []
end
@pitcher_interest = Interest.find_by_pitcher_id_and_catcher_id(current_user.id, @user.id)
@catcher_interest = Interest.find_by_pitcher_id_and_catcher_id(@user.id, current_user.id)
end
def edit
@user = User.find(params[:id])
if current_user.id != @user.id
redirect_to users_path
end
if @user.groupable_type == "Cohort" && @user.github_handle
@current_top_5 = @user.git_hub_selections
github = Github.new(@user)
@repos = github.zip_repo_url_names
p @repos
else
@repos = []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment