Account settings

This commit is contained in:
Marcin Kulik 2012-04-09 14:42:22 +02:00
parent 26b1afff08
commit 4a6186f523
7 changed files with 87 additions and 12 deletions

View File

@ -33,8 +33,11 @@ class SessionsController < ApplicationController
end
def store_sensitive_user_data_in_session
session[:provider] = @user.provider
session[:uid] = @user.uid
session[:new_user] = {
:provider => @user.provider,
:uid => @user.uid,
:avatar_url => @user.avatar_url
}
end
end

View File

@ -1,6 +1,8 @@
class UsersController < ApplicationController
PER_PAGE = 20
before_filter :ensure_authenticated!, :only => [:edit, :update]
def show
@user = User.find_by_nickname(params[:nickname])
collection = @user.asciicasts.
@ -23,15 +25,25 @@ class UsersController < ApplicationController
end
end
def edit
@user = current_user
end
def update
current_user.update_attributes(params[:user])
redirect_to profile_path(current_user),
:notice => 'Account settings saved.'
end
private
def load_sensitive_user_data_from_session
@user.provider = session[:provider]
@user.uid = session[:uid]
@user.provider = session[:new_user][:provider]
@user.uid = session[:new_user][:uid]
@user.avatar_url = session[:new_user][:avatar_url]
end
def clear_sensitive_session_user_data
session[:provider] = nil
session[:uid] = nil
session.delete(:new_user)
end
end

View File

@ -12,6 +12,8 @@ class User < ActiveRecord::Base
has_many :asciicasts, :dependent => :destroy
has_many :comments, :dependent => :destroy
attr_accessible :nickname, :email, :name
def self.create_with_omniauth(auth)
user = new
user.provider = auth["provider"]

View File

@ -0,0 +1,31 @@
<%= form_for @user, :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<legend>Account settings</legend>
<div class="control-group">
<%= f.label :nickname, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :nickname, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :name, 'Real name', :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :email, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit 'Save', :class => 'btn btn-primary' %>
<%= link_to 'Cancel', profile_path(@user), :class => 'btn' %>
</div>
</fieldset>
<% end %>

View File

@ -1,8 +1,31 @@
<%= simple_form_for @user do |f| %>
<%= f.input :nickname, :input_html => { :class => 'special' } %>
<%= form_for @user, :url => '/user', :html => { :class => 'form-horizontal' } do |f| %>
<fieldset>
<legend>Your new account</legend>
<%= f.input :name, :as => :hidden %>
<%= f.input :avatar_url, :as => :hidden %>
<div class="control-group <%= 'error' if @user.errors[:nickname].present? %>">
<%= f.label :nickname, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :nickname, :class => 'text_field' %>
<span class="help-inline"><%= @user.errors[:nickname].first %></span>
</div>
</div>
<%= f.button :submit %>
<div class="control-group">
<%= f.label :name, 'Real name', :class => 'control-label' %>
<div class="controls">
<%= f.text_field :name, :class => 'text_field' %>
</div>
</div>
<div class="control-group">
<%= f.label :email, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :email, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit 'Create', :class => 'btn btn-primary' %>
</div>
</fieldset>
<% end %>

View File

@ -7,6 +7,10 @@
<p class="meta">
<%= @user.asciicasts.count %> asciicasts
</p>
<% if current_user %>
<p><%= link_to 'Settings', edit_user_path %></p>
<% end %>
</div>
<ul class="asciicasts">

View File

@ -16,7 +16,7 @@ AsciiIo::Application.routes.draw do
match "/connect/:user_token" => "user_tokens#create"
resource :users, :only => [:create]
resource :user, :only => [:create, :edit, :update]
namespace :api do
resources :comments