diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 2b282ef5c044d9cccd67b6bb30e7c20d35b105d7..5c29913cb0750b5e2153522820dd9bce427a9c3f 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 9879e971f9c75555a9a2c6a57a5dd4f4f6a65519..f37026b68805f80351004c46e383d552e4032442 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) - "<img alt='#{person.name}' class='avatar' data-person_id='#{person.id}' src='#{image_or_default(person)}' title='#{person.name}'>".html_safe + def person_image_tag(person, size=:thumb_small) + "<img alt='#{person.name}' class='avatar' data-person_id='#{person.id}' src='#{image_or_default(person, size)}' title='#{person.name}'>".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 "<a href='/people/#{person.id}'> #{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 67935ef7c70ff7ead523ccd289c2b740b8ccb466..92540c4bc055090df96505ee1ee0ca8f2e897c4e 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 e945361ce949fac6bdd025ef5f430803bd3cea51..9df98828bb883e014bb5d69bb7592b8ef75e42c2 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" - - -else - = image_tag @photo.url(:scaled_full) - - #caption - = @photo.caption + #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) + + #caption + = @photo.caption %br diff --git a/config/routes.rb b/config/routes.rb index 3f6337fd065c21134ba48f2ef0e0e4a595196219..b193b001771f9df27d1b4d5776a81897e3844cd2 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 972a16bd4029302dec03d000aafd0681a7d3f9e3..55df7d9d1652faaca40ddde2fb0fe3e6098bbff5 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 3aa3044454a1d8ce96efd196fbf775874478273d..bce815be1db636bad326058443bddba8f97cad48 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 e82f8cac27fd5f3e1299ac5315a5ff22fbd056e3..939e6ef4a9c8edf6827990687a3dce6c4685e4a6 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