Sha256: 5988483919d55f51adea39f915f395176893f8e3e939f5a38c9c737762e5154a

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

module TinyMCE::Rails
  class Configuration
    class Function < String
      def encode_json(encoder)
        self
      end
    end
    
    def self.defaults
      {
        "mode"            => "specific_textareas",
        "editor_selector" => "tinymce",
        "theme"           => "advanced"
      }
    end
    
    attr_reader :options
    
    def initialize(options)
      @options = options
    end
    
    def options_for_tinymce
      result = {}
      
      options.each do |key, value|
        if value.is_a?(Array) && value.all? { |v| v.is_a?(String) }
          result[key] = value.join(",")
        elsif value.to_s.starts_with?("function(")
          result[key] = Function.new(value)
        else
          result[key] = value
        end
      end
      
      result["language"] ||= self.class.default_language
      
      result
    end
    
    def merge(options)
      self.class.new(self.options.merge(options))
    end
    
    def load(filename)
      options.merge!(YAML::load(ERB.new(IO.read(filename)).result))
    end
    
    def self.load(filename)
      config = new(defaults)
      config.load(filename) if File.exists?(filename)
      config
    end
    
    # Default language falls back to English if current locale is not available.
    def self.default_language
      available_languages.include?(I18n.locale.to_s) ? I18n.locale.to_s : "en"
    end
    
    # Searches asset paths for TinyMCE language files.
    def self.available_languages
      assets.paths.map { |path|
        files = assets.entries(File.join(path, "tinymce/langs"))
        files.map { |file|
          asset = assets.attributes_for(File.join(path, file))
          asset.logical_path.sub(/\.js$/, "")
        }
      }.flatten.uniq
    end
  
    def self.assets
      Rails.application.assets
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
tinymce-rails-3.5.6 lib/tinymce/rails/configuration.rb
tinymce-rails-3.5.5 lib/tinymce/rails/configuration.rb
tinymce-rails-3.5.4.1 lib/tinymce/rails/configuration.rb
tinymce-rails-3.5.2 lib/tinymce/rails/configuration.rb