Class: Railsful::Serializer
- Inherits:
-
Object
- Object
- Railsful::Serializer
- 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
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
Instance Method Summary collapse
-
#initialize(controller) ⇒ Serializer
constructor
Keep a reference to the controller, so all helper methods like
url_for
can be used. -
#method ⇒ String
Fetch the HTTP method from controllers request.
-
#params ⇒ Object
Fetch the params from controller.
-
#relation?(options) ⇒ Boolean
Check if given options contain an ActiveRecord::Relation.
-
#render(options) ⇒ Hash
The render function every interceptor MUST implement in order to add certain functionality.
-
#serializer_by_options(options) ⇒ Class
Check the options hash for a serializer key.
-
#serializer_by_renderable(renderable) ⇒ Class
:reek:UtilityFunction.
-
#serializer_for(renderable, options = {}) ⇒ Class
Find the right serializer for given object.
Methods included from Interceptors::Include
#include_options, #includes, #should_include?
Methods included from Interceptors::Pagination
Methods included from Interceptors::Sorting
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
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
19 20 21 |
# File 'lib/railsful/serializer.rb', line 19 def controller @controller end |
Instance Method Details
#method ⇒ String
Fetch the HTTP method from controllers request.
103 104 105 |
# File 'lib/railsful/serializer.rb', line 103 def method controller.request.request_method end |
#params ⇒ Object
Fetch the params from controller.
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
113 114 115 |
# File 'lib/railsful/serializer.rb', line 113 def relation?() [:json].is_a?(ActiveRecord::Relation) end |
#render(options) ⇒ Hash
The render function every interceptor MUST implement in order to add certain functionality.
:reek:FeatureEnvy
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() # Get the renderable (Object that should be rendered) from options hash. renderable = [:json] # Return if renderable is blank return 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 unless serializer # Replace json value with new serializer .merge(json: serializer.new(renderable, )) end |
#serializer_by_options(options) ⇒ Class
Check the options hash for a serializer key.
:reek:UtilityFunction
69 70 71 72 73 74 75 76 77 |
# File 'lib/railsful/serializer.rb', line 69 def () serializer = [: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
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
60 61 62 |
# File 'lib/railsful/serializer.rb', line 60 def serializer_for(renderable, = {}) () || serializer_by_renderable(renderable) end |