OSDN Git Service

Refactored profile to resource. Added missing flash notice on successfull updated...
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Sun, 2 Dec 2012 11:29:24 +0000 (13:29 +0200)
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Sun, 2 Dec 2012 11:29:24 +0000 (13:29 +0200)
14 files changed:
app/assets/javascripts/profile.js.coffee
app/controllers/profiles_controller.rb [moved from app/controllers/profile_controller.rb with 53% similarity]
app/views/layouts/profile.html.haml
app/views/profile/index.html.haml [deleted file]
app/views/profiles/account.html.haml [moved from app/views/profile/account.html.haml with 79% similarity]
app/views/profiles/design.html.haml [moved from app/views/profile/design.html.haml with 93% similarity]
app/views/profiles/history.html.haml [moved from app/views/profile/history.html.haml with 100% similarity]
app/views/profiles/show.html.haml [moved from app/views/profile/show.html.haml with 92% similarity]
app/views/profiles/update.js.erb [moved from app/views/profile/update.js.erb with 100% similarity]
app/views/profiles/update_username.js.haml [new file with mode: 0644]
config/routes.rb
features/steps/shared/paths.rb
spec/requests/security/profile_access_spec.rb
spec/routing/routing_spec.rb

index e536afa..42207a3 100644 (file)
@@ -8,3 +8,13 @@ $ ->
 
     # Go up the hierarchy and show the corresponding submission feedback element
     $(@).closest('fieldset').find('.update-feedback').show('highlight', {color: '#DFF0D8'}, 500)
+
+  $('.update-username form').on 'ajax:before', ->
+    $('.loading-gif').show()
+    $(this).find('.update-success').hide()
+    $(this).find('.update-failed').hide()
+
+  $('.update-username form').on 'ajax:complete', ->
+    $(this).find('.save-btn').removeAttr('disabled')
+    $(this).find('.save-btn').removeClass('disabled')
+    $(this).find('.loading-gif').hide()
similarity index 53%
rename from app/controllers/profile_controller.rb
rename to app/controllers/profiles_controller.rb
index 5f8b11f..1d1efb1 100644 (file)
@@ -1,5 +1,6 @@
-class ProfileController < ApplicationController
+class ProfilesController < ApplicationController
   before_filter :user
+  layout 'profile'
 
   def show
   end
@@ -7,8 +8,15 @@ class ProfileController < ApplicationController
   def design
   end
 
+  def account
+  end
+
   def update
-    @user.update_attributes(params[:user])
+    if @user.update_attributes(params[:user])
+      flash[:notice] = "Profile was successfully updated"
+    else
+      flash[:alert] = "Failed to update profile"
+    end
 
     respond_to do |format|
       format.html { redirect_to :back }
@@ -19,7 +27,7 @@ class ProfileController < ApplicationController
   def token
   end
 
-  def password_update
+  def update_password
     params[:user].reject!{ |k, v| k != "password" && k != "password_confirmation"}
 
     if @user.update_attributes(params[:user])
@@ -31,14 +39,25 @@ class ProfileController < ApplicationController
   end
 
   def reset_private_token
-    current_user.reset_authentication_token!
-    redirect_to profile_account_path
+    if current_user.reset_authentication_token!
+      flash[:notice] = "Token was successfully updated"
+    end
+
+    redirect_to account_profile_path
   end
 
   def history
     @events = current_user.recent_events.page(params[:page]).per(20)
   end
 
+  def update_username
+    @user.update_attributes(username: params[:user][:username])
+
+    respond_to do |format|
+      format.js
+    end
+  end
+
   private
 
   def user
index b274322..7852ed6 100644 (file)
@@ -6,17 +6,17 @@
     = render "layouts/head_panel", title: "Profile"
     .container
       %ul.main_menu
-        = nav_link(path: 'profile#show', html_options: {class: 'home'}) do
+        = nav_link(path: 'profiles#show', html_options: {class: 'home'}) do
           = link_to "Profile", profile_path
-        = nav_link(path: 'profile#account') do
-          = link_to "Account", profile_account_path
+        = nav_link(path: 'profiles#account') do
+          = link_to "Account", account_profile_path
         = nav_link(controller: :keys) do
           = link_to keys_path do
             SSH Keys
             %span.count= current_user.keys.count
-        = nav_link(path: 'profile#design') do
-          = link_to "Design", profile_design_path
-        = nav_link(path: 'profile#history') do
-          = link_to "History", profile_history_path
+        = nav_link(path: 'profiles#design') do
+          = link_to "Design", design_profile_path
+        = nav_link(path: 'profiles#history') do
+          = link_to "History", history_profile_path
 
       .content= yield
diff --git a/app/views/profile/index.html.haml b/app/views/profile/index.html.haml
deleted file mode 100644 (file)
index 84174ac..0000000
+++ /dev/null
@@ -1 +0,0 @@
-%h1 Profile
similarity index 79%
rename from app/views/profile/account.html.haml
rename to app/views/profiles/account.html.haml
index e2c5bcd..1c51f48 100644 (file)
@@ -15,7 +15,7 @@
     %span.cred.right
       keep it secret!
   .padded
-    = form_for @user, url: profile_reset_private_token_path, method: :put do |f|
+    = form_for @user, url: reset_private_token_profile_path, method: :put do |f|
       .data
         %p.slead
           Private token used to access application resources without authentication.
@@ -31,7 +31,7 @@
 
 %fieldset
   %legend Password
-  = form_for @user, url: profile_password_path, method: :put do |f|
+  = form_for @user, url: update_password_profile_path, method: :put do |f|
     .padded
       %p.slead After successful password update you will be redirected to login page where you should login with new password
       -if @user.errors.any?
 
 
 
-%fieldset
+%fieldset.update-username
   %legend
     Username
-    %small.right
+    %small.cred.right
       Changing your username can have unintended side effects!
-  = form_for @user, url: profile_update_path,  method: :put do |f|
+  = form_for @user, url: update_username_profile_path,  method: :put, remote: true do |f|
     .padded
       = f.label :username
       .input
         = f.text_field :username, required: true
+        &nbsp;
+        %span.loading-gif.hide= image_tag "ajax_loader.gif"
+        %span.update-success.cgreen.hide
+          %i.icon-ok
+          Saved
+        %span.update-failed.cred.hide
+          %i.icon-ok
+          Failed
       .input
         = f.submit 'Save username', class: "btn save-btn"
 
similarity index 93%
rename from app/views/profile/design.html.haml
rename to app/views/profiles/design.html.haml
index 502cca4..f4b5067 100644 (file)
@@ -1,4 +1,4 @@
-= form_for @user, url: profile_update_path, remote: true, method: :put do |f|
+= form_for @user, url: profile_path, remote: true, method: :put do |f|
   %fieldset.application-theme
     %legend
       Application theme
similarity index 92%
rename from app/views/profile/show.html.haml
rename to app/views/profiles/show.html.haml
index 7d9e90c..ac36fa3 100644 (file)
@@ -8,7 +8,7 @@
 
 %hr
 
-= form_for @user, url: profile_update_path, method: :put, html: { class: "edit_user form-horizontal" }  do |f|
+= form_for @user, url: profile_path, method: :put, html: { class: "edit_user form-horizontal" }  do |f|
   -if @user.errors.any?
     %div.alert-message.block-message.error
       %ul
@@ -39,9 +39,9 @@
 
           - if Gitlab.config.omniauth_enabled? && @user.provider?
             %li
-              %p.hint
+              %p
                 You can login through #{@user.provider.titleize}!
-                = link_to "click here to change", profile_account_path
+                = link_to "click here to change", account_profile_path
 
   .row
     .span7
diff --git a/app/views/profiles/update_username.js.haml b/app/views/profiles/update_username.js.haml
new file mode 100644 (file)
index 0000000..abd9026
--- /dev/null
@@ -0,0 +1,6 @@
+- if @user.valid?
+  :plain
+    $('.update-username .update-success').show();
+- else
+  :plain
+    $('.update-username .update-failed').show();
index 9c58ce1..1e24e0a 100644 (file)
@@ -69,14 +69,18 @@ Gitlab::Application.routes.draw do
   #
   # Profile Area
   #
-  get "profile/account"             => "profile#account"
-  get "profile/history"             => "profile#history"
-  put "profile/password"            => "profile#password_update"
-  get "profile/token"               => "profile#token"
-  put "profile/reset_private_token" => "profile#reset_private_token"
-  get "profile"                     => "profile#show"
-  get "profile/design"              => "profile#design"
-  put "profile/update"              => "profile#update"
+  resource :profile, only: [:show, :update] do
+    member do
+      get :account
+      get :history
+      get :token
+      get :design
+
+      put :update_password
+      put :reset_private_token
+      put :update_username
+    end
+  end
 
   resources :keys
 
index 33a9402..a125762 100644 (file)
@@ -54,7 +54,7 @@ module SharedPaths
   end
 
   Given 'I visit profile account page' do
-    visit profile_account_path
+    visit account_profile_path
   end
 
   Given 'I visit profile SSH keys page' do
@@ -62,15 +62,11 @@ module SharedPaths
   end
 
   Given 'I visit profile design page' do
-    visit profile_design_path
+    visit design_profile_path
   end
 
   Given 'I visit profile history page' do
-    visit profile_history_path
-  end
-
-  Given 'I visit profile token page' do
-    visit profile_token_path
+    visit history_profile_path
   end
 
   # ----------------------------------------
index 8562b8e..f854f3f 100644 (file)
@@ -29,7 +29,16 @@ describe "Users Security" do
     end
 
     describe "GET /profile/account" do
-      subject { profile_account_path }
+      subject { account_profile_path }
+
+      it { should be_allowed_for @u1 }
+      it { should be_allowed_for :admin }
+      it { should be_allowed_for :user }
+      it { should be_denied_for :visitor }
+    end
+
+    describe "GET /profile/design" do
+      subject { design_profile_path }
 
       it { should be_allowed_for @u1 }
       it { should be_allowed_for :admin }
index cb8dbf3..988063d 100644 (file)
@@ -82,37 +82,25 @@ end
 #                     profile GET    /profile(.:format)                     profile#show
 #              profile_design GET    /profile/design(.:format)              profile#design
 #              profile_update PUT    /profile/update(.:format)              profile#update
-describe ProfileController, "routing" do
+describe ProfilesController, "routing" do
   it "to #account" do
-    get("/profile/account").should route_to('profile#account')
+    get("/profile/account").should route_to('profiles#account')
   end
 
   it "to #history" do
-    get("/profile/history").should route_to('profile#history')
-  end
-
-  it "to #password_update" do
-    put("/profile/password").should route_to('profile#password_update')
-  end
-
-  it "to #token" do
-    get("/profile/token").should route_to('profile#token')
+    get("/profile/history").should route_to('profiles#history')
   end
 
   it "to #reset_private_token" do
-    put("/profile/reset_private_token").should route_to('profile#reset_private_token')
+    put("/profile/reset_private_token").should route_to('profiles#reset_private_token')
   end
 
   it "to #show" do
-    get("/profile").should route_to('profile#show')
+    get("/profile").should route_to('profiles#show')
   end
 
   it "to #design" do
-    get("/profile/design").should route_to('profile#design')
-  end
-
-  it "to #update" do
-    put("/profile/update").should route_to('profile#update')
+    get("/profile/design").should route_to('profiles#design')
   end
 end