Module: Shreddies::AsJson::ActiveRecordBase

Defined in:
lib/shreddies/as_json.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/shreddies/as_json.rb', line 6

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