[fixex #4] Implement Facebook Login
[from now] 2013/04/27 18:34:44

diff --git a/Gemfile b/Gemfile
index 88dc2e3..9591fe7 100644
--- a/Gemfile
+++ b/Gemfile
@@ -107,6 +107,7 @@ gem "factory_girl_rails"
 gem "kaminari"
 gem "omniauth"
 gem "omniauth-twitter"
+gem "omniauth-facebook"
 gem 'oj'

 gem "faraday"
diff --git a/Gemfile.lock b/Gemfile.lock
index 79f50bc..44fdd36 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -237,6 +237,7 @@ GEM
       hirb (~> 0.5)
       unicode-display_width (~> 0.1.1)
     http_parser.rb (0.5.3)
+    httpauth (0.2.0)
     i18n (0.6.4)
     i18n_generators (1.2.1)
       mechanize
@@ -261,6 +262,8 @@ GEM
     jquery-turbolinks (0.2.1)
       rails (>= 3.1.0)
     json (1.7.7)
+    jwt (0.1.8)
+      multi_json (>= 1.5)
     kaminari (0.14.1)
       actionpack (>= 3.0.0)
       activesupport (>= 3.0.0)
@@ -306,13 +309,24 @@ GEM
     nokogiri (1.5.9)
     ntlm-http (0.1.1)
     oauth (0.4.7)
+    oauth2 (0.8.1)
+      faraday (~> 0.8)
+      httpauth (~> 0.1)
+      jwt (~> 0.1.4)
+      multi_json (~> 1.0)
+      rack (~> 1.2)
     oj (2.0.8)
     omniauth (1.1.4)
       hashie (>= 1.2, < 3)
       rack
+    omniauth-facebook (1.4.1)
+      omniauth-oauth2 (~> 1.1.0)
     omniauth-oauth (1.0.1)
       oauth
       omniauth (~> 1.0)
+    omniauth-oauth2 (1.1.1)
+      oauth2 (~> 0.8.0)
+      omniauth (~> 1.0)
     omniauth-twitter (0.0.16)
       multi_json (~> 1.3)
       omniauth-oauth (~> 1.0)
@@ -531,6 +545,7 @@ DEPENDENCIES
   nico_downloader!
   oj
   omniauth
+  omniauth-facebook
   omniauth-twitter
   parallel_tests
   poltergeist
diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index c00b97a..f2c957c 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -17,6 +17,8 @@
 //= require jquery.turbolinks
 //= require jquery.lazyload
 //= require jquery-fonteffect-1.0.0.min
+//= require bootstrap-dropdown
+//= require bootstrap-alert
 //= require handlebars
 //= require ember
 //= require ember-data
diff --git a/app/assets/javascripts/controllers/song_queues_index_controller.js.coffee b/app/assets/javascripts/controllers/song_queues_index_controller.js.coffee
index 54776e1..682f83a 100644
--- a/app/assets/javascripts/controllers/song_queues_index_controller.js.coffee
+++ b/app/assets/javascripts/controllers/song_queues_index_controller.js.coffee
@@ -3,7 +3,7 @@ Seirenes.SongQueuesIndexController = Ember.ArrayController.extend({
     song_queue.deleteRecord()
     song_queue.on 'didDelete', ->
       jQuery.gritter.add
-        image: '/assets/success.png'
+        image: "#{gritter_success_image_path}"
         title: 'Success'
         text: "「#{song_queue.get("title")}」を予約から削除しました"
     song_queue.store.commit()
diff --git a/app/assets/stylesheets/layout.css.scss b/app/assets/stylesheets/layout.css.scss
index e8d545f..4321d09 100644
--- a/app/assets/stylesheets/layout.css.scss
+++ b/app/assets/stylesheets/layout.css.scss
@@ -43,7 +43,7 @@ footer {
   color: #66FF66;
   position: absolute;
   top: 8px;
-  right: 10px;
+  right: 40px;
 }

 $small-menu-height: 24px;
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml
index ee3cbe0..b7790a4 100644
--- a/app/views/layouts/application.html.haml
+++ b/app/views/layouts/application.html.haml
@@ -34,16 +34,34 @@
         eirenes

     - if notice
-      .alert.alert-success= notice
+      .alert.alert-success
+        %button.close{data: {dismiss: "alert"}} &times;
+        = notice
     - if alert
-      .alert.alert-error= alert
+      .alert.alert-error
+        %button.close{data: {dismiss: "alert"}} &times;
+        = alert

     #login
       - if current_user
-        Logged in as #{current_user.screen_name} :
-        = link_to "ログアウト", logout_path
+        Logged in as #{current_user.screen_name}
+        .dropdown
+          %a.dropdown-toggle{href: "#", data: {toggle: "dropdown"}} メニュー
+          %ul.dropdown-menu{role: "menu"}
+            %li
+              %a{href: "/auth/twitter"} Twitter
+            %li
+              %a{href: "/auth/facebook"} Facebook
+            %li
+              = link_to "ログアウト", logout_path
       - else
-        %a{href: "/auth/twitter"} ログイン
+        .dropdown
+          %a.dropdown-toggle{href: "#", data: {toggle: "dropdown"}} ログイン
+          %ul.dropdown-menu{role: "menu"}
+            %li
+              %a{href: "/auth/twitter"} Twitter
+            %li
+              %a{href: "/auth/facebook"} Facebook
     #main

     %footer
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
index 69f0af8..836bf33 100644
--- a/config/initializers/omniauth.rb
+++ b/config/initializers/omniauth.rb
@@ -1,3 +1,4 @@
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :twitter, Settings.twitter_consumer_key, Settings.twitter_consumer_secret
+  provider :facebook, Settings.facebook_consumer_key, Settings.facebook_consumer_secret
 end
diff --git a/config/settings.sample.yml b/config/settings.sample.yml
index bc6e802..2d7b883 100644
--- a/config/settings.sample.yml
+++ b/config/settings.sample.yml
@@ -1,6 +1,8 @@
 defaults: &default
   twitter_consumer_key: "TWITTER CONSUMER KEY"
   twitter_consumer_secret: "TWITTER CONSUMER SECRET"
+  facebook_consumer_key: "FACEBOOK CONSUMER KEY"
+  facebook_consumer_secret: "FACEBOOK CONSUMER SECRET"
   aws_access_key_id: "AWS KEY"
   aws_secret_access_key: "AWS SECRET KEY"
   aws_region: "ap-northeast-1"

[from now] 2013/04/27 18:50:40

diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
index c377b0f..b36e8c6 100644
--- a/app/controllers/sessions_controller.rb
+++ b/app/controllers/sessions_controller.rb
@@ -1,8 +1,10 @@
 class SessionsController < ApplicationController
   def callback
     auth = request.env["omniauth.auth"]
-    @user = current_user || User.find_or_create_by_omniauth(auth)
+    @user = current_user.taap || User.find_or_create_by_omniauth(auth)
     if @user.persisted?
+      @user.update_auth(auth)
+
       session[:user_id] = @user.id
       redirect_to :root, notice: "#{@user.screen_name}としてログインしました"
     else
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index de6be79..75a6177 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -1,2 +1,17 @@
 module ApplicationHelper
+  def link_to_twitter_auth
+    if current_user.try(:bind_with?, :twitter)
+      content_tag(:span, "Twitter連携済み")
+    else
+      link_to "Twitterと連携する", "/auth/twitter"
+    end
+  end
+
+  def link_to_facebook_auth
+    if current_user.try(:bind_with?, :facebook)
+      content_tag(:span, "Facebook連携済み")
+    else
+      link_to "facebookと連携する", "/auth/facebook"
+    end
+  end
 end
diff --git a/app/models/user.rb b/app/models/user.rb
index 149d5da..84e562e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -6,7 +6,7 @@ class User < ActiveRecord::Base
   class << self
     def find_or_create_by_omniauth(auth)
       case auth.provider
-      when "twitter"
+      when "twitter", "facebook"
         user = User.joins(:user_auths).merge( UserAuth.where(provider: auth.provider, uid: auth.uid) ).first
         return user if user

@@ -20,4 +20,14 @@ class User < ActiveRecord::Base
       user
     end
   end
+
+  def update_auth(auth)
+    return nil if user_auths.where(provider: auth.provider).exists?
+
+    user_auths.create!(provider: auth.provider, uid: auth.uid)
+  end
+
+  def bind_with?(provider)
+    user_auths.where(provider: provider).exists?
+  end
 end
diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml
index b7790a4..a44a099 100644
--- a/app/views/layouts/application.html.haml
+++ b/app/views/layouts/application.html.haml
@@ -49,9 +49,9 @@
           %a.dropdown-toggle{href: "#", data: {toggle: "dropdown"}} メニュー
           %ul.dropdown-menu{role: "menu"}
             %li
-              %a{href: "/auth/twitter"} Twitter
+              = link_to_twitter_auth
             %li
-              %a{href: "/auth/facebook"} Facebook
+              = link_to_facebook_auth
             %li
               = link_to "ログアウト", logout_path
       - else

[from now] 2013/04/27 19:08:04

diff --git a/app/assets/stylesheets/layout.css.scss b/app/assets/stylesheets/layout.css.scss
index 4321d09..2effab0 100644
--- a/app/assets/stylesheets/layout.css.scss
+++ b/app/assets/stylesheets/layout.css.scss
@@ -46,6 +46,16 @@ footer {
   right: 40px;
 }

+.dropdown-menu > li > span {
+  display: block;
+  padding: 3px 20px;
+  clear: both;
+  font-weight: normal;
+  line-height: 20px;
+  color: #333333;
+  white-space: nowrap;
+}
+
 $small-menu-height: 24px;
 $middle-menu-height: 36px;
 $large-menu-height: 48px;