Module: Shreddies::AsJson::ActiveRecordRelation

Defined in:
lib/shreddies/as_json.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shreddies/as_json.rb', line 23

def as_json(options = {})
  serializer = options.delete(:serializer) || "#{model_name}Serializer"

  if serializer.is_a?(String) || serializer.is_a?(Symbol)
    serializer = serializer.to_s.safe_constantize
  elsif serializer.is_a?(Proc)
    return serializer.call
  elsif serializer.is_a?(Hash)
    options = serializer
    serializer = "#{model_name}Serializer".safe_constantize
  end

  serializer ? serializer.render_as_json(self, options) : super
end