Class: Admin::AssetsController

Inherits:
ResourceController
  • Object
show all
Defined in:
app/controllers/admin/assets_controller.rb

Constant Summary collapse

COMPRESS_FILE_TYPE =
['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'].freeze

Instance Method Summary collapse

Instance Method Details

#createObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/admin/assets_controller.rb', line 34

def create
  @assets = []
  @page_attachments = []
  compress = current_site.try(:compress).nil? ? true : current_site.compress
  asset_params[:asset][:asset].to_a.each do |uploaded_asset|
    if uploaded_asset.content_type == 'application/octet-stream'
      flash[:notice] = 'Please only upload assets that have a valid extension in the name.'
    else
      uploaded_asset = compress(uploaded_asset) if $kraken.api_key.present? && COMPRESS_FILE_TYPE.include?(uploaded_asset.content_type) && compress
      @asset = Asset.create(asset: uploaded_asset, caption: asset_params[:asset][:caption])
      if @asset.valid?
        set_owner_or_editor
        if params[:for_attachment]
          @page = Page.find_by_id(params[:page_id]) || Page.new
          @page_attachments << @page_attachment = @asset.page_attachments.build(page: @page)
        end
        @assets << @asset
      else
        error = @asset.errors.first.message
        flash[:error] = error
      end
    end
  end
  
  if asset_params[:for_attachment]
    render partial: 'admin/page_attachments/attachment', collection: @page_attachments
  else
    response_for :create
  end
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/admin/assets_controller.rb', line 8

def index
  assets = Asset.order('created_at DESC')
  @page = Page.find(params[:page_id]) if params[:page_id]

  @term = params[:search] || ''
  assets = assets.matching(@term) if @term && !@term.blank?

  @types = params[:filter] ? params[:filter].split(',') : []
  if @types.include?('all')
    params[:filter] = nil
  elsif @types.any?
    assets = assets.of_types(@types)
  end

  @assets = paginated? ? assets.paginate(pagination_parameters) : assets.all
  respond_to do |format|
    format.js do
      @page = Page.find_by_id(params[:page_id])
      render partial: 'asset_table', locals: { with_pagination: true }
    end
    format.html do
      render
    end
  end
end

#refreshObject



65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/admin/assets_controller.rb', line 65

def refresh
  if asset_params[:id]
    @asset = Asset.find(params[:id])
    @asset.asset.reprocess!
    flash[:notice] = t('clipped_extension.thumbnails_refreshed')
    redirect_to edit_admin_asset_path(@asset)
  else
    render
  end
end