added github and twitter connections to user profiles

This commit is contained in:
Richard Taylor 2011-09-12 15:39:19 +01:00
Родитель 784646aabe
Коммит e15b4cf2c1
8 изменённых файлов: 95 добавлений и 2 удалений

2
.gitignore поставляемый
Просмотреть файл

@ -4,5 +4,7 @@ log/*.log
tmp/
.sass-cache/
config/database.yml
config/github.yml
config/twitter.yml
db/schema.rb
.rvmrc

Просмотреть файл

@ -15,4 +15,36 @@ class ProfilesController < ApplicationController
render :new
end
end
def github
redirect_to :root unless logged_in? and current_user.profile
profile = current_user.profile
auth = request.env['omniauth.auth']
profile.update_attributes(
:github_token => auth['credentials']['token'],
:github_username => auth['user_info']['nickname'],
:github_url => auth['user_info']['urls']['GitHub'],
:github_public_repo_count => auth['extra']['user_hash']['public_repo_count'],
:github_followers_count => auth['extra']['user_hash']['followers_count']
)
redirect_to current_user
end
def twitter
redirect_to :root unless logged_in? and current_user.profile
profile = current_user.profile
auth = request.env['omniauth.auth']
profile.update_attributes(
:twitter_token => auth['credentials']['token'],
:twitter_secret => auth['credentials']['secret'],
:twitter_username => auth['user_info']['nickname']
)
redirect_to current_user
end
end

Просмотреть файл

@ -2,4 +2,12 @@ class Profile < ActiveRecord::Base
belongs_to :user
validates_presence_of :username, :name
validates_uniqueness_of :username
def github?
self.github_token.present?
end
def twitter?
self.twitter_token.present?
end
end

Просмотреть файл

@ -0,0 +1,17 @@
<h2>Connections</h2>
<ul>
<% if current_user.profile.twitter? %>
<li>You have connected with Twitter</li>
<% else %>
<li><%= link_to 'Connect Twitter Account', '/auth/twitter' %></li>
<% end %>
<% if current_user.profile.github? %>
<li>You have connected with GitHub</li>
<% else %>
<li><%= link_to 'Connect GitHub Account', '/auth/github' %></li>
<% end %>
</ul>

Просмотреть файл

@ -8,10 +8,26 @@
<% if @user.profile.website.present? %>
Website: <%= link_to @user.profile.website, @user.profile.website %>
<% end %>
<% end %>
<% if @user.profile.twitter.present? %>
Twitter: <%= link_to @user.profile.twitter, "http://twitter.com/#{@user.profile.twitter.gsub(/^@/, '')}" %>
<p>Twitter: <%= link_to @user.profile.twitter, "http://twitter.com/#{@user.profile.twitter.gsub(/^@/, '')}" %></p>
<% end %>
<% if current_user.profile.github? %>
<p>
GitHub: <%= link_to current_user.profile.github_username, current_user.profile.github_url %> |
<%= current_user.profile.github_public_repo_count %> Public Repositories |
<%= current_user.profile.github_followers_count %> Followers
</p>
<% end %>
<% if current_user.profile.twitter? %>
<p>Twitter: <%= link_to "@#{current_user.profile.twitter_username}", "https://twitter.com/#{current_user.profile.twitter_username}" %></p>
<% end %>
<% if @user == current_user %>
<div><%= render :partial => 'connections' %></div>
<% end %>
<h2>Challenges attempted</h2>

Просмотреть файл

@ -1,5 +1,7 @@
github_config = YAML.load_file(File.join(Rails.root, "config/github.yml"))[Rails.env] rescue nil || {}
twitter_config = YAML.load_file(File.join(Rails.root, "config/twitter.yml"))[Rails.env] rescue nil || {}
Rails.application.config.middleware.use OmniAuth::Builder do
provider :github, github_config['client_id'], github_config['secret']
provider :twitter, twitter_config['consumer_key'], twitter_config['consumer_secret']
end

Просмотреть файл

@ -25,6 +25,9 @@ Mozchallenge::Application.routes.draw do
# end
# end
match '/auth/github/callback' => 'profiles#github'
match '/auth/twitter/callback' => 'profiles#twitter'
resource :community, :controller => :community
resources :users do

Просмотреть файл

@ -7,6 +7,19 @@ class CreateProfiles < ActiveRecord::Migration
t.text :bio
t.string :website
t.string :twitter
# GitHub Fields
t.string :github_token
t.string :github_username
t.string :github_url
t.integer :github_public_repo_count
t.integer :github_followers_count
# Twitter Fields
t.string :twitter_token
t.string :twitter_secret
t.string :twitter_username
t.timestamps
end
add_index :profiles, :username