Class: Admin::Atreides::PhotosController

Inherits:
Atreides::AdminController
  • Object
show all
Includes:
DropboxHelper, Atreides::Extendable
Defined in:
app/controllers/admin/atreides/photos_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



9
10
11
12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
52
# File 'app/controllers/admin/atreides/photos_controller.rb', line 9

def create
  if params[:dropbox_path]
    puts "Downloading from dropbox..."
    start = Time.now
    data = dropbox_client.get_file(params[:dropbox_path])
    puts "Downloaded in #{Time.now - start}..."
    filename = File.basename params[:dropbox_path]
    puts "Filename: #{filename}"
  else
    # Flash photo upload
    data = request.body.read
    filename = params[:qqfile]
  end

  extension = File.extname(filename)
  basename = File.basename(filename, extension)
  tempfile = Tempfile.new([basename, extension], :encoding => data.encoding)
  tempfile.write(data)
  tempfile.rewind

  @photo = end_of_association_chain.new
  @photo.image = tempfile

  # Features belong to the photo and not the otherway
  if photoable && photoable.is_a?(Atreides::Feature)
    feature = photoable

    # Delete any others photos unless they belong to posts
    feature.photo.destroy if feature.photo and feature.photo.photoable.nil?

    @photo.features << feature
  else
    @photo.photoable = photoable
  end

  super do |success, failure|
    success.json { render @photo.to_json }
    success.js   { render :template => "admin/atreides/photos/create.js.haml", :layout => false, :status => :ok }
    success.html { render :template => "admin/atreides/photos/create.js.haml", :layout => false, :status => :ok }
    failure.json { render @photo.errors.to_json }
    failure.js   { render :text => @photo.errors.full_messages.to_sentence }
    failure.html { render :text => @photo.errors.full_messages.to_sentence }
  end
end

#reorderObject



54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/admin/atreides/photos_controller.rb', line 54

def reorder
  if params[:photos_list].is_a?(Array)
    i = 0
    params[:photos_list].each do |id|
      Atreides::Photo.update_all({:display_order => (i+=1)}, {:id => id})
    end
    render :nothing => true, :status => :ok
  else
    render :nothing => true, :status => :error
  end
end