diff options
-rw-r--r-- | app/controllers/user_controller.rb | 72 | ||||
-rw-r--r-- | app/models/user.rb | 4 | ||||
-rw-r--r-- | app/views/user/profile_photo.rhtml | 6 | ||||
-rw-r--r-- | spec/models/profile_photo_spec.rb | 3 |
4 files changed, 43 insertions, 42 deletions
diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 33728b65e..bc117ce2e 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -279,6 +279,42 @@ class UserController < ApplicationController }.flatten.sort { |a,b| b[:model].created_at <=> a[:model].created_at }.first(20) end + def profile_photo + # check they are logged in (the upload photo option is anyway only available when logged in) + if authenticated_user.nil? + flash[:error] = "You need to be logged in to change your profile photo." + redirect_to frontpage_url + return + end + if params[:submitted_profile_photo].nil? + # default page + return + end + + # check for uploaded image + file_name = nil + file_content = nil + if !params[:file].nil? + file_name = params[:file].original_filename + file_content = params[:file].read + end + if file_name.nil? + flash[:error] = "Please choose a file containing your photo" + return + end + + # change user's photo + @profile_photo = ProfilePhoto.new(:data => file_content) + @user.set_profile_photo(@profile_photo) + if !@profile_photo.valid? + # error page (uses @profile_photo's error fields in view to show errors) + return + end + + flash[:notice] = "Thank you for updating your profile photo" + redirect_to user_url(@user) + end + private # Decide where we are going to redirect back to after signin/signup, and record that @@ -325,41 +361,5 @@ class UserController < ApplicationController render :action => 'confirm' # must be same as for send_confirmation_mail above to avoid leak of presence of email in db end - def profile_photo - raise 'boo" - # check they are logged in (the upload photo option is anyway only available when logged in) - if authenticated_user.nil? - flash[:error] = "You need to be logged in to change your profile photo." - redirect_to frontpage_url - return - end - if params[:submitted_profile_photo].nil? - # default page - return - end - - # check for uploaded image - file_name = nil - file_content = nil - if params[:file].class.to_s == "ActionController::UploadedTempfile" - file_name = params[:file].original_filename - file_content = params[:file].read - end - if file_name.nil? - flash[:error] = "Please type a message and/or choose a file containing your response." - return - end - - # change user's photo - new_profile_photo = ProfilePhoto.new(:data => params[:data]) - if !new_profile_photo.valid? - # error page - return - end - @user.set_profile_photo(new_profile_photo) - - flash[:notice] = "Thank you for updating your profile photo" - redirect_to user_url(@user) - end end diff --git a/app/models/user.rb b/app/models/user.rb index e0698a47f..bcad6229f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -275,7 +275,9 @@ class User < ActiveRecord::Base def set_profile_photo(new_profile_photo) ActiveRecord::Base.transaction do if !self.profile_photo.nil? - self.profile_photo.destroy + old_profile_photo = self.profile_photo + self.profile_photo = nil + old_profile_photo.destroy end new_profile_photo.user = self self.profile_photo = new_profile_photo diff --git a/app/views/user/profile_photo.rhtml b/app/views/user/profile_photo.rhtml index 5b0eb5b56..3b227e6c2 100644 --- a/app/views/user/profile_photo.rhtml +++ b/app/views/user/profile_photo.rhtml @@ -6,16 +6,18 @@ <h2>Change your profile photo</h2> +<%= foi_error_messages_for :profile_photo %> + <div id="profile_photo"> -<% form_tag '', :html => { :id => 'profile_photo_form' }, :multipart => true do %> +<% form_tag 'profile_photo', :html => { :id => 'profile_photo_form' }, :multipart => true do %> <p> <label class="form_label" for="file_1">Photo of you:</label> <%= file_field_tag :file, :size => 35 %> </p> <p><strong>Privacy note:</strong> Your photo will be shown in public on the Internet, - everywhere you do something on WhatDoTheyKnow. + wherever you do something on WhatDoTheyKnow. <p> <%= hidden_field_tag 'submitted_profile_photo', 1 %> diff --git a/spec/models/profile_photo_spec.rb b/spec/models/profile_photo_spec.rb index 51de45928..af58d0274 100644 --- a/spec/models/profile_photo_spec.rb +++ b/spec/models/profile_photo_spec.rb @@ -3,9 +3,6 @@ require File.dirname(__FILE__) + '/../spec_helper' describe ProfilePhoto, "when constructing a new photo" do before do - #@request_event = mock_model(InfoRequestEvent, :xapian_mark_needs_index => true) - #@request = mock_model(InfoRequest, :info_request_events => [@request_event]) - #@user = mock_model(User) end it 'should take no image as invalid' do |