Permalink
Browse files

Implement custom Configuration#to_javascript method.

The TinyMCE configuration is not valid JSON (since it includes
functions) so #to_json is not appropriate.
  • Loading branch information...
1 parent 84af105 commit fcb55d365a4979bec4fcaf77fa2c6476b13e2220 @spohlenz committed Feb 7, 2014
Showing with 32 additions and 18 deletions.
  1. +1 −1 Gemfile
  2. +15 −1 lib/tinymce/rails/configuration.rb
  3. +4 −4 lib/tinymce/rails/helper.rb
  4. +12 −12 spec/helpers/helper_spec.rb
View
2 Gemfile
@@ -1,6 +1,6 @@
source 'https://rubygems.org'
-gem 'rails', '3.2.13'
+gem 'rails', '3.2.16'
gem 'jquery-rails'
gem 'uglifier'
View
16 lib/tinymce/rails/configuration.rb
@@ -3,7 +3,7 @@
module TinyMCE::Rails
class Configuration
class Function < String
- def encode_json(encoder)
+ def to_javascript
self
end
end
@@ -46,6 +46,20 @@ def options_for_tinymce
result
end
+ def to_javascript
+ pairs = options_for_tinymce.inject([]) do |result, (k, v)|
+ if v.respond_to?(:to_javascript)
+ v = v.to_javascript
+ elsif v.respond_to?(:to_json)
+ v = v.to_json
+ end
+
+ result << [k, v].join(": ")
+ end
+
+ "{\n#{pairs.join(",\n")}\n}"
+ end
+
def merge(options)
self.class.new(self.options.merge(options))
end
View
8 lib/tinymce/rails/helper.rb
@@ -21,11 +21,11 @@ def tinymce(config=:default, options={})
# Returns the JavaScript code required to initialize TinyMCE.
def tinymce_javascript(config=:default, options={})
- "tinyMCE.init(#{tinymce_configuration(config, options).to_json});".html_safe
+ "tinyMCE.init(#{tinymce_configuration(config, options).to_javascript});".html_safe
end
- # Returns the TinyMCE configuration as a hash.
- # It should be converted to JSON (via #to_json) for use within JavaScript.
+ # Returns the TinyMCE configuration object.
+ # It should be converted to JavaScript (via #to_javascript) for use within JavaScript.
def tinymce_configuration(config=:default, options={})
options, config = config, :default if config.is_a?(Hash)
options.stringify_keys!
@@ -36,7 +36,7 @@ def tinymce_configuration(config=:default, options={})
base_configuration = base_configuration.fetch(config)
end
- base_configuration.merge(options).options_for_tinymce
+ base_configuration.merge(options)
end
# Includes TinyMCE javascript assets via a script tag.
View
24 spec/helpers/helper_spec.rb
@@ -31,20 +31,20 @@ module TinyMCE::Rails
result = tinymce
result.should have_selector("script")
result.should include('tinyMCE.init({')
- result.should include('"theme":"advanced"')
- result.should include('"plugins":"paste,table,fullscreen"')
+ result.should include('theme: "advanced"')
+ result.should include('plugins: "paste,table,fullscreen"')
result.should include('});')
end
it "initializes TinyMCE with passed in options" do
result = tinymce(:theme => "simple")
- result.should include('"theme":"simple"')
- result.should include('"plugins":"paste,table,fullscreen"')
+ result.should include('theme: "simple"')
+ result.should include('plugins: "paste,table,fullscreen"')
end
it "outputs function strings without quotes" do
result = tinymce(:oninit => "function() { alert('Hello'); }")
- result.should include('"oninit":function() { alert(\'Hello\'); }')
+ result.should include('oninit: function() { alert(\'Hello\'); }')
end
end
@@ -58,25 +58,25 @@ module TinyMCE::Rails
it "initializes TinyMCE with default configuration" do
result = tinymce
- result.should include('"theme":"advanced"')
- result.should include('"plugins":"paste,table"')
+ result.should include('theme: "advanced"')
+ result.should include('plugins: "paste,table"')
end
it "merges passed in options with default configuration" do
result = tinymce(:theme => "simple")
- result.should include('"theme":"simple"')
- result.should include('"plugins":"paste,table"')
+ result.should include('theme: "simple"')
+ result.should include('plugins: "paste,table"')
end
it "initializes TinyMCE with custom configuration" do
result = tinymce(:alternate)
- result.should include('"skin":"alternate"')
+ result.should include('skin: "alternate"')
end
it "merges passed in options with custom configuration" do
result = tinymce(:alternate, :theme => "simple")
- result.should include('"theme":"simple"')
- result.should include('"skin":"alternate"')
+ result.should include('theme: "simple"')
+ result.should include('skin: "alternate"')
end
it "raises an error when given an invalid configuration" do

0 comments on commit fcb55d3

Please sign in to comment.