From 3af2265d7bfed77126a60450b170a87571286236 Mon Sep 17 00:00:00 2001 From: Kieran Pilkington Date: Sat, 27 Feb 2010 10:28:33 +1300 Subject: [PATCH] Cleanup and bug fixes. --- lib/tiny_mce.rb | 5 -- lib/tiny_mce/base.rb | 24 ++------- lib/tiny_mce/configuration.rb | 93 +++++++++++++++-------------------- lib/tiny_mce/exceptions.rb | 4 +- lib/tiny_mce/helpers.rb | 12 ++--- 5 files changed, 53 insertions(+), 85 deletions(-) diff --git a/lib/tiny_mce.rb b/lib/tiny_mce.rb index 69768e6..6be0431 100644 --- a/lib/tiny_mce.rb +++ b/lib/tiny_mce.rb @@ -65,12 +65,7 @@ module Base end end -# Load up the available configuration options (we do it here because -# the result doesn't, so we don't want to load it per request) -TinyMCE::Configuration.load_valid_options - # Include the TinyMCE methods and TinyMCE Helpers into ActionController::Base - if defined?(Rails) && defined?(ActionController) ActionController::Base.send(:include, TinyMCE::Base) ActionController::Base.send(:helper, TinyMCE::Helpers) diff --git a/lib/tiny_mce/base.rb b/lib/tiny_mce/base.rb index 57871e7..f1d97f8 100644 --- a/lib/tiny_mce/base.rb +++ b/lib/tiny_mce/base.rb @@ -11,34 +11,20 @@ module ClassMethods # Takes options hash, raw_options string, and any normal params you # can send to a before_filter (only, except etc) def uses_tiny_mce(options = {}) - - tiny_mce_options = options.delete(:options) || {} - raw_tiny_mce_options = options.delete(:raw_options) || '' - - configuration = TinyMCE::Configuration.new :options=>tiny_mce_options,:raw_options=>raw_tiny_mce_options - - # Allow users to have default options in config/tiny_mce.yml so that - # they do not need to specify the same options over all controllers - tiny_mce_yaml_filepath = File.join(RAILS_ROOT, 'config', 'tiny_mce.yml') - if File.exist?(tiny_mce_yaml_filepath) - yaml_options = YAML::load(IO.read(tiny_mce_yaml_filepath)) rescue Hash.new - configuration.reverse_merge_options(yaml_options) if yaml_options - end + configuration = TinyMCE::Configuration.new(options.delete(:options), options.delete(:raw_options)) # If the tiny_mce plugins includes the spellchecker, then form a spellchecking path, # add it to the tiny_mce_options, and include the SpellChecking module - if configuration.has_plugins? && configuration.plugins_include?('spellchecker') - configuration.reverse_merge_options("spellchecker_rpc_url" => "/" + self.controller_name + "/spellchecker") - self.class_eval do - include TinyMCE::SpellChecker - end + if configuration.plugins.include?('spellchecker') + configuration.reverse_add_options("spellchecker_rpc_url" => "/" + self.controller_name + "/spellchecker") + self.class_eval { include TinyMCE::SpellChecker } end # Set instance vars in the current class proc = Proc.new do |c| 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 diff --git a/lib/tiny_mce/configuration.rb b/lib/tiny_mce/configuration.rb index c865cc6..a033a75 100644 --- a/lib/tiny_mce/configuration.rb +++ b/lib/tiny_mce/configuration.rb @@ -1,66 +1,65 @@ 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 + # 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', - 'editor_selector' => 'mceEditor', - 'theme' => 'simple', - 'language' => (defined?(I18n) ? I18n.locale : :en) } - - def initialize combined_options={} - @options = combined_options[:options] || {} - @options.stringify_keys! - @raw_options = [combined_options[:raw_options]] - @plugins = Array.new + DEFAULT_OPTIONS = { + 'mode' => 'textareas', + 'editor_selector' => 'mceEditor', + 'theme' => 'simple', + 'language' => (defined?(I18n) ? I18n.locale : :en) + } + + def self.config_file_options + @@config_file_options ||= begin + tiny_mce_yaml_filepath = File.join(RAILS_ROOT, 'config', 'tiny_mce.yml') + YAML::load(IO.read(tiny_mce_yaml_filepath)) rescue nil + end 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) } + def self.valid_options + @@valid_options ||= begin + valid_options_path = File.join(File.dirname(__FILE__), 'valid_tinymce_options.yml') + File.open(valid_options_path) { |f| YAML.load(f.read) } + end end - # Merge additional options, but don't overwrite existing - def reverse_merge_options options - @options = options.merge(@options) + attr_accessor :options, :raw_options + + def initialize(options = {}, raw_options = nil) + @options = DEFAULT_OPTIONS.merge(self.class.config_file_options.stringify_keys || {}). + merge(options.stringify_keys || {}) + @raw_options = [raw_options] end # Merge additional options and raw_options - def add_options combined_options={} - options = combined_options[:options] || {} - raw_options = combined_options[:raw_options] - @options.merge!(options.stringify_keys) + def add_options(options = {}, raw_options = nil) + @options.merge!(options.stringify_keys) unless options.blank? @raw_options << raw_options unless raw_options.blank? end - def has_plugins? - !@options.stringify_keys["plugins"].blank? + # Merge additional options and raw_options, but don't overwrite existing + def reverse_add_options(options = {}, raw_options = nil) + @options.reverse_merge!(options.stringify_keys) unless options.blank? + @raw_options << raw_options unless raw_options.blank? end - def plugins_include? plugin - @options.stringify_keys["plugins"].include? plugin + def plugins + @options['plugins'] || [] 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) - - unless merged_options['plugins'].nil? - raise TinyMCEInvalidOptionType.invalid_type_of merged_options['plugins'],:for=>:plugins unless merged_options['plugins'].is_a?(Array) - - # Append the plugins we have enabled for this field to the OptionsValidator - @plugins += merged_options['plugins'] - end + def to_json + raise TinyMCEInvalidOptionType.invalid_type_of(plugins, :for => :plugins) unless plugins.is_a?(Array) json_options = [] - merged_options.each_pair do |key,value| - raise TinyMCEInvalidOption.invalid_option key unless valid?(key) + @options.each_pair do |key, value| + raise TinyMCEInvalidOption.invalid_option(key) unless valid?(key) json_options << "#{key} : " + case value when String, Symbol, Fixnum "'#{value.to_s}'" @@ -71,7 +70,7 @@ def to_json options={},raw_options='' when FalseClass 'false' else - raise TinyMCEInvalidOptionType.invalid_type_of value,:for=>:plugins + raise TinyMCEInvalidOptionType.invalid_type_of(value, :for => key) end end @@ -79,28 +78,18 @@ def to_json options={},raw_options='' @raw_options.compact! json_options += @raw_options unless @raw_options.blank? - json_options << raw_options unless raw_options.blank? "{\n" + json_options.delete_if {|o| o.blank? }.join(",\n") + "\n\n}" end - 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) || - (@plugins && @plugins.include?(option.split('_').first)) || + self.class.valid_options.include?(option) || + plugins.include?(option.split('_').first) || option =~ /^theme_advanced_container_\w+$/ end - - # If we need to get the array of valid options, we can call this method - def self.valid_options - @@valid_options - end end end diff --git a/lib/tiny_mce/exceptions.rb b/lib/tiny_mce/exceptions.rb index 2ed7bcc..259d4ef 100644 --- a/lib/tiny_mce/exceptions.rb +++ b/lib/tiny_mce/exceptions.rb @@ -1,13 +1,13 @@ module TinyMCE # Setup a couple of Exception classes that we use later on class TinyMCEInvalidOption < Exception - def self.invalid_option option + def self.invalid_option(option) new "Invalid option #{option} passed to tinymce" end end class TinyMCEInvalidOptionType < Exception - def self.invalid_type_of value, parameters={} + def self.invalid_type_of(value, parameters={}) new "Invalid value of type #{value.class} passed for TinyMCE option #{parameters[:for].to_s}" end end diff --git a/lib/tiny_mce/helpers.rb b/lib/tiny_mce/helpers.rb index 3c85062..b61bf37 100644 --- a/lib/tiny_mce/helpers.rb +++ b/lib/tiny_mce/helpers.rb @@ -9,14 +9,12 @@ def using_tiny_mce? # Parse @tiny_mce_options and @raw_tiny_mce_options to create a raw JS string # used by TinyMCE. Returns errors if the option or options type is invalid - def raw_tiny_mce_init(options = {}, raw_options = '') - + def raw_tiny_mce_init(options = {}, raw_options = nil) tinymce_js = "" @tiny_mce_configurations ||= [Configuration.new] - @tiny_mce_configurations.each do |configuration| - configuration.add_options :options=>options,:raw_options=>raw_options + configuration.add_options(options, raw_options) tinymce_js += "tinyMCE.init(" tinymce_js += configuration.to_json tinymce_js += ");" @@ -26,12 +24,12 @@ def raw_tiny_mce_init(options = {}, raw_options = '') end # Form the raw JS and wrap in in a