Skip to content

Commit

Permalink
Join configuration options defined as arrays according to TinyMCE docs
Browse files Browse the repository at this point in the history
  • Loading branch information
spohlenz committed May 27, 2014
1 parent bd24189 commit bd53a48
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Be sure to add to the global group, not the `assets` group. Then run `bundle ins
**2. Create a `config/tinymce.yml` file with your global configuration options:**

```yml
toolbar1: styleselect | bold italic | link image | undo redo
toolbar2: table | fullscreen
toolbar:
- styleselect | bold italic | link image | undo redo
- table | fullscreen
plugins:
- table
- fullscreen
Expand Down
30 changes: 28 additions & 2 deletions lib/tinymce/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ def self.defaults
}
end

COMMA = ",".freeze
SPACE = " ".freeze
SEMICOLON = ";".freeze

OPTION_SEPARATORS = {
"plugins" => COMMA,
"custom_elements" => COMMA,
"entities" => COMMA,
"extended_valid_elements" => COMMA,
"font_formats" => SEMICOLON,
"fontsize_formats" => COMMA,
"invalid_elements" => COMMA,
"block_formats" => SEMICOLON,
"valid_children" => COMMA,
"valid_elements" => COMMA,
"body_id" => COMMA,
"body_class" => COMMA,
"content_css" => COMMA,
"tabfocus_elements" => COMMA,
"table_clone_elements" => SPACE,
"paste_word_valid_elements" => COMMA,
"paste_webkit_styles" => SPACE,
"paste_retain_style_properties" => SPACE,
"spellchecker_languages" => COMMA
}

attr_reader :options

def initialize(options)
Expand All @@ -30,8 +56,8 @@ def options_for_tinymce
result = {}

options.each do |key, value|
if value.is_a?(Array) && value.all? { |v| v.is_a?(String) }
result[key] = value.join(",")
if OPTION_SEPARATORS[key] && value.is_a?(Array)
result[key] = value.join(OPTION_SEPARATORS[key])
elsif value.to_s.starts_with?("function(")
result[key] = Function.new(value)
else
Expand Down
17 changes: 16 additions & 1 deletion spec/lib/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,26 @@ module TinyMCE::Rails
config.options_for_tinymce["mode"].should eq("textareas")
end

it "combines arrays of strings into a single comma-separated string" do
it "returns arrays without predefined separators as normal" do
config = Configuration.new("toolbar" => ["styleselect", "bold italic"])
config.options_for_tinymce["toolbar"].should eq(["styleselect", "bold italic"])
end

it "combines arrays of plugins into a single comma-separated string" do
config = Configuration.new("plugins" => %w(paste table fullscreen))
config.options_for_tinymce["plugins"].should eq("paste,table,fullscreen")
end

it "returns string of plugins as normal" do
config = Configuration.new("plugins" => "paste,table,fullscreen")
config.options_for_tinymce["plugins"].should eq("paste,table,fullscreen")
end

it "combines arrays of font_formats into a single semicolon-separated string" do
config = Configuration.new("font_formats" => ["Andale Mono=andale mono,times", "Comic Sans MS=comic sans ms,sans-serif"])
config.options_for_tinymce["font_formats"].should eq("Andale Mono=andale mono,times;Comic Sans MS=comic sans ms,sans-serif")
end

it "works with integer values" do
config = Configuration.new("width" => 123)
config.options_for_tinymce["width"].should eq(123)
Expand Down

0 comments on commit bd53a48

Please sign in to comment.