Permalink
Browse files

Merge branch 'chapter_15' into chapter_18

* chapter_15:
  Chapter 15

Conflicts:
	Gemfile
  • Loading branch information...
2 parents 6a25e12 + e64d6a0 commit 669983eb8faf471e242f0fea415ae0255314d194 @radar radar committed Oct 9, 2012
View
@@ -4,9 +4,10 @@ gem 'rails', '3.2.8'
gem 'dynamic_form', '1.1.4'
gem 'devise', '2.1.0.rc'
gem 'cancan', '1.6.7'
-gem 'paperclip', '2.7.0'
+gem 'paperclip', '2.8.0'
gem 'searcher', :git => "git://github.com/radar/searcher"
gem 'sinatra'
+gem 'omniauth-twitter', :git => 'https://github.com/arunagw/omniauth-twitter.git'
group :assets do
gem 'sass-rails', '~> 3.2.3'
View
@@ -5,6 +5,14 @@ GIT
searcher (0.0.6)
activerecord (~> 3.0)
+GIT
+ remote: https://github.com/arunagw/omniauth-twitter.git
+ revision: efa0dc10728ec41dd2297b10441146615caa4807
+ specs:
+ omniauth-twitter (0.0.13)
+ multi_json (~> 1.3)
+ omniauth-oauth (~> 1.0)
+
GEM
remote: https://rubygems.org/
specs:
@@ -55,7 +63,7 @@ GEM
xpath (~> 0.1.4)
childprocess (0.3.5)
ffi (~> 1.0, >= 1.0.6)
- cocaine (0.2.1)
+ cocaine (0.4.0)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@@ -94,6 +102,7 @@ GEM
mime (>= 0.1)
gmail_xoauth (0.3.2)
oauth (>= 0.3.6)
+ hashie (1.2.0)
highline (1.6.13)
hike (1.2.1)
i18n (0.6.0)
@@ -123,13 +132,19 @@ GEM
net-ssh (>= 1.99.1)
nokogiri (1.5.5)
oauth (0.4.6)
+ omniauth (1.1.1)
+ hashie (~> 1.2)
+ rack
+ omniauth-oauth (1.0.1)
+ oauth
+ omniauth (~> 1.0)
orm_adapter (0.0.7)
- paperclip (2.7.0)
+ paperclip (2.8.0)
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
cocaine (>= 0.0.2)
mime-types
- pg (0.14.0)
+ pg (0.14.1)
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
@@ -224,7 +239,8 @@ DEPENDENCIES
gmail (= 0.4.0)
jquery-rails
launchy
- paperclip (= 2.7.0)
+ omniauth-twitter!
+ paperclip (= 2.8.0)
pg
rails (= 3.2.8)
rspec-rails (~> 2.9)
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
@@ -0,0 +1,3 @@
+// Place all the styles related to the users/omniauth_callbacks controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
@@ -0,0 +1,10 @@
+module Users
+ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
+ def twitter
+ @user = User.find_or_create_for_twitter(env["omniauth.auth"])
+ flash[:notice] = "Signed in with Twitter successfully."
+ sign_in_and_redirect @user, :event => :authentication
+ end
+ end
+end
+
@@ -0,0 +1,2 @@
+module Users::OmniauthCallbacksHelper
+end
View
@@ -3,7 +3,7 @@ class User < ActiveRecord::Base
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
- :confirmable, :token_authenticatable
+ :confirmable, :token_authenticatable, :omniauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
@@ -14,11 +14,34 @@ class User < ActiveRecord::Base
has_many :permissions
+ def self.find_or_create_for_twitter(response)
+ data = response['extra']['user_hash']
+ if user = User.find_by_twitter_id(data["id"])
+ user
+ else # Create a user with a stub password.
+ user = User.new(:email => "twitter+#{data["id"]}@example.com",
+ :password => Devise.friendly_token[0,20])
+ user.twitter_id = data["id"]
+ user.twitter_screen_name = data["screen_name"]
+ user.twitter_display_name = data["display_name"]
+ user.confirm!
+ user
+ end
+ end
+
def self.reset_request_count!
update_all("request_count = 0", "request_count > 0")
end
def to_s
- "#{email} (#{admin? ? "Admin" : "User"})"
+ "#{display_name} (#{admin? ? "Admin" : "User"})"
+ end
+
+ def display_name
+ if twitter_id
+ "#{twitter_display_name} (@#{twitter_screen_name})"
+ else
+ email
+ end
end
end
@@ -1,4 +1,4 @@
-<h2>Permissions for <%= @user.email %></h2>
+<h2>Permissions for <%= @user.display_name %></h2>
<%= form_tag update_user_permissions_path, :method => :put do %>
<table id='permissions' cellspacing='0'>
<thead>
@@ -22,11 +22,15 @@
<li class='login-info'>
<% if user_signed_in? %>
- Signed in as <%= current_user.email %>
+ Signed in as <%= current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
<%= link_to "Sign in", new_user_session_path %>
+ Or use <%= link_to image_tag("icons/twitter_32.png"),
+ user_omniauth_authorize_path(:twitter),
+ :id => "sign_in_with_twitter" %>
+
<% end %>
</li>
</ul>
@@ -1,6 +1,6 @@
<div id='ticket'>
<h2><%= @ticket.title %></h2>
- <span id='author'>Created by <%= @ticket.user.email %></span><br>
+ <span id='author'>Created by <%= @ticket.user.display_name %></span><br>
<% authorized?("edit tickets", @project) do %>
<%= link_to "Edit Ticket", [:edit, @project, @ticket] %>
<% end %>
@@ -37,3 +37,5 @@
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end
+
+OmniAuth.config.test_mode = true
@@ -211,6 +211,7 @@
# Add a new OmniAuth provider. Check the wiki for more information on setting
# up on your models and hooks.
# config.omniauth :github, 'APP_ID', 'APP_SECRET', :scope => 'user,public_repo'
+ config.omniauth :twitter, 'K7h3TU2ODm0gYQIMwEu3dg', 'fCgqHFEep1aEyYwmG6WW0e3SregFWguDJGTPcSGJaw'
# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
View
@@ -24,7 +24,10 @@
end
end
- devise_for :users, :controllers => { :registrations => "registrations" }
+ devise_for :users, :controllers =>
+ { :registrations => "registrations",
+ :omniauth_callbacks => "users/omniauth_callbacks"
+ }
root :to => "projects#index"
@@ -68,6 +71,6 @@
:as => 'confirm_user'
put '/admin/users/:user_id/permissions',
- :to => 'admin/permissions#update',
- :as => :update_user_permissions
+ :to => 'admin/permissions#update',
+ :as => :update_user_permissions
end
@@ -0,0 +1,8 @@
+class AddTwitterFieldsToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :twitter_id, :string
+ add_column :users, :twitter_screen_name, :string
+ add_column :users, :twitter_display_name, :string
+ end
+end
+
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120814082805) do
+ActiveRecord::Schema.define(:version => 20121006065210) do
create_table "assets", :force => true do |t|
t.string "asset_file_name"
@@ -105,6 +105,9 @@
t.boolean "admin", :default => false
t.string "authentication_token"
t.integer "request_count", :default => 0
+ t.string "twitter_id"
+ t.string "twitter_screen_name"
+ t.string "twitter_display_name"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Users::OmniauthCallbacksController do
+
+end
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the Users::OmniauthCallbacksHelper. For example:
+#
+# describe Users::OmniauthCallbacksHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe Users::OmniauthCallbacksHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
@@ -0,0 +1,22 @@
+require 'spec_helper'
+
+describe 'Twitter Auth' do
+ before do
+ OmniAuth.config.mock_auth[:twitter] = {
+ "extra" => {
+ "user_hash" => {
+ "id" => '12345',
+ "screen_name" => 'twit',
+ "display_name" => "A Twit"
+ }
+ }
+ }
+ end
+
+ it "signing in with Twitter" do
+ visit '/'
+ click_link 'sign_in_with_twitter'
+ page.should have_content("Signed in with Twitter successfully.")
+ page.should have_content("Signed in as A Twit (@twit)")
+ end
+end

0 comments on commit 669983e

Please sign in to comment.