mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Implement basic rendering
Woo actioncontroller
This commit is contained in:
@@ -19,6 +19,28 @@ module ActiveModel
|
||||
end
|
||||
end
|
||||
|
||||
if RUBY_VERSION >= '2.0'
|
||||
def self.serializer_for(resource)
|
||||
if resource.respond_to?(:to_ary)
|
||||
ArraySerializer
|
||||
else
|
||||
begin
|
||||
Object.const_get "#{resource.class.name}Serializer"
|
||||
rescue NameError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
def self.serializer_for(resource)
|
||||
if resource.respond_to?(:to_ary)
|
||||
ArraySerializer
|
||||
else
|
||||
"#{resource.class.name}Serializer".safe_constantize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_accessor :object
|
||||
|
||||
def initialize(object)
|
||||
|
||||
@@ -6,10 +6,10 @@ module ActiveModel
|
||||
@serializer = serializer
|
||||
end
|
||||
|
||||
def to_json
|
||||
def to_json(options={})
|
||||
@serializer.attributes.each_with_object({}) do |(attr, value), h|
|
||||
h[attr] = value
|
||||
end.to_json
|
||||
end.to_json # FIXME: why does passing options here cause {}?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user