diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb
index 2b282ef5c..5c29913cb 100644
--- a/app/controllers/photos_controller.rb
+++ b/app/controllers/photos_controller.rb
@@ -69,6 +69,32 @@ class PhotosController < ApplicationController
end
end
+ def make_profile_photo
+ person_id = current_user.person.id
+ @photo = Photo.find_by_id_and_person_id(params[:photo_id], person_id)
+
+ if @photo
+ profile_hash = {:image_url => @photo.url(:thumb_large),
+ :image_url_medium => @photo.url(:thumb_medium),
+ :image_url_small => @photo.url(:thumb_small)}
+
+ if current_user.update_profile(profile_hash)
+ respond_to do |format|
+ format.js{ render :json => { :photo_id => @photo.id,
+ :image_url => @photo.url(:thumb_large),
+ :image_url_medium => @photo.url(:thumb_medium),
+ :image_url_small => @photo.url(:thumb_small),
+ :person_id => person_id},
+ :status => 201}
+ end
+ else
+ render :nothing => true, :status => 406
+ end
+ else
+ render :nothing => true, :status => 406
+ end
+ end
+
def new
@photo = Photo.new
respond_with @photo
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 9879e971f..f37026b68 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -87,16 +87,20 @@ module ApplicationHelper
end
end
- def owner_image_tag
- person_image_tag(current_user.person)
+ def profile_photo(person)
+ person_image_link(person, :size => :thumb_large, :to => :photos)
+ end
+
+ def owner_image_tag(size=nil)
+ person_image_tag(current_user.person, size)
end
def owner_image_link
person_image_link(current_user.person)
end
- def person_image_tag(person)
- "
".html_safe
+ def person_image_tag(person, size=:thumb_small)
+ "
".html_safe
end
def person_link(person)
@@ -119,7 +123,7 @@ module ApplicationHelper
def person_image_link(person, opts = {})
return "" if person.nil?
if opts[:to] == :photos
- link_to person_image_tag(person), person_photos_path(person)
+ link_to person_image_tag(person,opts[:size]), person_photos_path(person)
else
"
#{person_image_tag(person)}
diff --git a/app/views/people/_profile_photo_upload.html.haml b/app/views/people/_profile_photo_upload.html.haml
index 67935ef7c..92540c4bc 100644
--- a/app/views/people/_profile_photo_upload.html.haml
+++ b/app/views/people/_profile_photo_upload.html.haml
@@ -4,7 +4,7 @@
#profile_photo_upload
- = owner_image_tag
+ = owner_image_tag(:thumb_medium)
= form.file_field :image
-if !@aspect.nil? && @aspect != :getting_started
diff --git a/app/views/photos/show.html.haml b/app/views/photos/show.html.haml
index e945361ce..9df98828b 100644
--- a/app/views/photos/show.html.haml
+++ b/app/views/photos/show.html.haml
@@ -8,21 +8,20 @@
= render 'shared/author_info', :person => @photo.person, :post => @photo
.span-14.append-1.last
- %div{:data=>{:guid=>@photo.id}}
- #show_photo
- -if @ownership
- = image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden"
- = image_tag @photo.url(:scaled_full)
- .photo_options{:data=>{:actor=>"#{@photo.person.owner.id}",:actor_person=>"#{@photo.person.id}",:image_url=>"#{@photo.url(:thumb_large)}"}}
- = link_to t('.make_profile_photo'), '#', :class => 'make_profile_photo'
- |
- = link_to t('.edit'), '#', :id => "edit_photo_toggle"
+ #show_photo{:data=>{:guid=>@photo.id}}
+ -if @ownership
+ = image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden"
+ = image_tag @photo.url(:scaled_full)
+ .photo_options{:data=>{:actor=>"#{@photo.person.owner.id}",:actor_person=>"#{@photo.person.id}",:image_url=>"#{@photo.url(:thumb_large)}"}}
+ = link_to t('.make_profile_photo'), {:controller => "photos", :action => "make_profile_photo", :photo_id => @photo.id}, :remote => true, :class => 'make_profile_photo'
+ |
+ = link_to t('.edit'), '#', :id => "edit_photo_toggle"
- -else
- = image_tag @photo.url(:scaled_full)
+ -else
+ = image_tag @photo.url(:scaled_full)
- #caption
- = @photo.caption
+ #caption
+ = @photo.caption
%br
diff --git a/config/routes.rb b/config/routes.rb
index 3f6337fd0..b193b0017 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -6,7 +6,6 @@ Diaspora::Application.routes.draw do
resources :status_messages, :only => [:create, :destroy, :show]
resources :comments, :except => [:index]
resources :requests, :except => [:edit, :update]
- resources :photos, :except => [:index]
resources :services
resources :people
@@ -14,10 +13,13 @@ Diaspora::Application.routes.draw do
resources :status_messages
resources :photos
end
- match '/people/by_handle' => 'people#retrieve_remote', :as => 'person_by_handle'
+ match '/people/by_handle' => 'people#retrieve_remote', :as => 'person_by_handle'
match '/auth/:provider/callback' => 'services#create'
+ match 'photos/make_profile_photo' => 'photos#make_profile_photo'
+ resources :photos, :except => [:index]
+
devise_for :users, :controllers => {:registrations => "registrations",
:password => "devise/passwords",
:invitations => "invitations"}
diff --git a/public/javascripts/photo-show.js b/public/javascripts/photo-show.js
index 972a16bd4..55df7d9d1 100644
--- a/public/javascripts/photo-show.js
+++ b/public/javascripts/photo-show.js
@@ -4,6 +4,8 @@
*/
$(document).ready( function(){
+
+ //edit photo
$("#edit_photo_toggle").bind('click', function(evt) {
evt.preventDefault();
$("#photo_edit_options").toggle();
@@ -29,4 +31,31 @@ $(document).ready( function(){
$("#show_photo").find("img").fadeTo(200,1);
$("#photo_spinner").hide();
});
+
+ // make profile photo
+ $('.make_profile_photo').bind('ajax:loading', function(data, json, xhr) {
+ var person_id = $(this).closest(".photo_options").attr('data-actor_person');
+
+ $("img[data-person_id='"+ person_id +"']").each( function() {
+ $(this).fadeTo(200,0.3);
+ });
+ });
+
+ $('.make_profile_photo').bind('ajax:success', function(data, json, xhr) {
+ json = $.parseJSON(json);
+
+ $("img[data-person_id='"+ json['person_id'] +"']").each( function() {
+ $(this).fadeTo(200,1);
+ this.src = json['image_url_small'];
+ });
+ });
+
+ $('.make_profile_photo').bind('ajax:failure', function(data, json, xhr) {
+ var person_id = $(this).closest(".photo_options").attr('data-actor_person');
+ alert("Failed to update profile photo!");
+ $("img[data-person_id='"+ person_id +"']").each( function() {
+ $(this).fadeTo(200,1);
+ });
+ });
+
});
diff --git a/public/javascripts/view.js b/public/javascripts/view.js
index 3aa304445..bce815be1 100644
--- a/public/javascripts/view.js
+++ b/public/javascripts/view.js
@@ -114,34 +114,6 @@ $.fn.clearForm = function() {
});
};
-
-$(".make_profile_photo").live("click", function(evt){
-
- evt.preventDefault();
-
- var $this = $(this),
- $controls = $this.closest(".photo_options"),
- user_id = $controls.attr('data-actor');
- person_id = $controls.attr('data-actor_person');
- photo_url = $controls.attr('data-image_url');
-
- $("img[data-person_id='"+ person_id +"']").each( function() {
- $(this).fadeTo(200,0.3);
- });
-
- $.ajax({
- type: "PUT",
- url: '/people/'+user_id,
- data: {"person":{"profile":{ "image_url": photo_url }}},
- success: function(){
- $("img[data-person_id='"+ person_id +"']").each( function() {
- $(this).fadeTo(200,1);
- this.src = photo_url;
- });
- }
- });
-});
-
$(".getting_started_box").live("click",function(evt){
$(this).animate({
left: parseInt($(this).css('left'),30) == 0 ?
diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb
index e82f8cac2..939e6ef4a 100644
--- a/spec/controllers/photos_controller_spec.rb
+++ b/spec/controllers/photos_controller_spec.rb
@@ -111,4 +111,19 @@ describe PhotosController do
end
end
+
+ describe "#make_profile_photo" do
+
+ it 'should return a 201 on a js success' do
+ get :make_profile_photo, :photo_id => photo.id, :format => 'js'
+ response.code.should == "201"
+ end
+
+ it 'should return a 406 on failure' do
+ get :make_profile_photo, :photo_id => photo2.id
+ response.code.should == "406"
+ end
+
+ end
+
end