Module: Transmutation::Serialization::Rendering

Defined in:
lib/transmutation/serialization/rendering.rb

Instance Method Summary collapse

Instance Method Details

#render(options = nil, **kwargs) ⇒ Object

Serializes the value of the json parameter before calling the existing render method.

Handles both Rails-style hash arguments (e.g., from send_data) and keyword arguments.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/transmutation/serialization/rendering.rb', line 9

def render(options = nil, **kwargs)
  # Handle Rails-style positional hash argument (e.g., from send_data calling render(body: data))
  if options.is_a?(Hash)
    return super(options) unless options.key?(:json)

    kwargs = options.merge(kwargs)
  end

  json = kwargs.delete(:json)
  should_serialize = kwargs.key?(:serialize) ? kwargs.delete(:serialize) : true
  namespace = kwargs.delete(:namespace)
  serializer = kwargs.delete(:serializer)
  max_depth = kwargs.delete(:max_depth) || Transmutation.max_depth

  return super(**kwargs) unless json
  return super(**kwargs, json:) unless should_serialize

  super(**kwargs, json: serialize(json, namespace:, serializer:, max_depth:))
end