spohlenz/tinymce-rails

Nested options with functions

pa657 opened this issue · 1 comments

pa657 commented

Hi,
I have installed a plugin but this one required nested options with functions.
Nested functions are evaluated as text.
With this code, it's ok :

    def options_for_tinymce
      options_for_tinymce_nested(options)
    end

    def options_for_tinymce_nested(options_nested)
      result = {}
      options_nested.each do |key, value|
        if array_option?(key, value)
          result[key] = value.join(OPTION_SEPARATORS[key])
        elsif function_option?(value)
          result[key] = Function.new(value)
        elsif value.is_a?(Hash)
          result[key] = options_for_tinymce_nested(value)
        else
          result[key] = value
        end

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

      result
    end

    def to_javascript
      to_javascript_nested(options_for_tinymce)
    end

    def to_javascript_nested(options_nested)
      pairs = options_nested.inject([]) do |result, (k, v)|
        if v.respond_to?(:to_javascript)
          v = v.to_javascript
        elsif v.is_a?(Hash) && v.detect{|k2, v2| v2.respond_to?(:to_javascript)}
          v = to_javascript_nested(v)
        elsif v.respond_to?(:to_json)
          v = v.to_json
        end

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

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

Is it possible to integrate this kind of code to the gem ?
Best Regards and thank you.

Thank you for the report and code example. I have added some tests and implemented this in 71af064, which is now available in the 4.9.0 gem release.