lib/ckeditor/asset_response.rb in ckeditor-4.1.6 vs lib/ckeditor/asset_response.rb in ckeditor-4.2.0
- old
+ new
@@ -20,40 +20,84 @@
def file
!(ckeditor? || json?) ? params[:qqfile] : params[:upload]
end
+ def current_mode
+ @current_mode ||= extract_mode
+ end
+
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>"
- }
+ send("success_#{current_mode}", relative_url_root)
+ end
+
+ def errors
+ send("errors_#{current_mode}")
+ end
+
+ private
+
+ def success_json(_relative_url_root = nil)
+ {
+ json: { uploaded: 1, fileName: asset.filename, url: asset.url }.to_json
+ }
+ end
+
+ def success_ckeditor(relative_url_root = nil)
+ {
+ text: %"<script type='text/javascript'>
+ window.parent.CKEDITOR.tools.callFunction(#{params[:CKEditorFuncNum]}, '#{asset_url(relative_url_root)}');
+ </script>"
+ }
+ end
+
+ def success_default(_relative_url_root = nil)
+ {
+ json: asset.to_json(only: [:id, :type])
+ }
+ end
+
+ def errors_json
+ {
+ json: { uploaded: 0, error: { message: error_message } }.to_json
+ }
+ end
+
+ def errors_ckeditor
+ {
+ text: %"<script type='text/javascript'>
+ window.parent.CKEDITOR.tools.callFunction(#{params[:CKEditorFuncNum]}, null, '#{error_message}');
+ </script>"
+ }
+ end
+
+ def errors_default
+ {
+ json: { message: error_message }.to_json
+ }
+ end
+
+ def error_message
+ Ckeditor::Utils.escape_single_quotes(asset.errors.full_messages.first)
+ end
+
+ def asset_url(relative_url_root)
+ url = Ckeditor::Utils.escape_single_quotes(asset.url_content)
+
+ if URI(url).relative?
+ "#{relative_url_root}#{url}"
else
- {
- json: asset.to_json(only: [:id, :type])
- }
+ url
end
end
- def errors
+ def extract_mode
if json?
- {
- json: { "uploaded" => 0, "error" => { "message" => "Upload failed"} }.to_json
- }
+ :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>"
- }
+ :ckeditor
else
- {nothing: true, format: :json}
+ :default
end
end
end
-end
\ No newline at end of file
+end