Skip to content

Commit

Permalink
add self account edit for all users
Browse files Browse the repository at this point in the history
  • Loading branch information
demental committed Dec 9, 2012
1 parent 7ce99b9 commit 51a80c2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
21 changes: 21 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,21 @@
class UsersController < ApplicationController
before_filter :authorize
before_filter :authorize_me

def edit
end

def update
if @user.update_attributes params[:user]
redirect_to edit_user_path(@user), :notice => "account updated"
else
render action: :edit
end
end

private
def authorize_me
@user = User.find(params[:id])
deny_access! if current_user.id != @user.id
end
end
12 changes: 1 addition & 11 deletions app/views/admin/users/_form.html.erb
@@ -1,15 +1,5 @@
<%= form_for [ :admin, @user ] do |f| %> <%= form_for [ :admin, @user ] do |f| %>
<%= render 'form_fields', f: f%>
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>


<fieldset> <fieldset>
<legend>Groups <small>&mdash; <%= link_to 'new group', new_admin_group_path %></small></legend> <legend>Groups <small>&mdash; <%= link_to 'new group', new_admin_group_path %></small></legend>
<%= hidden_field_tag "user[group_ids][]", nil%> <%= hidden_field_tag "user[group_ids][]", nil%>
Expand Down
9 changes: 9 additions & 0 deletions app/views/admin/users/_form_fields.html.erb
@@ -0,0 +1,9 @@
<%= f.label :username %>
<%= f.text_field :username %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %>
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Expand Up @@ -16,6 +16,9 @@
<%= link_to 'users', admin_users_path %> | <%= link_to 'users', admin_users_path %> |
<%= link_to 'groups', admin_groups_path %> | <%= link_to 'groups', admin_groups_path %> |
</span> </span>
<% else %>
<%= link_to 'My account', edit_user_path(current_user) %> |

<% end %> <% end %>
<%= link_to 'logout', logout_path %> <%= link_to 'logout', logout_path %>
</div> </div>
Expand Down
11 changes: 11 additions & 0 deletions app/views/users/edit.html.erb
@@ -0,0 +1,11 @@
<div class="page-header">
<h3>My account</h3>
</div>

<div class="span6">
<%= form_for @user do |f| %>
<%= render 'admin/users/form_fields', f: f %>
<br />
<button type="submit" class="btn">Update</button>
<% end %>
</div>
3 changes: 2 additions & 1 deletion config/routes.rb
@@ -1,4 +1,5 @@
Roswell::Application.routes.draw do Roswell::Application.routes.draw do

get 'signup', :to => 'users#new' get 'signup', :to => 'users#new'
get 'login', :to => 'sessions#new' get 'login', :to => 'sessions#new'
get 'logout', :to => 'sessions#destroy' get 'logout', :to => 'sessions#destroy'
Expand All @@ -9,7 +10,7 @@
delete '/favorites', :to => 'favorites#destroy' delete '/favorites', :to => 'favorites#destroy'


resources :sessions resources :sessions
resources :users resources :users, only: [ :edit, :update ]


namespace :accounts do namespace :accounts do
resources :generic_accounts, :path => 'generic' resources :generic_accounts, :path => 'generic'
Expand Down
41 changes: 41 additions & 0 deletions test/controllers/users_controller_test.rb
@@ -0,0 +1,41 @@
require "minitest_helper"

describe UsersController do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user }

it "refuses to edit not me" do
get :edit, id: FactoryGirl.create(:user)
assert_redirected_to login_path
end

it "accepts to edit me" do
get :edit, id: user
assert_response :success
end

it "refuses to update not me" do
post :update, id: FactoryGirl.create(:user), user: valid_attributes
assert_redirected_to login_path
end

it "updates me if attributes are valid" do
post :update, id: user, user: valid_attributes
user.reload
assert_equal 'this_is_me', user.username
assert_redirected_to user
end

it "re-renders the form if attributes are not valid" do
post :update, id: user, user: invalid_attributes
assert_response :success
end


def valid_attributes
{ username: 'this_is_me', password: 'secret_password', password_confirmation: 'secret_password' }
end
def invalid_attributes
{ username: 'this_is_me', password: 'secret_password', password_confirmation: 'oups...' }
end
end

0 comments on commit 51a80c2

Please sign in to comment.