From 3a4caca397f71bb387b82621347866bb28eff791 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 9 Dec 2010 13:02:04 -0800 Subject: [PATCH] Fix photo uploading in profile photos, style photo upload button better --- app/controllers/people_controller.rb | 13 +---- app/controllers/photos_controller.rb | 9 ++- app/models/photo.rb | 2 +- app/models/user.rb | 6 ++ app/views/people/_edit.html.haml | 5 +- app/views/photos/_new_profile_photo.haml | 67 ++++++++++++++++++++++ config/locales/diaspora/en.yml | 5 +- features/accepts_invitation.feature | 12 ++-- features/edits_profile.feature | 4 +- features/signs_up.feature | 6 +- public/javascripts/vendor/fileuploader.js | 7 +-- public/stylesheets/sass/application.sass | 58 ++++++++++--------- spec/controllers/people_controller_spec.rb | 9 --- spec/controllers/photos_controller_spec.rb | 12 +++- spec/models/user_spec.rb | 48 +++++++++++++--- 15 files changed, 182 insertions(+), 81 deletions(-) create mode 100644 app/views/photos/_new_profile_photo.haml diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index ec0926aef..4dd0b5751 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -81,18 +81,7 @@ class PeopleController < ApplicationController # upload and set new profile photo params[:profile] ||= {} params[:profile][:searchable] ||= false - if params[:profile][:image].present? - raw_image = params[:profile].delete(:image) - params[:profile_image_hash] = { :user_file => raw_image, :to => "all" } - - photo = current_user.build_post(:photo, params[:profile_image_hash]) - if photo.save! - - params[:profile][:image_url] = photo.url(:thumb_large) - params[:profile][:image_url_medium] = photo.url(:thumb_medium) - params[:profile][:image_url_small] = photo.url(:thumb_small) - end - end + params[:profile][:photo] = Photo.first(:person_id => current_user.person.id, :id => params[:photo_id]) if params[:photo_id] if current_user.update_profile params[:profile] flash[:notice] = I18n.t 'people.update.updated' diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 152f6f26d..779e1c21c 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -33,7 +33,6 @@ class PhotosController < ApplicationController def create begin - raise unless params[:photo][:aspect_ids] if params[:photo][:aspect_ids] == "all" @@ -49,6 +48,14 @@ class PhotosController < ApplicationController current_user.add_to_streams(@photo, params[:photo][:aspect_ids]) current_user.dispatch_post(@photo, :to => params[:photo][:aspect_ids]) unless @photo.pending + + if params[:photo][:set_profile_photo] + profile_params = {:image_url => @photo.url(:thumb_large), + :image_url_medium => @photo.url(:thumb_medium), + :image_url_small => @photo.url(:thumb_small)} + current_user.update_profile(profile_params) + end + respond_to do |format| format.json{ render(:layout => false , :json => {"success" => true, "data" => @photo}.to_json )} end diff --git a/app/models/photo.rb b/app/models/photo.rb index d3e993c19..3173f8719 100644 --- a/app/models/photo.rb +++ b/app/models/photo.rb @@ -22,7 +22,7 @@ class Photo < Post belongs_to :status_message - attr_accessible :caption + attr_accessible :caption, :pending validate :ownership_of_status_message before_destroy :ensure_user_picture diff --git a/app/models/user.rb b/app/models/user.rb index 364112405..438634d99 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -330,6 +330,12 @@ class User ########### Profile ###################### def update_profile(params) + if params[:photo] + params[:photo].update_attributes(:pending => false) if params[:photo].pending + params[:image_url] = params[:photo].url + params[:image_url_medium] = params[:photo].url(:thumb_medium) + params[:image_url_small] = params[:photo].url(:thumb_small) + end if self.person.profile.update_attributes(params) push_to_people profile, self.person_objects(contacts) true diff --git a/app/views/people/_edit.html.haml b/app/views/people/_edit.html.haml index 14c4c4ec0..1fa379815 100644 --- a/app/views/people/_edit.html.haml +++ b/app/views/people/_edit.html.haml @@ -3,7 +3,7 @@ -# the COPYRIGHT file. -= form_tag person_path(@person), :method => :put, :multipart => true do += form_tag person_path(@person), :method => :put, :multipart => true, :id => 'update_profile_form' do %h3 = t('people.edit.your_profile') .description @@ -33,7 +33,8 @@ %h4 = t('people.edit.your_photo') - = render 'people/profile_photo_upload' + = render 'photos/new_profile_photo' + %br %h4 = t('search') diff --git a/app/views/photos/_new_profile_photo.haml b/app/views/photos/_new_profile_photo.haml new file mode 100644 index 000000000..241d425f3 --- /dev/null +++ b/app/views/photos/_new_profile_photo.haml @@ -0,0 +1,67 @@ +-# Copyright (c) 2010, Diaspora Inc. This file is +-# licensed under the Affero General Public License version 3 or later. See +-# the COPYRIGHT file. + + +#profile_photo_upload + = owner_image_tag(:thumb_medium) + + #file-upload.button + =t('.upload') + + -if !@aspect.nil? && @aspect != :getting_started + %br + %br + %p + \...#{t('.or_select_one')} + = link_to t('_photos'), person_photos_path(@person) + + #publisher_photo_upload + :javascript + function createUploader(){ + var uploader = new qq.FileUploaderBasic({ + element: document.getElementById('file-upload'), + params: {'photo' : {'pending' : true, 'aspect_ids' : "all"}}, + allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'], + action: "#{photos_path}", + debug: true, + button: document.getElementById('file-upload'), + sizeLimit: 4194304, + + + onProgress: function(id, fileName, loaded, total){ + var progress = Math.round(loaded / total * 100 ); + $('#fileInfo').text(fileName + ' ' + progress + '%'); + }, + + messages: { + typeError: "#{t('photos.new_photo.invalid_ext')}", + sizeError: "#{t('photos.new_photo.size_error')}", + emptyError: "#{t('photos.new_photo.empty')}" + }, + + onSubmit: function(id, fileName){ + $('#file-upload').addClass("loading"); + $("#profile_photo_upload").find(".avatar").fadeTo(200,0.5); + }, + + onComplete: function(id, fileName, responseJSON){ + $('#fileInfo').text(fileName + ' completed').fadeOut(2000); + $('#file-upload').removeClass("loading"); + var id = responseJSON.data.photo.id; + var url = responseJSON.data.photo.url; + var oldPhoto = $('#photo_id'); + + if(oldPhoto.length == 0) { + $('#update_profile_form').prepend(""); + }else{ + oldPhoto.val(id); + } + + $("#profile_photo_upload").find(".avatar").fadeTo(200,1); + $("#profile_photo_upload").find(".avatar").attr("src",url); + } + + }); + } + window.onload = createUploader; diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 7b4631963..21123b46a 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -250,6 +250,9 @@ en: invalid_ext: "{file} has invalid extension. Only {extensions} are allowed." size_error: "{file} is too large, maximum file size is {sizeLimit}." empty: "{file} is empty, please select files again without it." + new_profile_photo: + upload: "Upload a new profile photo!" + or_select_one: "or select one from your already existing" registrations: new: sign_up: "Sign up" @@ -330,8 +333,6 @@ en: your_photo: "Your photo" update_profile: "Update Profile" allow_search: "Allow for people to search for you within Diaspora" - profile_photo_upload: - or_select_one: "or select one from your already existing" update: updated: "Profile updated" failed: "Failed to update profile" diff --git a/features/accepts_invitation.feature b/features/accepts_invitation.feature index ec39d54f0..7d019b613 100644 --- a/features/accepts_invitation.feature +++ b/features/accepts_invitation.feature @@ -10,9 +10,9 @@ Feature: invitation acceptance Then I should be on the getting started page And I should see "Welcome to Diaspora!" And I should see "ohai" - And I fill in "person_profile_first_name" with "O" - And I fill in "person_profile_last_name" with "Hai" - And I fill in "person_profile_gender" with "guess!" + And I fill in "profile_first_name" with "O" + And I fill in "profile_last_name" with "Hai" + And I fill in "profile_gender" with "guess!" And I press "Save and continue" Then I should see "Profile updated" And I should see "Your aspects" @@ -28,9 +28,9 @@ Feature: invitation acceptance Then I should be on the getting started page And I should see "Welcome to Diaspora!" And I should see "ohai" - And I fill in "person_profile_first_name" with "O" - And I fill in "person_profile_last_name" with "Hai" - And I fill in "person_profile_gender" with "guess!" + And I fill in "profile_first_name" with "O" + And I fill in "profile_last_name" with "Hai" + And I fill in "profile_gender" with "guess!" And I press "Save and continue" Then I should see "Profile updated" And I should see "Your aspects" diff --git a/features/edits_profile.feature b/features/edits_profile.feature index c21e177d6..11148d9a4 100644 --- a/features/edits_profile.feature +++ b/features/edits_profile.feature @@ -6,8 +6,8 @@ Feature: editing your profile And I click on my name in the header And I follow "edit profile" Then I should be on my edit profile page - When I fill in "person_profile_gender" with "F" + When I fill in "profile_gender" with "F" And I press "Update Profile" Then I should be on my edit profile page And I should see "Profile updated" - And the "person_profile_gender" field should contain "F" + And the "profile_gender" field should contain "F" diff --git a/features/signs_up.feature b/features/signs_up.feature index b91df0a3f..dab53f96b 100644 --- a/features/signs_up.feature +++ b/features/signs_up.feature @@ -12,9 +12,9 @@ Feature: new user registration And I should see "Welcome to Diaspora!" Scenario: new user goes through the setup wizard - When I fill in "person_profile_first_name" with "O" - And I fill in "person_profile_last_name" with "Hai" - And I fill in "person_profile_gender" with "guess!" + When I fill in "profile_first_name" with "O" + And I fill in "profile_last_name" with "Hai" + And I fill in "profile_gender" with "guess!" And I press "Save and continue" Then I should see "Profile updated" And I should see "Your aspects" diff --git a/public/javascripts/vendor/fileuploader.js b/public/javascripts/vendor/fileuploader.js index 89c09ebf5..7da173107 100644 --- a/public/javascripts/vendor/fileuploader.js +++ b/public/javascripts/vendor/fileuploader.js @@ -809,12 +809,11 @@ qq.UploadButton.prototype = { // the right side of the input right: 0, top: 0, - fontFamily: 'Arial', + //fontFamily: 'Arial', // 4 persons reported this, the max values that worked for them were 243, 236, 236, 118 - fontSize: '118px', + //fontSize: '118px', margin: 0, padding: 0, - cursor: 'pointer', opacity: 0 }); @@ -1244,4 +1243,4 @@ qq.extend(qq.UploadHandlerXhr.prototype, { this._xhrs[id] = null; } } -}); \ No newline at end of file +}); diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index e3ba1cb3d..d19bb7cab 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -775,32 +775,9 @@ label :shadow 0 1px 1px #eee #file-upload - :z-index 10 - :float right - :height 34px - :width 70px - :margin 0 - :background - :color #107FC9 - - :-webkit-border-radius 5px - :-moz-border-radius 5px - :border-radius 5px - - :background -webkit-gradient( linear, left bottom, left top, color-stop(0, rgb(0,123,194)), color-stop(1, rgb(65,182,250))) - :background -moz-linear-gradient( center bottom, rgb(0,123,194) 0%, rgb(65,182,250) 100%) - - :padding 9px - - :font - :size 16px - :weight bold - :color #eee - :text-shadow 0 1px 0 #333 - - &.loading - :opacity 0.5 - + input + :height 100% + :width 100% #publisher :background @@ -907,8 +884,33 @@ label :position absolute :top 5px -#file_upload - :z-index 10 + #file-upload + :z-index 10 + :float right + :height 34px + :width 70px + :margin 0 + :background + :color #107FC9 + + :-webkit-border-radius 5px + :-moz-border-radius 5px + :border-radius 5px + + :background -webkit-gradient( linear, left bottom, left top, color-stop(0, rgb(0,123,194)), color-stop(1, rgb(65,182,250))) + :background -moz-linear-gradient( center bottom, rgb(0,123,194) 0%, rgb(65,182,250) 100%) + + :padding 9px + + :font + :size 16px + :weight bold + :color #eee + :text-shadow 0 1px 0 #333 + + &.loading + :opacity 0.5 + .field_with_submit input[type='text'] :width 82% diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index 74868950a..86e0a1e82 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -183,15 +183,6 @@ describe PeopleController do user.person.reload user.person.profile.image_url.should == image_url end - it 'updates a profile photo url' do - fixture_name = File.dirname(__FILE__) + '/../fixtures/button.png' - photo = user.post(:photo, :user_file => File.open(fixture_name), :to => aspect.id) - @params[:profile][:image_url] = photo.url(:thumb_medium) - put :update, @params - goal_pod_url = (APP_CONFIG[:pod_url][-1,1] == '/' ? APP_CONFIG[:pod_url].chop : APP_CONFIG[:pod_url]) - user.person.reload.profile.image_url.should == - "#{goal_pod_url}#{photo.url(:thumb_medium)}" - end end it 'does not allow mass assignment' do new_user = make_user diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index 03f4f468d..d805ef052 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -22,16 +22,24 @@ describe PhotosController do before do connect_users(user, aspect, user2, aspect2) sign_in :user, user - @controller.stub!(:current_user).and_return(user) end describe '#create' do before do @controller.stub!(:file_handler).and_return(image) + @params = {:photo => {:user_file => image, :aspect_ids => "all"}} end it 'can make a photo' do - proc{ post :create, :photo => {:user_file => image, :aspect_ids => "all"} }.should change(Photo, :count).by(1) + proc{ + post :create, @params + }.should change(Photo, :count).by(1) + end + it 'can set the photo as the profile photo' do + old_url = user.person.profile.image_url + @params[:photo][:set_profile_photo] = true + post :create, @params + user.reload.person.profile.image_url.should_not == old_url end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 55c1105ec..bbfc62202 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -262,21 +262,51 @@ describe User do end end - context 'profiles' do - let(:updated_profile) {{ + describe '#update_profile' do + before do + @params = { :first_name => 'bob', :last_name => 'billytown', - :image_url => "http://clown.com"}} - - it 'should be able to update their profile' do - user.update_profile(updated_profile).should be true - user.reload.profile.image_url.should == "http://clown.com" + } end - it 'sends a profile to their contacts' do connect_users(user, aspect, user2, aspect2) user.should_receive(:push_to_person).once - user.update_profile(updated_profile).should be true + user.update_profile(@params).should be_true + end + it 'updates names' do + user.update_profile(@params).should be_true + user.reload.profile.first_name.should == 'bob' + end + it 'updates image_url' do + params = {:image_url => "http://clown.com"} + + user.update_profile(params).should be_true + user.reload.profile.image_url.should == "http://clown.com" + end + context 'passing in a photo' do + before do + fixture_filename = 'button.png' + fixture_name = File.join(File.dirname(__FILE__), '..', 'fixtures', fixture_filename) + image = File.open(fixture_name) + @photo = Photo.instantiate( + :person => user.person, :user_file => image) + @photo.save! + @params = {:photo => @photo} + end + it 'updates image_url' do + user.update_profile(@params).should be_true + user.reload.profile.image_url.should == @photo.absolute_url + user.profile.image_url_medium.should == @photo.absolute_url(:thumb_medium) + user.profile.image_url_small.should == @photo.absolute_url(:thumb_small) + end + it 'unpends the photo' do + @photo.pending = true + @photo.save! + @photo.reload + user.update_profile(@params).should be true + @photo.reload.pending.should be_false + end end end