Module: TinyMCE::Rails::Helper

Defined in:
lib/tinymce/rails/helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.tinymce(config = :default, options = {}) ⇒ Object

Initializes TinyMCE on the current page based on the global configuration.

Custom options can be set via the options hash, which will be passed to the TinyMCE init function.

By default, all textareas with a class of “tinymce” will have the TinyMCE editor applied. The current locale will also be used as the language when TinyMCE language files are available, falling back to English if not available. The :editor_selector and :language options can be used to override these defaults.

Examples:

<%= tinymce(:theme => "advanced", :editor_selector => "editorClass") %>


18
19
20
# File 'lib/tinymce/rails/helper.rb', line 18

def tinymce(config=:default, options={})
  javascript_tag { tinymce_javascript(config, options) }
end

.tinymce_configuration(config = :default, options = {}) ⇒ Object

Returns the TinyMCE configuration object. It should be converted to JavaScript (via #to_javascript) for use within JavaScript.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/tinymce/rails/helper.rb', line 37

def tinymce_configuration(config=:default, options={})
  options, config = config, :default if config.is_a?(Hash)
  options.stringify_keys!
  
  base_configuration = TinyMCE::Rails.configuration
  
  if base_configuration.is_a?(MultipleConfiguration)
    base_configuration = base_configuration.fetch(config)
  end
  
  base_configuration.merge(options)
end

.tinymce_javascript(config = :default, options = {}) ⇒ Object

Returns the JavaScript code required to initialize TinyMCE.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tinymce/rails/helper.rb', line 23

def tinymce_javascript(config=:default, options={})
  <<-JAVASCRIPT.strip_heredoc.html_safe
  (function() {
    if (typeof tinyMCE != 'undefined') {
      tinyMCE.init(#{tinymce_configuration(config, options).to_javascript.gsub(/^/, ' ' * 10).sub(/\A\s+/, "")});
    } else {
      setTimeout(arguments.callee, 50);
    }
  })();
  JAVASCRIPT
end

Instance Method Details

#tinymce_assetsObject

Includes TinyMCE javascript assets via a script tag.



51
52
53
# File 'lib/tinymce/rails/helper.rb', line 51

def tinymce_assets
  javascript_include_tag "tinymce"
end