Class: Upframework::BaseSerializer
- Inherits:
-
Object
- Object
- Upframework::BaseSerializer
- Includes:
- JSONAPI::Serializer
- Defined in:
- app/serializers/upframework/base_serializer.rb
Instance Attribute Summary collapse
-
#current_controller ⇒ Object
readonly
Returns the value of attribute current_controller.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(resource, current_controller: nil, **args) ⇒ BaseSerializer
constructor
A new instance of BaseSerializer.
- #pagination ⇒ Object
- #set_format ⇒ Object
Constructor Details
#initialize(resource, current_controller: nil, **args) ⇒ BaseSerializer
Returns a new instance of BaseSerializer.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/serializers/upframework/base_serializer.rb', line 12 def initialize(resource, current_controller: nil, **args) @resource = resource @current_controller = current_controller if args[:format] == :minimal args[:fields] = set_format end if @current_controller && resource.respond_to?(:total_pages) args.merge!(pagination) end super(resource, **args) end |
Instance Attribute Details
#current_controller ⇒ Object (readonly)
Returns the value of attribute current_controller.
5 6 7 |
# File 'app/serializers/upframework/base_serializer.rb', line 5 def current_controller @current_controller end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'app/serializers/upframework/base_serializer.rb', line 5 def resource @resource end |
Class Method Details
.default_includes(relations = []) ⇒ Object
48 49 50 |
# File 'app/serializers/upframework/base_serializer.rb', line 48 def self.default_includes(relations = []) @relations ||= relations end |
Instance Method Details
#pagination ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/serializers/upframework/base_serializer.rb', line 35 def pagination { meta: { per_page: resource.size, total_pages: resource.total_pages, }, links: { prev: view_context.path_to_prev_page(resource), next: view_context.path_to_next_page(resource), }, }.deep_transform_keys { |key| key.to_s.camelize(:lower).to_sym } end |
#set_format ⇒ Object
27 28 29 30 31 32 33 |
# File 'app/serializers/upframework/base_serializer.rb', line 27 def set_format resource_klass = resource.class.name resource_klass = resource.klass.name if resource_klass == 'ActiveRecord::Relation' resource_klass = resource.first.class.name if resource.kind_of?(Array) { resource_klass.camelize(:lower) => [:id] } end |