Class: RESTFramework::NativeModelSerializer

Inherits:
BaseSerializer show all
Defined in:
lib/rest_framework/serializers.rb

Overview

This serializer uses ‘.as_json` to serialize objects. Despite the name, `.as_json` is a Rails method which converts objects to Ruby primitives (with the top-level being either an array or a hash).

Instance Attribute Summary

Attributes inherited from BaseSerializer

#errors

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model: nil, many: nil, **kwargs) ⇒ NativeModelSerializer

Returns a new instance of NativeModelSerializer.



21
22
23
24
25
# File 'lib/rest_framework/serializers.rb', line 21

def initialize(model: nil, many: nil, **kwargs)
  super(**kwargs)
  @many = many
  @model = model || (@controller ? @controller.send(:get_model) : nil)
end

Class Method Details

.[](key) ⇒ Object

Allow a serializer class to be used as a hash directly in a nested serializer config.



95
96
97
98
99
100
# File 'lib/rest_framework/serializers.rb', line 95

def self.[](key)
  unless instance_variable_defined?(:@_nested_config)
    @_nested_config = self.new.get_serializer_config
  end
  return @_nested_config[key]
end

.[]=(key, value) ⇒ Object



101
102
103
104
105
106
# File 'lib/rest_framework/serializers.rb', line 101

def self.[]=(key, value)
  unless instance_variable_defined?(:@_nested_config)
    @_nested_config = self.new.get_serializer_config
  end
  return @_nested_config[key] = value
end

Instance Method Details

#[](key) ⇒ Object

Allow a serializer instance to be used as a hash directly in a nested serializer config.



81
82
83
84
85
86
# File 'lib/rest_framework/serializers.rb', line 81

def [](key)
  unless instance_variable_defined?(:@_nested_config)
    @_nested_config = self.get_serializer_config
  end
  return @_nested_config[key]
end

#[]=(key, value) ⇒ Object



87
88
89
90
91
92
# File 'lib/rest_framework/serializers.rb', line 87

def []=(key, value)
  unless instance_variable_defined?(:@_nested_config)
    @_nested_config = self.get_serializer_config
  end
  return @_nested_config[key] = value
end

#get_actionObject

Get controller action, if possible.



28
29
30
# File 'lib/rest_framework/serializers.rb', line 28

def get_action
  return @controller&.action_name&.to_sym
end

#get_local_serializer_configObject

Get a locally defined configuration, if one is defined.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rest_framework/serializers.rb', line 33

def get_local_serializer_config
  action = self.get_action

  if action && self.action_config
    # index action should use :list serializer config if :index is not provided
    action = :list if action == :index && !self.action_config.key?(:index)

    return self.action_config[action] if self.action_config[action]
  end

  # no action_config, so try singular/plural config
  return self.plural_config if @many && self.plural_config
  return self.singular_config if !@many && self.singular_config

  # lastly, try the default config
  return self.config
end

#get_serializer_configObject

Get a configuration passable to ‘as_json` for the model.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rest_framework/serializers.rb', line 52

def get_serializer_config
  # return a locally defined serializer config if one is defined
  local_config = self.get_local_serializer_config
  return local_config if local_config

  # return a serializer config if one is defined
  serializer_config = @controller.send(:get_native_serializer_config)
  return serializer_config if serializer_config

  # otherwise, build a serializer config from fields
  fields = @controller.send(:get_fields) if @controller
  unless fields.blank?
    columns, methods = fields.partition { |f| f.to_s.in?(@model.column_names) }
    return {only: columns, methods: methods}
  end

  return {}
end

#serializeObject

Convert the object(s) to Ruby primitives.



72
73
74
75
76
77
78
# File 'lib/rest_framework/serializers.rb', line 72

def serialize
  if @object
    @many = @object.respond_to?(:each) if @many.nil?
    return @object.as_json(self.get_serializer_config)
  end
  return nil
end