Class: Ckeditor::AssetResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/ckeditor/asset_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(asset, request) ⇒ AssetResponse

Returns a new instance of AssetResponse.



5
6
7
8
9
10
11
# File 'lib/ckeditor/asset_response.rb', line 5

def initialize(asset, request)
  @asset = asset
  @request = request
  @params = request.params

  @asset.data = Ckeditor::Http.normalize_param(file, @request)
end

Instance Attribute Details

#assetObject (readonly)

Returns the value of attribute asset.



3
4
5
# File 'lib/ckeditor/asset_response.rb', line 3

def asset
  @asset
end

#paramsObject (readonly)

Returns the value of attribute params.



3
4
5
# File 'lib/ckeditor/asset_response.rb', line 3

def params
  @params
end

Instance Method Details

#ckeditor?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ckeditor/asset_response.rb', line 17

def ckeditor?
  !params[:CKEditor].blank?
end

#errorsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ckeditor/asset_response.rb', line 43

def errors
  if json?
    {
      json: { "uploaded" => 0, "error" => { "message" => "Upload failed"} }.to_json
    }
  elsif ckeditor?
    {
      text: %Q"<script type='text/javascript'>
        window.parent.CKEDITOR.tools.callFunction(#{params[:CKEditorFuncNum]}, null, '#{Ckeditor::Utils.escape_single_quotes(asset.errors.full_messages.first)}');
      </script>"
    }
  else
    {nothing: true, format: :json}
  end
end

#fileObject



21
22
23
# File 'lib/ckeditor/asset_response.rb', line 21

def file
  !(ckeditor? || json?) ? params[:qqfile] : params[:upload]
end

#json?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ckeditor/asset_response.rb', line 13

def json?
  params[:responseType] == 'json'
end

#success(relative_url_root = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ckeditor/asset_response.rb', line 25

def success(relative_url_root = nil)
  if json?
    {
      json: { "uploaded" => 1, "fileName" => asset.filename, "url" => asset.url }.to_json
    }
  elsif ckeditor?
    {
      text: %Q"<script type='text/javascript'>
        window.parent.CKEDITOR.tools.callFunction(#{params[:CKEditorFuncNum]}, '#{relative_url_root}#{Ckeditor::Utils.escape_single_quotes(asset.url_content)}');
      </script>"
    }
  else
    {
      json: asset.to_json(only: [:id, :type])
    }
  end
end