Skip to content

Commit

Permalink
added some comments to the new TinyMCE::Configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
cice committed Jan 9, 2010
1 parent bf61111 commit 34f6bce
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/tiny_mce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ module Base
ActionController::Base.send(:include, TinyMCE::Base)
ActionController::Base.send(:helper, TinyMCE::Helpers)
TinyMCE.install_or_update_tinymce
end
end
5 changes: 2 additions & 3 deletions lib/tiny_mce/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ def uses_tiny_mce(options = {})

# Set instance vars in the current class
proc = Proc.new do |c|
configurations = c.instance_variable_get :@tiny_mce_configurations
configurations ||= Array.new
configurations = c.instance_variable_get(:@tiny_mce_configurations) || []
configurations << configuration
c.instance_variable_set :@tiny_mce_configurations,configurations
c.instance_variable_set(:@tiny_mce_configurations,configurations)
c.instance_variable_set(:@uses_tiny_mce, true)
end

Expand Down
24 changes: 19 additions & 5 deletions lib/tiny_mce/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module TinyMCE
class Configuration
# We use this to combine options and raw_options into one class and validate
# whether options passed in by the users are valid tiny mce configuration settings.
# Also loads which options are valid, and provides an plugins attribute to allow
# more configuration options dynamicly

attr_accessor :options,:raw_options,:plugins

DEFAULT_OPTIONS = { 'mode' => 'textareas',
Expand All @@ -14,20 +19,24 @@ def initialize combined_options={}
@plugins = Array.new
end

# Parse the options file and load it into an array
# (this method is called when tiny_mce is initialized - see init.rb)
def self.load_valid_options
@@valid_options = File.open(valid_options_path) { |f| YAML.load(f.read) }
end

# Merge additional options, but don't overwrite existing
def reverse_merge_options options
@options = options.merge(@options)
end

# Merge additional options and raw_options
def add_options combined_options={}
options = combined_options[:options] || {}
raw_options = combined_options[:raw_options] || ''
raw_options = combined_options[:raw_options]

@options.merge!(options.stringify_keys)
@raw_options += raw_options
@raw_options += ",\n" + raw_options if raw_options
end

def has_plugins?
Expand All @@ -38,9 +47,11 @@ def plugins_include? plugin
@options.stringify_keys["plugins"].include? plugin
end

# Validate and merge options and raw_options into a string
# to be used for tinyMCE.init() in the raw_tiny_mce_init helper
def to_json options={},raw_options=''
merged_options = DEFAULT_OPTIONS.merge(options.stringify_keys).merge(@options.stringify_keys)
merged_raw_options = raw_options + @raw_options unless raw_options.nil?
merged_raw_options = raw_options + ",\n" + @raw_options unless raw_options.nil?

unless merged_options['plugins'].nil?
raise TinyMCEInvalidOptionType.invalid_type_of merged_options['plugins'],:for=>:plugins unless merged_options['plugins'].is_a?(Array)
Expand Down Expand Up @@ -74,7 +85,10 @@ def to_json options={},raw_options=''
def self.valid_options_path
File.join(File.dirname(__FILE__),'valid_tinymce_options.yml')
end


# Does the check to see if the option is valid. It checks the valid_options
# array (see above), checks if the start of the option name is in the plugin list
# or checks if it's an theme_advanced_container setting
def valid?(option)
option = option.to_s
@@valid_options.include?(option) ||
Expand All @@ -87,4 +101,4 @@ def self.valid_options
@@valid_options
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tiny_mce/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def self.invalid_type_of value, parameters={}
new "Invalid value of type #{value.class} passed for TinyMCE option #{parameters[:for].to_s}"
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tiny_mce/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ def include_tiny_mce_if_needed(options = {}, raw_options = '')
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/tiny_mce/spell_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def check_spelling(spell_check_text, command, lang)
return xml_response_values
end
end
end
end
4 changes: 1 addition & 3 deletions test/functional/full_tiny_mce_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,5 @@ def assert_instance_vars_set
assigns(:tiny_mce_configurations).is_a?(Array) &&
assigns(:tiny_mce_configurations).first.options == { "spellchecker_rpc_url"=> "/full_tiny_mce/spellchecker",
"plugins" => ['spellchecker'] })

end

end
end
12 changes: 7 additions & 5 deletions test/functional/multi_configuration_tiny_mce_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class MultiConfigurationTinyMCEController < ApplicationController
uses_tiny_mce :options => { :plugins => ['spellchecker'] },
:raw_options => '',
:only => ['new_page']
:only => ['new_page','edit_page']
uses_tiny_mce :options => { :plugins => ['paste','fullscreen'] },
:raw_options => '',
:only => ['edit_page']
Expand All @@ -26,7 +26,7 @@ class MultiConfigurationTinyMCEController < ApplicationController
# filters to ensure the tests work in most cases
class MultiConfigurationTinyMCEControllerTest < ActionController::TestCase

test "all instance variables are properly set on new and edit" do
test "all instance variables are properly set on new" do
get :new_page
assert_response :success
assert (assigns(:uses_tiny_mce) &&
Expand All @@ -35,13 +35,17 @@ class MultiConfigurationTinyMCEControllerTest < ActionController::TestCase
assigns(:tiny_mce_configurations).is_a?(Array) &&
assigns(:tiny_mce_configurations).first.options == { "spellchecker_rpc_url"=> "/multi_configuration_tiny_mce/spellchecker",
"plugins" => ['spellchecker'] })
end

test "all instance variables are properly set on edit" do
get :edit_page
assert_response :success
assert (assigns(:uses_tiny_mce) &&
assigns(:uses_tiny_mce) == true)
assert (assigns(:tiny_mce_configurations) &&
assigns(:tiny_mce_configurations).is_a?(Array) &&
assigns(:tiny_mce_configurations).first.options == { "spellchecker_rpc_url"=> "/multi_configuration_tiny_mce/spellchecker",
"plugins" => ['spellchecker'] } &&
assigns(:tiny_mce_configurations).second.options == { "plugins" => ['paste','fullscreen'] })
end

Expand All @@ -51,6 +55,4 @@ class MultiConfigurationTinyMCEControllerTest < ActionController::TestCase
assert_nil assigns(:uses_tiny_mce)
assert_nil assigns(:tiny_mce_configurations)
end


end
end
2 changes: 1 addition & 1 deletion test/functional/slim_tiny_mce_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ def assert_instance_vars_set
# assigns(:raw_tiny_mce_options) == '')
end

end
end
2 changes: 1 addition & 1 deletion test/unit/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ class ConfigurationTest < ActiveSupport::TestCase
assert !configuration.valid?('theme_advanced_container_[content]_class')
end

end
end
2 changes: 1 addition & 1 deletion test/unit/helpers/tiny_mce_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,4 @@ def default_tiny_mce_javascript
"<script type=\"text/javascript\">\n//<![CDATA[\ntinyMCE.init({\neditor_selector : 'mceEditor',\nlanguage : 'en',\nmode : 'textareas',\ntheme : 'simple'\n\n});\n//]]>\n</script>"
end

end
end

0 comments on commit 34f6bce

Please sign in to comment.