spohlenz / tinymce-rails

Integration of TinyMCE with the Rails asset pipeline
Other
813 stars 256 forks source link

Nested options with functions #251

Closed pa657 closed 5 years ago

pa657 commented 5 years ago

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.

spohlenz commented 5 years ago

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.