Class: Railsful::Serializer

Inherits:
Object
  • Object
show all
Includes:
Interceptors::Errors, Interceptors::Include, Interceptors::Pagination, Interceptors::Sorting
Defined in:
lib/railsful/serializer.rb

Overview

This class allows to encapsulate the interceptor logic from the prepended controller, so the controller is not polluted with all needed (helper) methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interceptors::Include

#include_options, #includes, #should_include?

Methods included from Interceptors::Pagination

#pagination_options

Methods included from Interceptors::Sorting

#sorting_options

Methods included from Interceptors::Errors

#errors, #errors?, #errors_options, #field_errors, #formatted_error

Constructor Details

#initialize(controller) ⇒ Serializer

Keep a reference to the controller, so all helper methods like url_for can be used.



23
24
25
# File 'lib/railsful/serializer.rb', line 23

def initialize(controller)
  @controller = controller
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



19
20
21
# File 'lib/railsful/serializer.rb', line 19

def controller
  @controller
end

Instance Method Details

#methodString

Fetch the HTTP method from controllers request.

Returns:

  • (String)

    The method.



78
79
80
# File 'lib/railsful/serializer.rb', line 78

def method
  controller.request.request_method
end

#paramsObject

Fetch the params from controller.

Returns:

  • The params.



71
72
73
# File 'lib/railsful/serializer.rb', line 71

def params
  controller.params
end

#relation?(options) ⇒ Boolean

Check if given options contain an ActiveRecord::Relation.

:reek:UtilityFunction

Parameters:

  • options (Hash)

    The options.

Returns:

  • (Boolean)

    The answer.



88
89
90
# File 'lib/railsful/serializer.rb', line 88

def relation?(options)
  options[:json].is_a?(ActiveRecord::Relation)
end

#render(options) ⇒ Hash

The render function every interceptor MUST implement in order to add certain functionality.

:reek:FeatureEnvy

Parameters:

  • options (Hash)

    The render options hash.

Returns:

  • (Hash)

    The (modified) render options hash.



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

def render(options)
  # Get the renderable (Object that should be rendered) from options hash.
  renderable = options[:json]

  # Return if renderable is blank
  return options unless renderable

  # Try to fetch the right serializer for given renderable.
  serializer = serializer_for(renderable)

  # When no serializer is found just pass the original options hash.
  return options unless serializer

  # Replace json value with new serializer
  options.merge(json: serializer.new(renderable, options))
end

#serializer_for(renderable) ⇒ Class

Find the right serializer for given object.

:reek:UtilityFunction

Parameters:

  • (ApplicationRecord, ActiveRecord::Relation)

Returns:

  • (Class)

    The serializer class.



57
58
59
60
61
62
63
64
65
66
# File 'lib/railsful/serializer.rb', line 57

def serializer_for(renderable)
  # Get the class in order to find the right serializer.
  klass = if renderable.is_a?(ActiveRecord::Relation)
            renderable.model.name
          else
            renderable.class.name
          end

  "#{klass}Serializer".safe_constantize
end