Commit dff1bd8b

mo khan <mo@mokhan.ca>
2013-07-01 18:11:59
create page to upload a new avatar
1 parent c17758f
Changed files (6)
app/controllers/avatars_controller.rb
@@ -2,9 +2,12 @@ class AvatarsController < ApplicationController
   before_filter :authenticate_user!
   before_filter :find_or_build_avatar
 
-  def create
+  def edit
+  end
+
+  def update
     @avatar.attach(params[:avatar][:avatar])
-    redirect_to(profile_path(current_user), :notice => 'Your new avatar has been uploaded.') 
+    redirect_to avatar_path(current_user), :notice => 'Your avatar has been updated.'
   end
 
   protected 
app/views/avatars/edit.html.erb
@@ -0,0 +1,1 @@
+<%= render :partial => "shared/account_nav" %>
app/views/settings/index.html.erb
@@ -63,7 +63,7 @@ $(function(){
   </div>
 </div>
 <div id="change-avatar-dialog" class="modal hide fade">
-  <%= form_tag(avatars_path(current_user), :method => "post", :multipart => true) do |f| %>
+  <%= form_tag(edit_avatar_path(current_user), :method => :put, :multipart => true) do |f| %>
     <div class="modal-header">
       <button type="button" class="close" data-dismiss="modal">x</button>
       <h3>Change My Picture</h3>
@@ -74,7 +74,7 @@ $(function(){
       </p>
       <p style="margin-left:auto; margin-right:auto;text-align:center;">
       <%= fields_for Avatar.new do |f| %>
-        <%= f.file_field :avatar, :rel => avatars_path(current_user) %>
+        <%= f.file_field :avatar, :rel => edit_avatar_path(current_user) %>
       <% end %>
       </p>
     </div>
app/views/shared/_account_nav.html.erb
@@ -3,7 +3,7 @@
     <ul class="nav nav-tabs">
       <li><%= link_to "Basic info", settings_path %></li>
       <li><%= link_to "Password", passwords_path(@user) %></li>
-      <li><%= link_to "Picture", avatar_path(@user, @user) %></li>
+      <li><%= link_to "Picture", edit_avatar_path(@user) %></li>
     </ul>
   </div>
 </div>
config/routes.rb
@@ -44,7 +44,7 @@ Cake::Application.routes.draw do
 
   resources :settings, :only => [:index, :update]
   resources :passwords, :only => [:index, :update]
-  resources :avatars, :only => [:show, :create]
+  resources :avatars, :only => [:edit, :update]
 
   ActiveAdmin.routes(self)
 
spec/controllers/avatars_controller_spec.rb
@@ -3,25 +3,37 @@ require "spec_helper"
 describe AvatarsController do
   let(:user) { FactoryGirl.create(:user) }
 
-  before { http_login(user) }
+  context "when logged in " do
+    before { http_login(user) }
 
-  describe :create do
-    context "when uploading a new avatar" do
-      let(:image) { Rack::Test::UploadedFile.new('spec/fixtures/images/gorilla.jpg', 'image/jpeg') }
+    describe :update do
+      context "when uploading a new avatar" do
+        let(:image) { Rack::Test::UploadedFile.new('spec/fixtures/images/gorilla.jpg', 'image/jpeg') }
 
-      before { post :create, :avatar => { :avatar => image } }
+        before { put :update, :id => user.id, :avatar => { :avatar => image } }
 
-      it "should save the new avatar" do
-        Avatar.last.should_not be_nil
-        Avatar.last.avatar.should_not be_blank
+        it "should save the new avatar" do
+          Avatar.last.should_not be_nil
+          Avatar.last.avatar.should_not be_blank
+        end
+
+        it "should redirect to the profile page" do
+          response.should redirect_to avatar_path(user)
+        end
+
+        it "should display a flash notice" do
+          flash[:notice].should_not be_nil
+        end
       end
+    end
 
-      it "should redirect to the profile page" do
-        response.should redirect_to(profile_path(user))
+    describe :edit do
+      before :each do
+        get :edit, :id => user.id
       end
 
-      it "should display a flash notice" do
-        flash[:notice].should_not be_nil
+      it "should display the current avatar" do
+        assigns(:avatar).should_not be_nil
       end
     end
   end