Class: Admin::AssetsController

Inherits:
ResourceController 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 Attribute Summary

Attributes inherited from ApplicationController

#cache, #pagination_parameters, #trusty_config

Instance Method Summary collapse

Methods inherited from ResourceController

#destroy, model_class, paginate_models, #paginated?, #pagination_parameters, #will_paginate_options

Methods included from TrustyCms::ResourceResponses

#create_responses, extended

Methods inherited from ApplicationController

#after_sign_in_path_for, #initialize, #template_name

Methods included from LoginSystem

included

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/admin/assets_controller.rb', line 49

def create
  @assets = []
  @page_attachments = []
  uploads = Array(asset_params.dig('asset', 'asset')).reject(&:blank?)


  uploads.each do |uploaded_asset|
    result = process_uploaded_asset(uploaded_asset)

    if result[:asset]
      @asset = result[:asset]
      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
      flash[result.fetch(:flash_type, :error)] = result[:error]
      @errors = result[:error]
    end
  end

  if asset_params[:for_attachment]
    render partial: 'admin/page_attachments/attachment', collection: @page_attachments
  elsif @errors.present?
    flash[:error] = @errors
    redirect_to new_admin_asset_path
  else
    response_for :create
  end
end

#indexObject



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

def index
  assets = Asset.order('created_at DESC')
  @page = Page.find(params[:page_id]) if params[:page_id]
  @term = assets.ransack(params[:search] || '')
  assets = @term.result(distinct: true)

  @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

#uploaderObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/admin/assets_controller.rb', line 31

def uploader
  @page_attachments = []
  result = process_uploaded_asset(asset_params[:upload])

  if result[:asset]
    @asset = result[:asset]
    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

    render json: { url: @asset.public_url }
  else
    flash[result.fetch(:flash_type, :error)] = result[:error]
    render json: { error: result[:error] }, status: result.fetch(:status, :unprocessable_entity)
  end
end