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.



103
104
105
# File 'lib/railsful/serializer.rb', line 103

def method
  controller.request.request_method
end

#paramsObject

Fetch the params from controller.

Returns:

  • The params.



96
97
98
# File 'lib/railsful/serializer.rb', line 96

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.



113
114
115
# File 'lib/railsful/serializer.rb', line 113

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, options)

  # 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_by_options(options) ⇒ Class

Check the options hash for a serializer key.

:reek:UtilityFunction

Returns:

  • (Class)

    The serializer class.



69
70
71
72
73
74
75
76
77
# File 'lib/railsful/serializer.rb', line 69

def serializer_by_options(options)
  serializer = options[:serializer]
  return unless serializer

  # If the given serializer is a class return it.
  return serializer if serializer.is_a? Class

  "#{serializer.to_s.classify}Serializer".safe_constantize
end

#serializer_by_renderable(renderable) ⇒ Class

:reek:UtilityFunction

Returns:

  • (Class)

    The serializer class.



82
83
84
85
86
87
88
89
90
91
# File 'lib/railsful/serializer.rb', line 82

def serializer_by_renderable(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

#serializer_for(renderable, options = {}) ⇒ Class

Find the right serializer for given object. First we will look if the options hash includes a serializer. If not, we try to guess the right serializer from the model/class name.

:reek:UtilityFunction

Parameters:

  • renderable (ApplicationRecord, ActiveRecord::Relation)
  • options (Hash) (defaults to: {})

Returns:

  • (Class)

    The serializer class.



60
61
62
# File 'lib/railsful/serializer.rb', line 60

def serializer_for(renderable, options = {})
  serializer_by_options(options) || serializer_by_renderable(renderable)
end