mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Implement AC integration
This commit is contained in:
parent
d41e5ccef7
commit
0d3b56e9cf
71
lib/action_controller/serialization.rb
Normal file
71
lib/action_controller/serialization.rb
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
require 'active_support/core_ext/class/attribute'
|
||||||
|
|
||||||
|
module ActionController
|
||||||
|
# Action Controller Serialization
|
||||||
|
#
|
||||||
|
# Overrides render :json to check if the given object implements +active_model_serializer+
|
||||||
|
# as a method. If so, use the returned serializer instead of calling +to_json+ on the object.
|
||||||
|
#
|
||||||
|
# This module also provides a serialization_scope method that allows you to configure the
|
||||||
|
# +serialization_scope+ of the serializer. Most apps will likely set the +serialization_scope+
|
||||||
|
# to the current user:
|
||||||
|
#
|
||||||
|
# class ApplicationController < ActionController::Base
|
||||||
|
# serialization_scope :current_user
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# If you need more complex scope rules, you can simply override the serialization_scope:
|
||||||
|
#
|
||||||
|
# class ApplicationController < ActionController::Base
|
||||||
|
# private
|
||||||
|
#
|
||||||
|
# def serialization_scope
|
||||||
|
# current_user
|
||||||
|
# end
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
module Serialization
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
include ActionController::Renderers
|
||||||
|
|
||||||
|
included do
|
||||||
|
class_attribute :_serialization_scope
|
||||||
|
self._serialization_scope = :current_user
|
||||||
|
end
|
||||||
|
|
||||||
|
def _render_option_json(resource, options)
|
||||||
|
serializer = build_json_serializer(resource, options)
|
||||||
|
|
||||||
|
if serializer
|
||||||
|
super(serializer, options)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def default_serializer_options
|
||||||
|
{}
|
||||||
|
end
|
||||||
|
|
||||||
|
def serialization_scope
|
||||||
|
_serialization_scope = self.class._serialization_scope
|
||||||
|
send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def build_json_serializer(resource, options)
|
||||||
|
options = default_serializer_options.merge(options || {})
|
||||||
|
|
||||||
|
serializer =
|
||||||
|
options.delete(:serializer) ||
|
||||||
|
resource.respond_to?(:active_model_serializer) &&
|
||||||
|
resource.active_model_serializer
|
||||||
|
|
||||||
|
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
||||||
|
|
||||||
|
serializer.new(resource, options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
13
lib/active_model_serializers.rb
Normal file
13
lib/active_model_serializers.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
require 'newbase/active_model/serializer'
|
||||||
|
require 'newbase/active_model/serializer_support'
|
||||||
|
|
||||||
|
begin
|
||||||
|
require 'action_controller'
|
||||||
|
require 'newbase/action_controller/serialization'
|
||||||
|
|
||||||
|
ActiveSupport.on_load(:action_controller) do
|
||||||
|
include ::ActionController::Serialization
|
||||||
|
end
|
||||||
|
rescue LoadError
|
||||||
|
# rails not installed, continuing
|
||||||
|
end
|
||||||
172
test/integration/action_controller/serialization_test.rb
Normal file
172
test/integration/action_controller/serialization_test.rb
Normal file
@ -0,0 +1,172 @@
|
|||||||
|
require 'newbase/test_helper'
|
||||||
|
require 'newbase/active_model_serializers'
|
||||||
|
|
||||||
|
module ActionController
|
||||||
|
module Serialization
|
||||||
|
class ImplicitSerializerTest < ActionController::TestCase
|
||||||
|
class Model
|
||||||
|
include ActiveModel::SerializerSupport
|
||||||
|
|
||||||
|
def initialize(hash={})
|
||||||
|
@attributes = hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
@attributes[name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ModelSerializer < ActiveModel::Serializer
|
||||||
|
attributes :attr1, :attr2
|
||||||
|
end
|
||||||
|
|
||||||
|
class MyController < ActionController::Base
|
||||||
|
def render_using_implicit_serializer
|
||||||
|
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests MyController
|
||||||
|
|
||||||
|
def test_render_using_implicit_serializer
|
||||||
|
get :render_using_implicit_serializer
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"attr1":"value1","attr2":"value2"}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ImplicitSerializerScopeTest < ActionController::TestCase
|
||||||
|
class Model
|
||||||
|
include ActiveModel::SerializerSupport
|
||||||
|
|
||||||
|
def initialize(hash={})
|
||||||
|
@attributes = hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
@attributes[name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ModelSerializer < ActiveModel::Serializer
|
||||||
|
attributes :attr1, :attr2
|
||||||
|
|
||||||
|
def attr2
|
||||||
|
object.read_attribute_for_serialization(:attr2) + '-' + scope
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MyController < ActionController::Base
|
||||||
|
def render_using_implicit_serializer_and_scope
|
||||||
|
render :json => Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
'current_user'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests MyController
|
||||||
|
|
||||||
|
def test_render_using_implicit_serializer_and_scope
|
||||||
|
get :render_using_implicit_serializer_and_scope
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"attr1":"value1","attr2":"value2-current_user"}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ExplicitSerializerScopeTest < ActionController::TestCase
|
||||||
|
class Model
|
||||||
|
include ActiveModel::SerializerSupport
|
||||||
|
|
||||||
|
def initialize(hash={})
|
||||||
|
@attributes = hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
@attributes[name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ModelSerializer < ActiveModel::Serializer
|
||||||
|
attributes :attr1, :attr2
|
||||||
|
|
||||||
|
def attr2
|
||||||
|
object.read_attribute_for_serialization(:attr2) + '-' + scope
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MyController < ActionController::Base
|
||||||
|
def render_using_implicit_serializer_and_explicit_scope
|
||||||
|
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3'), scope: current_admin
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
'current_user'
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_admin
|
||||||
|
'current_admin'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests MyController
|
||||||
|
|
||||||
|
def test_render_using_implicit_serializer_and_explicit_scope
|
||||||
|
get :render_using_implicit_serializer_and_explicit_scope
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"attr1":"value1","attr2":"value2-current_admin"}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class OverridingSerializationScopeTest < ActionController::TestCase
|
||||||
|
class Model
|
||||||
|
include ActiveModel::SerializerSupport
|
||||||
|
|
||||||
|
def initialize(hash={})
|
||||||
|
@attributes = hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_attribute_for_serialization(name)
|
||||||
|
@attributes[name]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class ModelSerializer < ActiveModel::Serializer
|
||||||
|
attributes :attr1, :attr2
|
||||||
|
|
||||||
|
def attr2
|
||||||
|
object.read_attribute_for_serialization(:attr2) + '-' + scope
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class MyController < ActionController::Base
|
||||||
|
def render_overriding_serialization_scope
|
||||||
|
render json: Model.new(attr1: 'value1', attr2: 'value2', attr3: 'value3')
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
'current_user'
|
||||||
|
end
|
||||||
|
|
||||||
|
def serialization_scope
|
||||||
|
'current_admin'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests MyController
|
||||||
|
|
||||||
|
def test_render_overriding_serialization_scope
|
||||||
|
get :render_overriding_serialization_scope
|
||||||
|
assert_equal 'application/json', @response.content_type
|
||||||
|
assert_equal '{"attr1":"value1","attr2":"value2-current_admin"}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -1,5 +1,19 @@
|
|||||||
ENV['RAILS_ENV'] = 'test'
|
|
||||||
|
|
||||||
require 'bundler/setup'
|
require 'bundler/setup'
|
||||||
require 'rails'
|
require 'newbase/active_model_serializers'
|
||||||
require 'rails/test_help'
|
require 'test/unit'
|
||||||
|
|
||||||
|
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