[logaling-commit] logaling/logaling-server [use-username-when-display-resource-on-service] Use username when display resource on service

アーカイブの一覧に戻る

SHIMADA Koji null+****@clear*****
Fri Oct 5 13:12:19 JST 2012


SHIMADA Koji	2012-10-05 13:12:19 +0900 (Fri, 05 Oct 2012)

  New Revision: 4c54b90bb307b85b6aed37abd54db09ede34bdaf
  https://github.com/logaling/logaling-server/commit/4c54b90bb307b85b6aed37abd54db09ede34bdaf

  Log:
    Use username when display resource on service

  Added files:
    app/controllers/users_controller.rb
    app/views/users/_form.html.haml
    app/views/users/new.html.haml
  Modified files:
    app/controllers/application_controller.rb
    app/controllers/sessions_controller.rb
    app/models/additional_information_as_search_results.rb
    app/models/user.rb
    app/views/shared/_term.html.haml
    config/locales/translation_ja.yml
    config/routes.rb
    features/step_definitions/signup_steps.rb

  Modified: app/controllers/application_controller.rb (+1 -1)
===================================================================
--- app/controllers/application_controller.rb    2012-10-05 10:49:22 +0900 (35d9a19)
+++ app/controllers/application_controller.rb    2012-10-05 13:12:19 +0900 (c4f3ce7)
@@ -21,7 +21,7 @@ class ApplicationController < ActionController::Base
   end
 
   def valid_user?
-    if current_user.id != params[:user_id].to_i
+    if current_user.name != params[:user_id]
       redirect_to dashboard_path, notice: "不正なアクセスです"
     end
   end

  Modified: app/controllers/sessions_controller.rb (+8 -3)
===================================================================
--- app/controllers/sessions_controller.rb    2012-10-05 10:49:22 +0900 (c319532)
+++ app/controllers/sessions_controller.rb    2012-10-05 13:12:19 +0900 (5358288)
@@ -4,9 +4,14 @@ class SessionsController < ApplicationController
 
   def create
     auth = request.env['omniauth.auth']
-    user = User.find_by_provider_and_uid(auth[:provider], auth[:uid].to_s) || User.create_with_omniauth!(auth)
-    session[:user_id] = user.id
-    redirect_to dashboard_url
+    user = User.find_by_provider_and_uid(auth[:provider], auth[:uid].to_s)
+    if user
+      session[:user_id] = user.id
+      redirect_to dashboard_url
+    else
+      session[:user_info] = { provider: auth.provider, uid: auth.uid, name: auth.info.nickname }
+      redirect_to :signup
+    end
   end
 
   def destroy

  Added: app/controllers/users_controller.rb (+21 -0) 100644
===================================================================
--- /dev/null
+++ app/controllers/users_controller.rb    2012-10-05 13:12:19 +0900 (464d966)
@@ -0,0 +1,21 @@
+#coding: utf-8
+class UsersController < ApplicationController
+  def new
+    @user = User.new(session[:user_info])
+  end
+
+  def create
+    @user = User.new(params[:user]) do |u|
+      u.provider = session[:user_info][:provider]
+      u.uid = session[:user_info][:uid]
+    end
+
+    if****@user*****
+      session[:user_id] =****@user*****
+      session[:user_info] = nil
+      redirect_to dashboard_url
+    else
+      render :new
+    end
+  end
+end

  Modified: app/models/additional_information_as_search_results.rb (+6 -1)
===================================================================
--- app/models/additional_information_as_search_results.rb    2012-10-05 10:49:22 +0900 (4a3375a)
+++ app/models/additional_information_as_search_results.rb    2012-10-05 13:12:19 +0900 (807f6ba)
@@ -26,6 +26,11 @@ module AdditionalInformationAsSearchResults
   def glossary_name_without_user_id
     split_glossary_name_to_user_id_and_name[1]
   end
-
   alias_method :glossary_name_without_github, :glossary_name_without_user_id
+
+  def decorated_glossary_name
+    user_id, glossary_name = split_glossary_name_to_user_id_and_name
+    name = User.where(id: user_id.to_i).first.name
+    "#{name}-#{glossary_name}"
+  end
 end

  Modified: app/models/user.rb (+10 -0)
===================================================================
--- app/models/user.rb    2012-10-05 10:49:22 +0900 (19c6ed6)
+++ app/models/user.rb    2012-10-05 13:12:19 +0900 (a4f67ca)
@@ -1,3 +1,4 @@
+#coding: utf-8
 class User < ActiveRecord::Base
   class << self
     def create_with_omniauth!(auth)
@@ -16,6 +17,15 @@ class User < ActiveRecord::Base
   has_many :github_projects, :through => :memberships, :uniq => true
   has_one :user_config, :dependent => :destroy
 
+  validates_presence_of :name, :uid, :provider
+  validates_uniqueness_of :name
+  validates_format_of :name, with: /^[a-zA-Z0-9_-]+$/,
+                             message: "には英数字とハイフン (-)、アンダースコア (_) が使えます"
+
+  def to_param
+    name
+  end
+
   def priority_glossary
     user_config ? user_config.glossary : nil
   end

  Modified: app/views/shared/_term.html.haml (+1 -1)
===================================================================
--- app/views/shared/_term.html.haml    2012-10-05 10:49:22 +0900 (0480198)
+++ app/views/shared/_term.html.haml    2012-10-05 13:12:19 +0900 (1329bab)
@@ -17,7 +17,7 @@
           = link_to term.glossary_name_without_github, '#'
       - elsif term.user_glossary?
         %p.logalingProject
-          = link_to term.glossary_name, '#'
+          = link_to term.decorated_glossary_name, '#'
           - if signed_in?
             - user_glossary = UserGlossary.find_by_term_and_user(term, current_user)
             - if user_glossary

  Added: app/views/users/_form.html.haml (+19 -0) 100644
===================================================================
--- /dev/null
+++ app/views/users/_form.html.haml    2012-10-05 13:12:19 +0900 (3ba0ae8)
@@ -0,0 +1,19 @@
+= form_for @user, :html => { :class => 'form-horizontal' } do |f|
+  -if****@user*****?
+    #error_explanation
+      %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
+      %ul
+        -****@user*****_messages.each do |msg|
+          %li= msg
+
+  .control-group
+    = f.label :name, :class => 'control-label required'
+    .controls
+      = f.text_field :name
+      %span.help-block
+        英数字とハイフン (-)、アンダースコア (_) が使用できます。
+
+  .control-group
+    .controls
+      .btn-group.actions
+        = f.submit '登録する', :class => 'btn btn-large'

  Added: app/views/users/new.html.haml (+6 -0) 100644
===================================================================
--- /dev/null
+++ app/views/users/new.html.haml    2012-10-05 13:12:19 +0900 (35de9ce)
@@ -0,0 +1,6 @@
+- @title = "アカウントの新規登録"
+.container
+  %h2
+    = @title
+  .well
+    = render 'form'

  Modified: config/locales/translation_ja.yml (+3 -0)
===================================================================
--- config/locales/translation_ja.yml    2012-10-05 10:49:22 +0900 (e543a4b)
+++ config/locales/translation_ja.yml    2012-10-05 13:12:19 +0900 (4372b81)
@@ -3,8 +3,11 @@ ja:
     models:
       github_project: Githubプロジェクト
       user_glossary: 用語集
+      user: アカウント
 
     attributes:
+      user:
+        name: ユーザ名
       github_project:
         owner: ユーザ
         name: プロジェクト

  Modified: config/routes.rb (+2 -1)
===================================================================
--- config/routes.rb    2012-10-05 10:49:22 +0900 (7d29069)
+++ config/routes.rb    2012-10-05 13:12:19 +0900 (dad192f)
@@ -12,7 +12,7 @@ LogalingServer::Application.routes.draw do
               :only => :show
   end
 
-  resources :users, :only => [] do
+  resources :users, :only => [:create] do
     resources :github_project_memberships,
       :only => [:new, :create, :destroy]
     resources :user_glossaries,
@@ -28,6 +28,7 @@ LogalingServer::Application.routes.draw do
 
   match '/auth/:provider/callback', to: 'sessions#create'
   match '/auth/failure', to: 'sessions#failure'
+  match '/signup' => 'users#new', as: :signup
   match '/signin' => 'sessions#new', as: :signin
   match "/signout" => "sessions#destroy", :as => :signout
   match "/dashboard" => "dashboard#show", :as => :dashboard

  Modified: features/step_definitions/signup_steps.rb (+3 -2)
===================================================================
--- features/step_definitions/signup_steps.rb    2012-10-05 10:49:22 +0900 (8b7c60d)
+++ features/step_definitions/signup_steps.rb    2012-10-05 13:12:19 +0900 (78f1b02)
@@ -2,8 +2,9 @@
 
 前提 /^ユーザ"([^"]*)"でログインしている$/ do |name|
   page.reset_session!
-  OmniAuth.config.add_mock(:github, uid: SecureRandom.hex(8), info: {name: name})
-  user = Fabricate(:user, name: name, provider: 'github')
+  uid = SecureRandom.hex(8)
+  OmniAuth.config.add_mock(:github, uid: uid, info: {name: name})
+  user = Fabricate(:user, name: name, provider: 'github', uid: uid)
   visit '/auth/github'
 end
 
-------------- next part --------------
An HTML attachment was scrubbed...
ダウンロード 



More information about the logaling-commit mailing list
アーカイブの一覧に戻る