Fix photo uploading in profile photos, style photo upload button better

This commit is contained in:
Raphael 2010-12-09 13:02:04 -08:00
parent b8589c457d
commit 3a4caca397
15 changed files with 182 additions and 81 deletions

View file

@ -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'

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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')

View file

@ -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("<input type='hidden' value='" + id + "' id='photo_id' name='photo_id'/>");
}else{
oldPhoto.val(id);
}
$("#profile_photo_upload").find(".avatar").fadeTo(200,1);
$("#profile_photo_upload").find(".avatar").attr("src",url);
}
});
}
window.onload = createUploader;

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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"

View file

@ -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;
}
}
});
});

View file

@ -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%

View file

@ -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

View file

@ -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

View file

@ -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