Class: Georgia::MediaController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_ability, #current_locale

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
# File 'app/controllers/georgia/media_controller.rb', line 18

def create
  begin
    @assets = params[:assets].map{|asset| CreateMediaAsset.new(asset).call}
    @assets = Ckeditor::AssetDecorator.decorate_collection(@assets)
  rescue ArgumentError => ex
    flash.now[:alert] = ex.message
  end
  render layout: false
end

#destroyObject

Destroy multiple assets



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/georgia/media_controller.rb', line 52

def destroy
  @assets = Ckeditor::Asset.where(id: params[:id])
  if can?(:destroy, Ckeditor::Asset) and @assets.destroy_all
    respond_to do |format|
      format.html { redirect_to search_media_index_path, notice: "Assets were successfully deleted." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to search_media_index_path, alert: "Oups. Something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end

#downloadObject

Download multiple assets as a zip file TODO: We could send via AJAX with jquery.fileDownload plugin We could then have a download spinner while the request is processing, even a progress bar



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/georgia/media_controller.rb', line 70

def download
  ids = params[:ids].split(',')
  @files = Ckeditor::Asset.find(ids)
  t = Tempfile.new("tmp-zip-#{Time.now.to_i}")
  Zip::OutputStream.open(t.path) do |zos|
    @files.each do |file|
      filename = file.filename
      zos.put_next_entry(filename)
      tmp_file = Tempfile.new(filename)
      open(file.url) do |data|
        tmp_file.write data.read.force_encoding('UTF-8')
      end
      zos.print IO.read(tmp_file)
      tmp_file.close
    end
  end
  filename = "#{Georgia.title.try(:parameterize) || 'georgia'}_assets_#{Time.now.strftime('%Y%m%d%H%M%S')}.zip"
  t.close

  send_file t.path, type: "application/zip", disposition: 'attachment', filename: filename
end

#editObject



32
33
34
# File 'app/controllers/georgia/media_controller.rb', line 32

def edit
  @asset = Ckeditor::Asset.find(params[:id])
end

#indexObject



8
9
10
# File 'app/controllers/georgia/media_controller.rb', line 8

def index
  redirect_to action: :search
end

#searchObject



12
13
14
15
16
# File 'app/controllers/georgia/media_controller.rb', line 12

def search
  @asset = Ckeditor::Asset.new
  @search = Georgia::Indexer.search(Ckeditor::Asset, params)
  @assets = Ckeditor::AssetDecorator.decorate_collection(@search.results)
end

#showObject



28
29
30
# File 'app/controllers/georgia/media_controller.rb', line 28

def show
  redirect_to edit_media_path(id: params[:id])
end

#updateObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/georgia/media_controller.rb', line 36

def update
  @asset = Ckeditor::Asset.find(params[:id])
  if @asset.update_attributes(params[:asset])
    respond_to do |format|
      format.html { redirect_to edit_media_path(@asset), notice: "Asset was successfully updated." }
      format.js { head :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_to edit_media_path(@asset), alert: "Oups. Something went wrong." }
      format.js { head :internal_server_error }
    end
  end
end