Module: ActionController::Serialization

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

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#get_serializer(resource, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/action_controller/serialization.rb', line 43

def get_serializer(resource, options = {})
  unless use_adapter?
    warn 'ActionController::Serialization#use_adapter? has been removed. '\
      "Please pass 'adapter: false' or see ActiveSupport::SerializableResource.new"
    options[:adapter] = false
  end

  options.fetch(:namespace) { options[:namespace] = namespace_for_serializer }

  serializable_resource = ActiveModelSerializers::SerializableResource.new(resource, options)
  serializable_resource.serialization_scope ||= options.fetch(:scope) { serialization_scope }
  serializable_resource.serialization_scope_name = options.fetch(:scope_name) { _serialization_scope }
  # For compatibility with the JSON renderer: `json.to_json(options) if json.is_a?(String)`.
  # Otherwise, since `serializable_resource` is not a string, the renderer would call
  # `to_json` on a String and given odd results, such as `"".to_json #=> '""'`
  serializable_resource.adapter.is_a?(String) ? serializable_resource.adapter : serializable_resource
end

#namespace_for_class(klass) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/action_controller/serialization.rb', line 29

def namespace_for_class(klass)
  if Module.method_defined?(:module_parent)
    klass.module_parent
  else
    klass.parent
  end
end

#namespace_for_serializerObject



25
26
27
# File 'lib/action_controller/serialization.rb', line 25

def namespace_for_serializer
  @namespace_for_serializer ||= namespace_for_class(self.class) unless namespace_for_class(self.class) == Object
end

#serialization_scopeObject



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

def serialization_scope
  return unless _serialization_scope && respond_to?(_serialization_scope, true)

  send(_serialization_scope)
end

#use_adapter?Boolean

Deprecated

Returns:

  • (Boolean)


62
63
64
# File 'lib/action_controller/serialization.rb', line 62

def use_adapter?
  true
end