Class: RESTFramework::NativeModelSerializer
- Inherits:
-
BaseSerializer
- Object
- BaseSerializer
- RESTFramework::NativeModelSerializer
- Defined in:
- lib/rest_framework/serializers.rb
Overview
This serializer uses ‘.as_json` to serialize objects. Despite the name, `.as_json` is an `ActiveModel` 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
Class Method Summary collapse
-
.[](key) ⇒ Object
Allow a serializer class to be used as a hash directly in a nested serializer config.
- .[]=(key, value) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Allow a serializer instance to be used as a hash directly in a nested serializer config.
- #[]=(key, value) ⇒ Object
-
#get_action ⇒ Object
Get controller action, if possible.
-
#get_local_serializer_config ⇒ Object
Get a locally defined configuration, if one is defined.
-
#get_serializer_config ⇒ Object
Get a configuration passable to ‘as_json` for the object.
-
#initialize(many: nil, model: nil, **kwargs) ⇒ NativeModelSerializer
constructor
A new instance of NativeModelSerializer.
-
#serialize ⇒ Object
Convert the object (record or recordset) to Ruby primitives.
Constructor Details
#initialize(many: nil, model: nil, **kwargs) ⇒ NativeModelSerializer
Returns a new instance of NativeModelSerializer.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rest_framework/serializers.rb', line 20 def initialize(many: nil, model: nil, **kwargs) super(**kwargs) if many.nil? @many = @object.respond_to?(:count) ? @object.count : nil else @many = many end @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.
102 103 104 105 106 107 |
# File 'lib/rest_framework/serializers.rb', line 102 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
108 109 110 111 112 113 |
# File 'lib/rest_framework/serializers.rb', line 108 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.
88 89 90 91 92 93 |
# File 'lib/rest_framework/serializers.rb', line 88 def [](key) unless instance_variable_defined?(:@_nested_config) @_nested_config = self.get_serializer_config end return @_nested_config[key] end |
#[]=(key, value) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/rest_framework/serializers.rb', line 94 def []=(key, value) unless instance_variable_defined?(:@_nested_config) @_nested_config = self.get_serializer_config end return @_nested_config[key] = value end |
#get_action ⇒ Object
Get controller action, if possible.
33 34 35 |
# File 'lib/rest_framework/serializers.rb', line 33 def get_action return @controller&.action_name&.to_sym end |
#get_local_serializer_config ⇒ Object
Get a locally defined configuration, if one is defined.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rest_framework/serializers.rb', line 38 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 returning the default config. return self.config end |
#get_serializer_config ⇒ Object
Get a configuration passable to ‘as_json` for the object.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rest_framework/serializers.rb', line 57 def get_serializer_config # Return a locally defined serializer config if one is defined. if local_config = self.get_local_serializer_config return local_config end # Return a serializer config if one is defined. if serializer_config = @controller.send(:get_native_serializer_config) return serializer_config end # If the config wasn't determined, build a serializer config from model fields. fields = @controller.try(: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 |
#serialize ⇒ Object
Convert the object (record or recordset) to Ruby primitives.
79 80 81 82 83 84 85 |
# File 'lib/rest_framework/serializers.rb', line 79 def serialize if @object @many = @object.respond_to?(:each) if @many.nil? return @object.as_json(self.get_serializer_config) end return nil end |