Permalink
Browse files

Use to_json method to convert hash to js options

  • Loading branch information...
1 parent 9be77c7 commit 3c10146d3b95a6ef731bc0d657a7185e3a96889f @galetahub committed May 14, 2012
Showing with 32 additions and 38 deletions.
  1. +7 −0 Rakefile
  2. +19 −32 lib/ckeditor/utils.rb
  3. +6 −6 test/functional/posts_controller_test.rb
View
@@ -46,6 +46,13 @@ Rake::TestTask.new("test:models") do |t|
t.verbose = false
end
+Rake::TestTask.new("test:functional") do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/functional/*_test.rb'
+ t.verbose = false
+end
+
task :default => :test
RDoc::Task.new do |rdoc|
View
@@ -1,6 +1,18 @@
# encoding: utf-8
+require 'active_support/json/encoding'
+
module Ckeditor
module Utils
+ class JavascriptCode < String
+ def to_json(options = nil)
+ self
+ end
+
+ def as_json(options = nil)
+ ActiveSupport::JSON::Variable.new(to_s).freeze
+ end
+ end
+
class << self
def escape_single_quotes(str)
str.gsub('\\','\0\0').gsub('</','<\/').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
@@ -14,14 +26,10 @@ def parameterize_filename(filename)
end
def js_replace(dom_id, options = {})
- js_options = apply_options(options)
- js = ["if (CKEDITOR.instances['#{dom_id}']) {CKEDITOR.remove(CKEDITOR.instances['#{dom_id}']);}"]
+ js_options = ActiveSupport::JSON.encode(options)
- if js_options.blank?
- js << "CKEDITOR.replace('#{dom_id}');"
- else
- js << "CKEDITOR.replace('#{dom_id}', { #{js_options} });"
- end
+ js = ["if (CKEDITOR.instances['#{dom_id}']) {CKEDITOR.remove(CKEDITOR.instances['#{dom_id}']);}"]
+ js << "CKEDITOR.replace('#{dom_id}', #{js_options});"
"$(document).ready(function(){ #{js.join} });".html_safe
end
@@ -31,42 +39,21 @@ def js_fileuploader(uploader_type, options = {})
case uploader_type.to_s.downcase
when "image" then
- options[:action] = "^EDITOR.config.filebrowserImageUploadUrl"
+ options[:action] = JavascriptCode.new("EDITOR.config.filebrowserImageUploadUrl")
options[:allowedExtensions] = Ckeditor.image_file_types
when "flash" then
- options[:action] = "^EDITOR.config.filebrowserFlashUploadUrl"
+ options[:action] = JavascriptCode.new("EDITOR.config.filebrowserFlashUploadUrl")
options[:allowedExtensions] = ["swf"]
else
- options[:action] = "^EDITOR.config.filebrowserUploadUrl"
+ options[:action] = JavascriptCode.new("EDITOR.config.filebrowserUploadUrl")
options[:allowedExtensions] = Ckeditor.attachment_file_types
end
- js_options = apply_options(options)
+ js_options = ActiveSupport::JSON.encode(options)
"$(document).ready(function(){ new qq.FileUploaderInput({ #{js_options} }); });".html_safe
end
- def apply_options(options)
- str = []
-
- options.each do |key, value|
- item = case value
- when String then
- value.split(//).first == '^' ? value.slice(1..-1) : "'#{value}'"
- when Hash then
- "{ #{apply_options(value)} }"
- when Array then
- arr = value.collect { |v| "'#{v}'" }
- "[ #{arr.join(',')} ]"
- else value
- end
-
- str << %Q{"#{key}": #{item}}
- end
-
- str.sort.join(',')
- end
-
def filethumb(filename)
extname = filename.blank? ? "unknown" : File.extname(filename).gsub(/^\./, '')
image = "#{extname}.gif"
@@ -11,25 +11,25 @@ def teardown
test "include javascripts" do
get :index
-
+
assert_select "script[src=/assets/application.js]"
- assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('test_area', { "language": 'en' });!))
+ assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('test_area', {"language":"en"});!))
end
test "pass text_area with options" do
get :index
assert_select "textarea#content[name=content][cols=10][rows=20]", "Ckeditor"
- assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('content', { "language": 'en',"toolbar": 'Easy' });!))
+ assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('content', {"language":"en","toolbar":"Easy"});!))
end
test "form builder helper" do
get :new
-
+
assert_select "textarea#post_content[name='post[content]'][cols=40][rows=20]", ""
- assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('post_content', { "height": 400,"language": 'en',"width": 800 });!))
+ assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('post_content', {"language":"en","width":800,"height":400});!))
assert_select "textarea#post_info[name='post[info]'][cols=40][rows=20]", "Defaults info content"
- assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('post_info', { "language": 'en' });!))
+ assert_select "script", Regexp.new(Regexp.escape(%q!CKEDITOR.replace('post_info', {"language":"en"});!))
end
test "text_area value" do

0 comments on commit 3c10146

Please sign in to comment.