Module: Rails::Patch::Json::Encode

Defined in:
lib/rails/patch/json/encode/version.rb,
lib/rails/patch/json/encode.rb

Defined Under Namespace

Modules: ToJsonWithMultiJson

Constant Summary collapse

VERSION =
"0.2.0"

Class Method Summary collapse

Class Method Details

.patch_base_classesObject



39
40
41
42
43
# File 'lib/rails/patch/json/encode.rb', line 39

def self.patch_base_classes
  [Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass, Enumerable].reverse_each do |klass|
    klass.prepend(ToJsonWithMultiJson)
  end
end

.patch_renderersObject

Use multi_json instead of Rails’ to_json method (which calls ActiveSupport::JSON) when ‘render :json => @obj` is called.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rails/patch/json/encode.rb', line 7

def self.patch_renderers
  ::ActionController::Renderers.module_eval do
    # Override
    add :json do |json, options|
      json = MultiJson::dump(json.as_json(options), options) unless json.kind_of?(String)

      if options[:callback].present?
        self.content_type ||= Mime::JS
        "#{options[:callback]}(#{json})"
      else
        self.content_type ||= Mime::JSON
        json
      end
    end
  end
end