Class: TinyMCE::Configuration
- Inherits:
-
Object
- Object
- TinyMCE::Configuration
- Defined in:
- lib/tiny_mce/configuration.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#raw_options ⇒ Object
Returns the value of attribute raw_options.
Class Method Summary collapse
-
.config_file_options ⇒ Object
The YAML file might not exist, might be blank, might be invalid, or might be valid.
-
.default_options ⇒ Object
The default tiny_mce options.
-
.valid_langs ⇒ Object
Parse the valid langs file and load it into an array.
-
.valid_options ⇒ Object
Parse the valid options file and load it into an array.
Instance Method Summary collapse
-
#add_options(options = {}, raw_options = nil) ⇒ Object
Merge additional options and raw_options.
-
#initialize(options = {}, raw_options = nil) ⇒ Configuration
constructor
A new instance of Configuration.
- #plugins ⇒ Object
-
#reverse_add_options(options = {}, raw_options = nil) ⇒ Object
Merge additional options and raw_options, but don’t overwrite existing.
-
#to_json ⇒ Object
Validate and merge options and raw_options into a string to be used for tinyMCE.init() in the raw_tiny_mce_init helper.
-
#valid?(option) ⇒ Boolean
Does the check to see if the option is valid.
Constructor Details
#initialize(options = {}, raw_options = nil) ⇒ Configuration
Returns a new instance of Configuration.
49 50 51 52 53 54 55 |
# File 'lib/tiny_mce/configuration.rb', line 49 def initialize( = {}, = nil) = Hash.new unless .is_a?(Hash) @options = self.class.. merge(self.class..stringify_keys). merge(.stringify_keys) @raw_options = [] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
47 48 49 |
# File 'lib/tiny_mce/configuration.rb', line 47 def @options end |
#raw_options ⇒ Object
Returns the value of attribute raw_options.
47 48 49 |
# File 'lib/tiny_mce/configuration.rb', line 47 def @raw_options end |
Class Method Details
.config_file_options ⇒ Object
The YAML file might not exist, might be blank, might be invalid, or might be valid. Catch all cases and make sure we always return a Hash Run it through an ERB parser so that environment specific code can be put in the file
21 22 23 24 25 26 27 28 29 |
# File 'lib/tiny_mce/configuration.rb', line 21 def self. @@config_file_options ||= begin tiny_mce_yaml_filepath = File.join(Rails.root.to_s, 'config', 'tiny_mce.yml') return Hash.new unless File.exist?(tiny_mce_yaml_filepath) tiny_mce_config = IO.read(tiny_mce_yaml_filepath) tiny_mce_config = ERB.new(tiny_mce_config).result if defined?(ERB) (YAML::load(tiny_mce_config) rescue nil) || Hash.new end end |
.default_options ⇒ Object
The default tiny_mce options. Tries it’s best to determine the locale If the current locale doesn’t have a lang in TinyMCE, default to en
10 11 12 13 14 15 |
# File 'lib/tiny_mce/configuration.rb', line 10 def self. locale = I18n.locale.to_s[0,2] if defined?(I18n) locale = :en unless locale && valid_langs.include?(locale) { 'mode' => 'textareas', 'editor_selector' => 'mceEditor', 'theme' => 'simple', 'language' => locale } end |
.valid_langs ⇒ Object
Parse the valid langs file and load it into an array
32 33 34 35 36 37 |
# File 'lib/tiny_mce/configuration.rb', line 32 def self.valid_langs @@valid_langs ||= begin valid_langs_path = File.join(File.dirname(__FILE__), 'valid_tinymce_langs.yml') File.open(valid_langs_path) { |f| YAML.load(f.read) } end end |
.valid_options ⇒ Object
Parse the valid options file and load it into an array
40 41 42 43 44 45 |
# File 'lib/tiny_mce/configuration.rb', line 40 def self. @@valid_options ||= begin = File.join(File.dirname(__FILE__), 'valid_tinymce_options.yml') File.open() { |f| YAML.load(f.read) } end end |
Instance Method Details
#add_options(options = {}, raw_options = nil) ⇒ Object
Merge additional options and raw_options
58 59 60 61 |
# File 'lib/tiny_mce/configuration.rb', line 58 def ( = {}, = nil) @options.merge!(.stringify_keys) unless .blank? @raw_options << unless .blank? end |
#plugins ⇒ Object
69 70 71 |
# File 'lib/tiny_mce/configuration.rb', line 69 def plugins @options['plugins'] || [] end |
#reverse_add_options(options = {}, raw_options = nil) ⇒ Object
Merge additional options and raw_options, but don’t overwrite existing
64 65 66 67 |
# File 'lib/tiny_mce/configuration.rb', line 64 def ( = {}, = nil) @options.reverse_merge!(.stringify_keys) unless .blank? @raw_options << unless .blank? end |
#to_json ⇒ Object
Validate and merge options and raw_options into a string to be used for tinyMCE.init() in the raw_tiny_mce_init helper
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/tiny_mce/configuration.rb', line 75 def to_json raise TinyMCEInvalidOptionType.invalid_type_of(plugins, :for => :plugins) unless plugins.is_a?(Array) = [] @options.each_pair do |key, value| raise TinyMCEInvalidOption.invalid_option(key) unless valid?(key) << "#{key} : " + case value when String, Symbol, Fixnum "'#{value.to_s}'" when Array '"' + value.join(',') + '"' when TrueClass 'true' when FalseClass 'false' else raise TinyMCEInvalidOptionType.invalid_type_of(value, :for => key) end end .sort! @raw_options.compact! += @raw_options unless @raw_options.blank? "{\n" + .delete_if {|o| o.blank? }.join(",\n") + "\n\n}" end |
#valid?(option) ⇒ Boolean
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
106 107 108 109 110 111 |
# File 'lib/tiny_mce/configuration.rb', line 106 def valid?(option) option = option.to_s self.class..include?(option) || plugins.include?(option.split('_').first) || option =~ /^theme_advanced_container_\w+$/ end |