make profile photo action uses :remote=>true rails ajax helpers. added photos#make_profile_photo.
This commit is contained in:
parent
c5f71184be
commit
601284a171
8 changed files with 96 additions and 49 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -8,13 +8,12 @@
|
|||
= render 'shared/author_info', :person => @photo.person, :post => @photo
|
||||
|
||||
.span-14.append-1.last
|
||||
%div{:data=>{:guid=>@photo.id}}
|
||||
#show_photo
|
||||
#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'), '#', :class => 'make_profile_photo'
|
||||
= 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"
|
||||
|
||||
|
|
|
|||
|
|
@ -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"}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 ?
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue