Module: MiniApi::Serialization
- Included in:
- ModelResponder, RelationResponder
- Defined in:
- lib/mini_api/serialization.rb
Instance Method Summary collapse
-
#get_error_serializer(resource) ⇒ Object
Search by the nested class
Error
on serializer Follow the same steps forget_serializer
. -
#serialiable_body(resource) ⇒ Object
This method search by serializer using the module parents of controller.
Instance Method Details
#get_error_serializer(resource) ⇒ Object
Search by the nested class Error
on serializer Follow the same steps for get_serializer
40 41 42 43 44 45 46 |
# File 'lib/mini_api/serialization.rb', line 40 def get_error_serializer(resource) error_serializer = serialiable_body(resource) return unless error_serializer "#{error_serializer.class}::Error".safe_constantize end |
#serialiable_body(resource) ⇒ Object
This method search by serializer using the module parents of controller. With this, is possible define serializers for the same resource in different controller scopes. If the resource class does not have a resource, will be use the default ‘as_json` method
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mini_api/serialization.rb', line 11 def serialiable_body(resource) controller_scope = @controller.class.module_parents resource_class = if resource.respond_to?(:model) resource.model else resource.class end serializer_class = loop do serializer_class = "#{controller_scope.first}::#{resource_class}Resource".safe_constantize break serializer_class if serializer_class break if controller_scope.empty? controller_scope.shift end return serializer_class.new(resource) if serializer_class resource end |