Module: ActionController::Serialization

Extended by:
ActiveSupport::Concern
Includes:
Renderers
Defined in:
lib/action_controller/serialization.rb

Overview

Action Controller Serialization

Overrides render :json to check if the given object implements active_model_serializer as a method. If so, use the returned serializer instead of calling to_json on the object.

This module also provides a serialization_scope method that allows you to configure the serialization_scope of the serializer. Most apps will likely set the serialization_scope to the current user:

class ApplicationController < ActionController::Base
  serialization_scope :current_user
end

If you need more complex scope rules, you can simply override the serialization_scope:

class ApplicationController < ActionController::Base
  private

  def serialization_scope
    current_user
  end
end

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_render_option_json(resource, options) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/action_controller/serialization.rb', line 42

def _render_option_json(resource, options)
  json = ActiveModel::Serializer.build_json(self, resource, options)

  if json
    super(json, options)
  else
    super
  end
end

#default_serializer_optionsObject



39
40
# File 'lib/action_controller/serialization.rb', line 39

def default_serializer_options
end

#serialization_scopeObject



35
36
37
# File 'lib/action_controller/serialization.rb', line 35

def serialization_scope
  send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
end