Class: PhotosController

Inherits:
ApplicationController show all
Defined in:
app/controllers/photos_controller.rb

Overview

Copyright © 2010-2011, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Instance Method Summary collapse

Instance Method Details

#createObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/photos_controller.rb', line 47

def create
  rescuing_photo_errors do
    if 
      @photo = current_user.build_post(:photo, photo_params)
      if @photo.save
        respond_to do |format|
          format.json { render :json => {"success" => true, "data" => @photo.as_api_response(:backbone)} }
        end
      else
        respond_with @photo, :location => photos_path, :error => message
      end
    else
      legacy_create
    end
  end
end

#destroyObject


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/photos_controller.rb', line 90

def destroy
  photo = current_user.photos.where(:id => params[:id]).first

  if photo
    current_user.retract(photo)

    respond_to do |format|
      format.json{ render :nothing => true, :status => 204 }
      format.html do
        flash[:notice] = I18n.t 'photos.destroy.notice'
        if StatusMessage.find_by_guid(photo.status_message_guid)
            respond_with photo, :location => post_path(photo.status_message)
        else
          respond_with photo, :location => person_photos_path(current_user.person)
        end
      end
    end
  else
    respond_with photo, :location => person_photos_path(current_user.person)
  end
end

#editObject


112
113
114
115
116
117
118
# File 'app/controllers/photos_controller.rb', line 112

def edit
  if @photo = current_user.photos.where(:id => params[:id]).first
    respond_with @photo
  else
    redirect_to person_photos_path(current_user.person)
  end
end

#indexObject


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/photos_controller.rb', line 20

def index
  @post_type = :photos
  @person = Person.find_by_guid(params[:person_id])

  if @person
    @contact = current_user.contact_for(@person)

    if @contact
      @contacts_of_contact = @contact.contacts
      @contacts_of_contact_count = @contact.contacts.count
    else
      @contact = Contact.new
    end

    @posts = current_user.photos_from(@person, max_time: max_time)

    respond_to do |format|
      format.all { render 'people/show' }
      format.json{ render_for_api :backbone, :json => @posts, :root => :photos }
    end

  else
    flash[:error] = I18n.t 'people.show.does_not_exist'
    redirect_to people_path
  end
end

#make_profile_photoObject


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/photos_controller.rb', line 64

def make_profile_photo
  author_id = current_user.person_id
  @photo = Photo.where(:id => params[:photo_id], :author_id => author_id).first

  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),
                                     :author_id => author_id},
                          :status => 201}
      end
    else
      render :nothing => true, :status => 422
    end
  else
    render :nothing => true, :status => 422
  end
end

#showObject

Raises:

  • (ActiveRecord::RecordNotFound)

10
11
12
13
14
15
16
17
18
# File 'app/controllers/photos_controller.rb', line 10

def show
  @photo = if user_signed_in?
    current_user.photos_from(Person.find_by_guid(params[:person_id])).where(id: params[:id]).first
  else
    Photo.where(id: params[:id], public: true).first
  end

  raise ActiveRecord::RecordNotFound unless @photo
end

#updateObject


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/photos_controller.rb', line 120

def update
  photo = current_user.photos.where(:id => params[:id]).first
  if photo
    if current_user.update_post( photo, photo_params )
      flash.now[:notice] = I18n.t 'photos.update.notice'
      respond_to do |format|
        format.js{ render :json => photo, :status => 200 }
      end
    else
      flash.now[:error] = I18n.t 'photos.update.error'
      respond_to do |format|
        format.html{ redirect_to [:edit, photo] }
        format.js{ render :status => 403 }
      end
    end
  else
    redirect_to person_photos_path(current_user.person)
  end
end