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
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Configuration

Returns a new instance of Configuration.



45
46
47
# File 'lib/tinymce/rails/configuration.rb', line 45

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/tinymce/rails/configuration.rb', line 43

def options
  @options
end

Class Method Details

.assetsObject



119
120
121
# File 'lib/tinymce/rails/configuration.rb', line 119

def self.assets
  Rails.application.assets
end

.available_languagesObject

Searches asset paths for TinyMCE language files.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tinymce/rails/configuration.rb', line 99

def self.available_languages
  assets.paths.map { |path|
    # Find all assets within tinymce/langs
    entries = assets.entries(File.join(path, "tinymce/langs"))
    entries.map { |entry|
      if assets.respond_to?(:attributes_for)
        assets.attributes_for(File.join(path, entry))
      else
        assets.find_asset(File.join("tinymce/langs", entry))
      end
    }.select { |asset|
      # Select only JavaScript files
      asset.logical_path =~ /\.js$/
    }.map { |asset|
      # Strip path and extension
      asset.logical_path.sub(/^tinymce\/langs\//, "").sub(/\.js$/, "")
    }
  }.flatten.uniq
end

.default_languageObject

Default language falls back to English if current locale is not available.



94
95
96
# File 'lib/tinymce/rails/configuration.rb', line 94

def self.default_language
  I18n.locale.to_s if available_languages.include?(I18n.locale.to_s)
end

.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



49
50
51
52
53
# File 'lib/tinymce/rails/configuration.rb', line 49

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

Instance Method Details

#merge(options) ⇒ Object



89
90
91
# File 'lib/tinymce/rails/configuration.rb', line 89

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

#options_for_tinymceObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tinymce/rails/configuration.rb', line 55

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
  end
  
  if self.class.default_language
    result["language"] ||= self.class.default_language
  end
  
  result
end

#to_javascriptObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/tinymce/rails/configuration.rb', line 75

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