mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Implement basic rendering
Woo actioncontroller
This commit is contained in:
parent
45803f1aa1
commit
970b542549
24
lib/action_controller/serialization.rb
Normal file
24
lib/action_controller/serialization.rb
Normal file
@ -0,0 +1,24 @@
|
||||
require 'active_support/core_ext/class/attribute'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include ActionController::Renderers
|
||||
|
||||
def _render_option_json(resource, options)
|
||||
serializer = ActiveModel::Serializer.serializer_for(resource)
|
||||
|
||||
if serializer
|
||||
# omg hax
|
||||
object = serializer.new(resource)
|
||||
adapter = ActiveModel::Serializer::Adapter::NullAdapter.new(object)
|
||||
|
||||
super(adapter, options)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -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
|
||||
|
||||
@ -4,3 +4,14 @@ require "active_model/serializer/version"
|
||||
require "active_model/serializer"
|
||||
|
||||
require "active_model/serializer/adapter/null_adapter"
|
||||
|
||||
begin
|
||||
require 'action_controller'
|
||||
require 'action_controller/serialization'
|
||||
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
include ::ActionController::Serialization
|
||||
end
|
||||
rescue LoadError
|
||||
# rails not installed, continuing
|
||||
end
|
||||
|
||||
25
test/action_controller/serialization_test.rb
Normal file
25
test/action_controller/serialization_test.rb
Normal file
@ -0,0 +1,25 @@
|
||||
require 'test_helper'
|
||||
|
||||
module ActionController
|
||||
module Serialization
|
||||
class ImplicitSerializerTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def render_using_implicit_serializer
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
# We just have Null for now, this will change
|
||||
def test_render_using_implicit_serializer
|
||||
get :render_using_implicit_serializer
|
||||
|
||||
assert_equal 'application/json', @response.content_type
|
||||
assert_equal '{"name":"Name 1","description":"Description 1"}', @response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@ -1,9 +1,27 @@
|
||||
require "bundler/setup"
|
||||
|
||||
require 'rails'
|
||||
require 'action_controller'
|
||||
require 'action_controller/test_case'
|
||||
require "active_support/json"
|
||||
require 'minitest/autorun'
|
||||
|
||||
require "active_model_serializers"
|
||||
|
||||
require 'fixtures/poro'
|
||||
|
||||
module TestHelper
|
||||
Routes = ActionDispatch::Routing::RouteSet.new
|
||||
Routes.draw do
|
||||
get ':controller(/:action(/:id))'
|
||||
get ':controller(/:action)'
|
||||
end
|
||||
|
||||
ActionController::Base.send :include, Routes.url_helpers
|
||||
end
|
||||
|
||||
ActionController::TestCase.class_eval do
|
||||
def setup
|
||||
@routes = TestHelper::Routes
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user