Module: Railsful::Interceptors::Include

Included in:
Serializer
Defined in:
lib/railsful/interceptors/include.rb

Overview

This interceptors implements the “include” functionality for a given record or a relation.

Instance Method Summary collapse

Instance Method Details

#include_options(options) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/railsful/interceptors/include.rb', line 12

def include_options(options)
  # Check if include key should be merged into options hash.
  return options unless should_include?

  # Deep merge include options, so we do not override existing
  # include options.
  options.deeper_merge(include: includes)
end

#includesArray

Fetch the list of all includes.

Returns:

  • (Array)

    The list of all include options.



33
34
35
# File 'lib/railsful/interceptors/include.rb', line 33

def includes
  params.fetch(:include, nil).to_s.split(',')
end

#render(options) ⇒ Object



8
9
10
# File 'lib/railsful/interceptors/include.rb', line 8

def render(options)
  super(include_options(options))
end

#should_include?Boolean

Check if options should contain includes.

Returns:

  • (Boolean)

    The answer.



24
25
26
27
28
# File 'lib/railsful/interceptors/include.rb', line 24

def should_include?
  # Only GET requests should have the "include" functionality,
  # since it may be a parameter in a create or update action.
  method == 'GET' && includes.any?
end