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
|
||||||
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
|
def new
|
||||||
@photo = Photo.new
|
@photo = Photo.new
|
||||||
respond_with @photo
|
respond_with @photo
|
||||||
|
|
|
||||||
|
|
@ -87,16 +87,20 @@ module ApplicationHelper
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def owner_image_tag
|
def profile_photo(person)
|
||||||
person_image_tag(current_user.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
|
end
|
||||||
|
|
||||||
def owner_image_link
|
def owner_image_link
|
||||||
person_image_link(current_user.person)
|
person_image_link(current_user.person)
|
||||||
end
|
end
|
||||||
|
|
||||||
def person_image_tag(person)
|
def person_image_tag(person, size=:thumb_small)
|
||||||
"<img alt='#{person.name}' class='avatar' data-person_id='#{person.id}' src='#{image_or_default(person)}' title='#{person.name}'>".html_safe
|
"<img alt='#{person.name}' class='avatar' data-person_id='#{person.id}' src='#{image_or_default(person, size)}' title='#{person.name}'>".html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
def person_link(person)
|
def person_link(person)
|
||||||
|
|
@ -119,7 +123,7 @@ module ApplicationHelper
|
||||||
def person_image_link(person, opts = {})
|
def person_image_link(person, opts = {})
|
||||||
return "" if person.nil?
|
return "" if person.nil?
|
||||||
if opts[:to] == :photos
|
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
|
else
|
||||||
"<a href='/people/#{person.id}'>
|
"<a href='/people/#{person.id}'>
|
||||||
#{person_image_tag(person)}
|
#{person_image_tag(person)}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
|
|
||||||
#profile_photo_upload
|
#profile_photo_upload
|
||||||
= owner_image_tag
|
= owner_image_tag(:thumb_medium)
|
||||||
= form.file_field :image
|
= form.file_field :image
|
||||||
|
|
||||||
-if !@aspect.nil? && @aspect != :getting_started
|
-if !@aspect.nil? && @aspect != :getting_started
|
||||||
|
|
|
||||||
|
|
@ -8,21 +8,20 @@
|
||||||
= render 'shared/author_info', :person => @photo.person, :post => @photo
|
= render 'shared/author_info', :person => @photo.person, :post => @photo
|
||||||
|
|
||||||
.span-14.append-1.last
|
.span-14.append-1.last
|
||||||
%div{:data=>{:guid=>@photo.id}}
|
#show_photo{:data=>{:guid=>@photo.id}}
|
||||||
#show_photo
|
-if @ownership
|
||||||
-if @ownership
|
= image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden"
|
||||||
= image_tag 'ajax-loader.gif', :id => "photo_spinner", :class => "hidden"
|
= image_tag @photo.url(:scaled_full)
|
||||||
= 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)}"}}
|
||||||
.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('.make_profile_photo'), '#', :class => 'make_profile_photo'
|
|
|
||||||
|
|
= link_to t('.edit'), '#', :id => "edit_photo_toggle"
|
||||||
= link_to t('.edit'), '#', :id => "edit_photo_toggle"
|
|
||||||
|
|
||||||
-else
|
-else
|
||||||
= image_tag @photo.url(:scaled_full)
|
= image_tag @photo.url(:scaled_full)
|
||||||
|
|
||||||
#caption
|
#caption
|
||||||
= @photo.caption
|
= @photo.caption
|
||||||
|
|
||||||
%br
|
%br
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ Diaspora::Application.routes.draw do
|
||||||
resources :status_messages, :only => [:create, :destroy, :show]
|
resources :status_messages, :only => [:create, :destroy, :show]
|
||||||
resources :comments, :except => [:index]
|
resources :comments, :except => [:index]
|
||||||
resources :requests, :except => [:edit, :update]
|
resources :requests, :except => [:edit, :update]
|
||||||
resources :photos, :except => [:index]
|
|
||||||
resources :services
|
resources :services
|
||||||
|
|
||||||
resources :people
|
resources :people
|
||||||
|
|
@ -14,10 +13,13 @@ Diaspora::Application.routes.draw do
|
||||||
resources :status_messages
|
resources :status_messages
|
||||||
resources :photos
|
resources :photos
|
||||||
end
|
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 '/auth/:provider/callback' => 'services#create'
|
||||||
|
|
||||||
|
match 'photos/make_profile_photo' => 'photos#make_profile_photo'
|
||||||
|
resources :photos, :except => [:index]
|
||||||
|
|
||||||
devise_for :users, :controllers => {:registrations => "registrations",
|
devise_for :users, :controllers => {:registrations => "registrations",
|
||||||
:password => "devise/passwords",
|
:password => "devise/passwords",
|
||||||
:invitations => "invitations"}
|
:invitations => "invitations"}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$(document).ready( function(){
|
$(document).ready( function(){
|
||||||
|
|
||||||
|
//edit photo
|
||||||
$("#edit_photo_toggle").bind('click', function(evt) {
|
$("#edit_photo_toggle").bind('click', function(evt) {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
$("#photo_edit_options").toggle();
|
$("#photo_edit_options").toggle();
|
||||||
|
|
@ -29,4 +31,31 @@ $(document).ready( function(){
|
||||||
$("#show_photo").find("img").fadeTo(200,1);
|
$("#show_photo").find("img").fadeTo(200,1);
|
||||||
$("#photo_spinner").hide();
|
$("#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){
|
$(".getting_started_box").live("click",function(evt){
|
||||||
$(this).animate({
|
$(this).animate({
|
||||||
left: parseInt($(this).css('left'),30) == 0 ?
|
left: parseInt($(this).css('left'),30) == 0 ?
|
||||||
|
|
|
||||||
|
|
@ -111,4 +111,19 @@ describe PhotosController do
|
||||||
|
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue