Skip to content

Commit

Permalink
Cleanup and bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kieran Pilkington committed Feb 26, 2010
1 parent 34cc321 commit 3af2265
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 85 deletions.
5 changes: 0 additions & 5 deletions lib/tiny_mce.rb
Expand Up @@ -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)
Expand Down
24 changes: 5 additions & 19 deletions lib/tiny_mce/base.rb
Expand Up @@ -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

Expand Down
93 changes: 41 additions & 52 deletions 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}'"
Expand All @@ -71,36 +70,26 @@ 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

json_options.sort!

@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
4 changes: 2 additions & 2 deletions 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
Expand Down
12 changes: 5 additions & 7 deletions lib/tiny_mce/helpers.rb
Expand Up @@ -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 += ");"
Expand All @@ -26,12 +24,12 @@ def raw_tiny_mce_init(options = {}, raw_options = '')
end

# Form the raw JS and wrap in in a <script> tag for inclusion in the <head>
def tiny_mce_init(options = {}, raw_options = '')
def tiny_mce_init(options = {}, raw_options = nil)
javascript_tag raw_tiny_mce_init(options, raw_options)
end
# Form the raw JS and wrap in in a <script> tag for inclusion in the <head>
# (only if tiny mce is actually being used)
def tiny_mce_init_if_needed(options = {}, raw_options = '')
def tiny_mce_init_if_needed(options = {}, raw_options = nil)
tiny_mce_init(options, raw_options) if using_tiny_mce?
end

Expand All @@ -48,7 +46,7 @@ def include_tiny_mce_js_if_needed
# Form a JS include tag for the TinyMCE JS src, and form the raw JS and wrap
# in in a <script> tag for inclusion in the <head> for inclusion in the <head>
# (only if tiny mce is actually being used)
def include_tiny_mce_if_needed(options = {}, raw_options = '')
def include_tiny_mce_if_needed(options = {}, raw_options = nil)
if using_tiny_mce?
include_tiny_mce_js + tiny_mce_init(options, raw_options)
end
Expand Down

0 comments on commit 3af2265

Please sign in to comment.