Class: TinyMCE::Rails::Configuration

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

Defined Under Namespace

Classes: Function

Constant Summary collapse

COMMA =
",".freeze
SPACE =
" ".freeze
SEMICOLON =
";".freeze
OPTION_SEPARATORS =
{
  "plugins"                       => COMMA,
  "custom_elements"               => COMMA,
  "entities"                      => COMMA,
  "extended_valid_elements"       => COMMA,
  "font_formats"                  => SEMICOLON,
  "fontsize_formats"              => COMMA,
  "invalid_elements"              => COMMA,
  "block_formats"                 => SEMICOLON,
  "valid_children"                => COMMA,
  "valid_elements"                => COMMA,
  "body_id"                       => COMMA,
  "body_class"                    => COMMA,
  "content_css"                   => COMMA,
  "tabfocus_elements"             => COMMA,
  "table_clone_elements"          => SPACE,
  "paste_word_valid_elements"     => COMMA,
  "paste_webkit_styles"           => SPACE,
  "paste_retain_style_properties" => SPACE,
  "spellchecker_languages"        => COMMA
}
OPTION_TRANSFORMERS =
{
  # Check for files provided in the content_css option to replace them with their actual path.
  # If no corresponding stylesheet is found for a file, it will remain unchanged.
  "content_css" => ->(value) {
    value.split(OPTION_SEPARATORS["content_css"]).map do |file|
      ActionController::Base.helpers.stylesheet_path(file.strip) || file
    end.join(OPTION_SEPARATORS["content_css"])
  }
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



55
56
57
# File 'lib/tinymce/rails/configuration.rb', line 55

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



53
54
55
# File 'lib/tinymce/rails/configuration.rb', line 53

def options
  @options
end

Class Method Details

.defaultsObject



11
12
13
14
15
# File 'lib/tinymce/rails/configuration.rb', line 11

def self.defaults
  {
    "selector" => "textarea.tinymce"
  }
end

.new_with_defaults(options = {}) ⇒ Object



59
60
61
62
63
# File 'lib/tinymce/rails/configuration.rb', line 59

def self.new_with_defaults(options={})
  config = new(defaults)
  config = config.merge(options) if options
  config
end

Instance Method Details

#merge(options) ⇒ Object



99
100
101
# File 'lib/tinymce/rails/configuration.rb', line 99

def merge(options)
  self.class.new(self.options.merge(options))
end

#options_for_tinymceObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/tinymce/rails/configuration.rb', line 65

def options_for_tinymce
  result = {}

  options.each do |key, value|
    if OPTION_SEPARATORS[key] && value.is_a?(Array)
      result[key] = value.join(OPTION_SEPARATORS[key])
    elsif value.to_s.starts_with?("function(")
      result[key] = Function.new(value)
    else
      result[key] = value
    end

    if OPTION_TRANSFORMERS[key]
      result[key] = OPTION_TRANSFORMERS[key].call result[key]
    end
  end

  result
end

#to_javascriptObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tinymce/rails/configuration.rb', line 85

def to_javascript
  pairs = options_for_tinymce.inject([]) do |result, (k, v)|
    if v.respond_to?(:to_javascript)
      v = v.to_javascript
    elsif v.respond_to?(:to_json)
      v = v.to_json
    end

    result << [k, v].join(": ")
  end

  "{\n  #{pairs.join(",\n  ")}\n}"
end