Class: DashboardController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#req, #res

Instance Method Summary collapse

Methods inherited from ApplicationController

#boolean_param, #csrf_token, #delete_all_images_from_content, #delete_from_storage, #initialize, #params, #redirect_to, #render, #session, #validate!

Constructor Details

This class inherits a constructor from ApplicationController

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
13
# File 'app/controllers/dashboard_controller.rb', line 5

def index
  @type = FEATURES_CONFIG['type']
  @stats = {
    pages: Page.count,
    posts: Post.count,
    projects: Project.count
  }
  render 'dashboard'
end

#uploadObject

Image Upload Action



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
# File 'app/controllers/dashboard_controller.rb', line 16

def upload
  file = params['file']
  if file && file[:tempfile]
    if FEATURES_CONFIG['storage'] == 'cloudinary'
      begin
        upload = Cloudinary::Uploader.upload(file[:tempfile].path)
        url = upload['secure_url']
        @res.body << { url: url }.to_json
      rescue => e
        @res.status = 500
        @res.body << { error: e.message }.to_json
      end
    else
      filename = "#{Time.now.to_i}_#{file[:filename]}"
      target = File.join(APP_ROOT, 'public/img/uploads', filename)
      FileUtils.cp(file[:tempfile].path, target)
      @res.body << { url: "/img/uploads/#{filename}" }.to_json
    end
  else
    @res.status = 400
    @res.body << { error: "No file uploaded" }.to_json
  end
  @res.headers['Content-Type'] = 'application/json'
  throw(:halt)
end