Module: Caprese::Rendering

Extended by:
ActiveSupport::Concern
Included in:
Controller
Defined in:
lib/caprese/controller/concerns/rendering.rb

Instance Method Summary collapse

Instance Method Details

#metaHash

Allows for meta tags to be added in response document

Examples:

meta[:redirect_url] = ...

Returns:

  • (Hash)

    the meta tag object



40
41
42
# File 'lib/caprese/controller/concerns/rendering.rb', line 40

def meta
  @caprese_meta ||= {}
end

#render(options = {}) ⇒ Object

Override render so we can automatically use our adapter and find the appropriate serializer instead of requiring that they be explicity stated



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/caprese/controller/concerns/rendering.rb', line 11

def render(options = {})
  if options.is_a?(Hash) && options[:json]
    options[:adapter] = Caprese::Adapter::JsonApi
    options[:meta] = meta unless meta.empty?

    if options[:json].respond_to?(:to_ary)
      if options[:json].first.is_a?(Error)
        options[:each_serializer] ||= Serializer::ErrorSerializer
      elsif options[:json].any?
        options[:each_serializer] ||= serializer_for(options[:json].first)
      end
    else
      if options[:json].is_a?(Error)
        options[:serializer] ||= Serializer::ErrorSerializer
      elsif options[:json].present?
        options[:serializer] ||= serializer_for(options[:json])
      end
    end
  end

  super
end