diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 9280e2d3e..fc5f42f5d 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -65,8 +65,6 @@ class PeopleController < ApplicationController params[:person][:profile][:image_url] = photo.url(:thumb_medium) end - prep_image_url(params[:person]) - if current_user.update_profile params[:person][:profile] flash[:notice] = "Profile updated" else @@ -79,22 +77,4 @@ class PeopleController < ApplicationController redirect_to edit_person_path end end - - private - def prep_image_url(params) - if params[:profile] && params[:profile][:image_url] - if params[:profile][:image_url].empty? - params[:profile].delete(:image_url) - else - url = APP_CONFIG[:pod_url].dup - url.chop! if APP_CONFIG[:pod_url][-1,1] == '/' - if params[:profile][:image_url].match(/^https?:\/\//) - params[:profile][:image_url] = params[:profile][:image_url] - else - params[:profile][:image_url] = url + params[:profile][:image_url] - end - end - end - end - end diff --git a/app/models/profile.rb b/app/models/profile.rb index b8383de17..698b858d3 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -41,10 +41,26 @@ class Profile (self._parent_document) ? self.person.diaspora_handle : self[:diaspora_handle] end + def image_url= url + return if url.nil? || url.empty? + if url.match(/^https?:\/\//) + super(url) + else + super(absolutify_local_url(url)) + end + end + protected def strip_names self.first_name.strip! if self.first_name self.last_name.strip! if self.last_name end + + private + def absolutify_local_url url + pod_url = APP_CONFIG[:pod_url].dup + pod_url.chop! if APP_CONFIG[:pod_url][-1,1] == '/' + "#{pod_url}#{url}" + end end diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index c69c0f9d5..853ab5553 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -37,21 +37,31 @@ describe PeopleController do describe '#update' do context 'with a profile photo set' do - it "doesn't overwrite the profile photo when an empty string is passed in" do + before do + @params = { :profile => + { :image_url => "", + :last_name => user.person.profile.last_name, + :first_name => user.person.profile.first_name }} + user.person.profile.image_url = "http://tom.joindiaspora.com/images/user/tom.jpg" user.person.profile.save - - params = { "profile" => - { "image" => "", - "last_name" => user.person.profile.last_name, - "first_name" => user.person.profile.first_name }} - + end + it "doesn't overwrite the profile photo when an empty string is passed in" do image_url = user.person.profile.image_url - put :update, "id" => user.person.id.to_s, "person" => params + put :update, :id => user.person.id.to_s, :person => @params 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, :id => user.person.id, :person => @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/models/profile_spec.rb b/spec/models/profile_spec.rb index 9e54cf6c7..7c9bfa5a1 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -42,6 +42,30 @@ describe Profile do end end + describe '#image_url=' do + before do + @user = make_user + @profile = @user.person.profile + fixture_name = File.dirname(__FILE__) + '/../fixtures/button.png' + @photo = @user.post(:photo, :user_file => File.open(fixture_name), :to => 'all') + @profile.image_url = "http://tom.joindiaspora.com/images/user/tom.jpg" + @pod_url = (APP_CONFIG[:pod_url][-1,1] == '/' ? APP_CONFIG[:pod_url].chop : APP_CONFIG[:pod_url]) + end + it 'ignores an empty string' do + lambda {@profile.image_url = ""}.should_not change(@profile, :image_url) + end + it 'ignores nil' do + lambda {@profile.image_url = nil}.should_not change(@profile, :image_url) + end + it 'makes relative urls absolute' do + @profile.image_url = @photo.url(:thumb_medium) + @profile.image_url.should == "#{@pod_url}#{@photo.url(:thumb_medium)}" + end + it 'accepts absolute urls' do + @profile.image_url = "#{@pod_url}#{@photo.url(:thumb_medium)}" + @profile.image_url.should == "#{@pod_url}#{@photo.url(:thumb_medium)}" + end + end describe 'serialization' do let(:person) {Factory.create(:person)}