Class: TinyMCE::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_mce/configuration.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, raw_options = nil) ⇒ Configuration


49
50
51
52
53
54
55
# File 'lib/tiny_mce/configuration.rb', line 49

def initialize(options = {}, raw_options = nil)
  options = Hash.new unless options.is_a?(Hash)
  @options = self.class.default_options.
                        merge(self.class.config_file_options.stringify_keys).
                        merge(options.stringify_keys)
  @raw_options = [raw_options]
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options


47
48
49
# File 'lib/tiny_mce/configuration.rb', line 47

def options
  @options
end

#raw_optionsObject

Returns the value of attribute raw_options


47
48
49
# File 'lib/tiny_mce/configuration.rb', line 47

def raw_options
  @raw_options
end

Class Method Details

.config_file_optionsObject

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
  @@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_optionsObject

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.default_options
  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_langsObject

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_optionsObject

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
  @@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

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 add_options(options = {}, raw_options = nil)
  @options.merge!(options.stringify_keys) unless options.blank?
  @raw_options << raw_options unless raw_options.blank?
end

#pluginsObject


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 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

#to_jsonObject

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)

  json_options = []
  @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}'"
    when Array
      '"' + value.join(',') + '"'
    when TrueClass
      'true'
    when FalseClass
      'false'
    else
      raise TinyMCEInvalidOptionType.invalid_type_of(value, :for => key)
    end
  end

  json_options.sort!

  @raw_options.compact!
  json_options += @raw_options unless @raw_options.blank?

  "{\n" + json_options.delete_if {|o| o.blank? }.join(",\n") + "\n\n}"
end

#uses_jquery?Boolean


113
114
115
# File 'lib/tiny_mce/configuration.rb', line 113

def uses_jquery?
  Rails.version.to_i >= 3 && ActionView::Helpers::AssetTagHelper.javascript_expansions[:defaults].detect{|x| x == "jquery"}
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.valid_options.include?(option) ||
    plugins.include?(option.split('_').first) ||
    option =~ /^theme_advanced_container_\w+$/
end