From 4a6186f5239eb9d8dc8386b79b8fe4353efaa450 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Mon, 9 Apr 2012 14:42:22 +0200 Subject: [PATCH] Account settings --- app/controllers/sessions_controller.rb | 7 ++++-- app/controllers/users_controller.rb | 20 ++++++++++++---- app/models/user.rb | 2 ++ app/views/users/edit.html.erb | 31 ++++++++++++++++++++++++ app/views/users/new.html.erb | 33 ++++++++++++++++++++++---- app/views/users/show.html.erb | 4 ++++ config/routes.rb | 2 +- 7 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 app/views/users/edit.html.erb diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 75ca452..531a660 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index abd5d15..ef357fb 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 5093588..d02ef88 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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"] diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 0000000..8f07ffd --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,31 @@ +<%= form_for @user, :html => { :class => 'form-horizontal' } do |f| %> +
+ Account settings + +
+ <%= f.label :nickname, :class => 'control-label' %> +
+ <%= f.text_field :nickname, :class => 'text_field' %> +
+
+ +
+ <%= f.label :name, 'Real name', :class => 'control-label' %> +
+ <%= f.text_field :name, :class => 'text_field' %> +
+
+ +
+ <%= f.label :email, :class => 'control-label' %> +
+ <%= f.text_field :email, :class => 'text_field' %> +
+
+ +
+ <%= f.submit 'Save', :class => 'btn btn-primary' %> + <%= link_to 'Cancel', profile_path(@user), :class => 'btn' %> +
+
+<% end %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 4525e3a..4a8303f 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -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| %> +
+ Your new account - <%= f.input :name, :as => :hidden %> - <%= f.input :avatar_url, :as => :hidden %> +
+ <%= f.label :nickname, :class => 'control-label' %> +
+ <%= f.text_field :nickname, :class => 'text_field' %> + <%= @user.errors[:nickname].first %> +
+
- <%= f.button :submit %> +
+ <%= f.label :name, 'Real name', :class => 'control-label' %> +
+ <%= f.text_field :name, :class => 'text_field' %> +
+
+ +
+ <%= f.label :email, :class => 'control-label' %> +
+ <%= f.text_field :email, :class => 'text_field' %> +
+
+ +
+ <%= f.submit 'Create', :class => 'btn btn-primary' %> +
+
<% end %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index a079b05..3434108 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -7,6 +7,10 @@

<%= @user.asciicasts.count %> asciicasts

+ + <% if current_user %> +

<%= link_to 'Settings', edit_user_path %>

+ <% end %>