mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Merge pull request #811 from mateomurphy/scope
Reimplement serialization scope and scope_name
This commit is contained in:
commit
d55f3b33b4
@ -8,6 +8,16 @@ module ActionController
|
|||||||
|
|
||||||
ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
|
ADAPTER_OPTION_KEYS = [:include, :fields, :root, :adapter]
|
||||||
|
|
||||||
|
included do
|
||||||
|
class_attribute :_serialization_scope
|
||||||
|
self._serialization_scope = :current_user
|
||||||
|
end
|
||||||
|
|
||||||
|
def serialization_scope
|
||||||
|
send(_serialization_scope) if _serialization_scope &&
|
||||||
|
respond_to?(_serialization_scope, true)
|
||||||
|
end
|
||||||
|
|
||||||
def get_serializer(resource)
|
def get_serializer(resource)
|
||||||
@_serializer ||= @_serializer_opts.delete(:serializer)
|
@_serializer ||= @_serializer_opts.delete(:serializer)
|
||||||
@_serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
@_serializer ||= ActiveModel::Serializer.serializer_for(resource)
|
||||||
@ -29,6 +39,10 @@ module ActionController
|
|||||||
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
options.partition { |k, _| ADAPTER_OPTION_KEYS.include? k }.map { |h| Hash[h] }
|
||||||
|
|
||||||
if use_adapter? && (serializer = get_serializer(resource))
|
if use_adapter? && (serializer = get_serializer(resource))
|
||||||
|
|
||||||
|
@_serializer_opts[:scope] ||= serialization_scope
|
||||||
|
@_serializer_opts[:scope_name] = _serialization_scope
|
||||||
|
|
||||||
# omg hax
|
# omg hax
|
||||||
object = serializer.new(resource, @_serializer_opts)
|
object = serializer.new(resource, @_serializer_opts)
|
||||||
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
|
adapter = ActiveModel::Serializer::Adapter.create(object, @_adapter_opts)
|
||||||
@ -38,5 +52,11 @@ module ActionController
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def serialization_scope(scope)
|
||||||
|
self._serialization_scope = scope
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -136,13 +136,21 @@ module ActiveModel
|
|||||||
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :object, :root, :meta, :meta_key
|
attr_accessor :object, :root, :meta, :meta_key, :scope
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
@object = object
|
@object = object
|
||||||
@root = options[:root] || (self.class._root ? self.class.root_name : false)
|
@root = options[:root] || (self.class._root ? self.class.root_name : false)
|
||||||
@meta = options[:meta]
|
@meta = options[:meta]
|
||||||
@meta_key = options[:meta_key]
|
@meta_key = options[:meta_key]
|
||||||
|
@scope = options[:scope]
|
||||||
|
|
||||||
|
scope_name = options[:scope_name]
|
||||||
|
if scope_name && !respond_to?(scope_name)
|
||||||
|
self.class.class_eval do
|
||||||
|
define_method scope_name, lambda { scope }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
|
|||||||
67
test/action_controller/serialization_scope_name_test.rb
Normal file
67
test/action_controller/serialization_scope_name_test.rb
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
require 'test_helper'
|
||||||
|
require 'pathname'
|
||||||
|
|
||||||
|
class DefaultScopeNameTest < ActionController::TestCase
|
||||||
|
TestUser = Struct.new(:name, :admin)
|
||||||
|
|
||||||
|
class UserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :admin?
|
||||||
|
def admin?
|
||||||
|
current_user.admin
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class UserTestController < ActionController::Base
|
||||||
|
protect_from_forgery
|
||||||
|
|
||||||
|
before_filter { request.format = :json }
|
||||||
|
|
||||||
|
def current_user
|
||||||
|
TestUser.new('Pete', false)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_new_user
|
||||||
|
render json: TestUser.new('pete', false), serializer: UserSerializer, adapter: :json_api
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests UserTestController
|
||||||
|
|
||||||
|
def test_default_scope_name
|
||||||
|
get :render_new_user
|
||||||
|
assert_equal '{"users":{"admin?":false}}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class SerializationScopeNameTest < ActionController::TestCase
|
||||||
|
TestUser = Struct.new(:name, :admin)
|
||||||
|
|
||||||
|
class AdminUserSerializer < ActiveModel::Serializer
|
||||||
|
attributes :admin?
|
||||||
|
def admin?
|
||||||
|
current_admin.admin
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class AdminUserTestController < ActionController::Base
|
||||||
|
protect_from_forgery
|
||||||
|
|
||||||
|
serialization_scope :current_admin
|
||||||
|
before_filter { request.format = :json }
|
||||||
|
|
||||||
|
def current_admin
|
||||||
|
TestUser.new('Bob', true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def render_new_user
|
||||||
|
render json: TestUser.new('pete', false), serializer: AdminUserSerializer, adapter: :json_api
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
tests AdminUserTestController
|
||||||
|
|
||||||
|
def test_override_scope_name_with_controller
|
||||||
|
get :render_new_user
|
||||||
|
assert_equal '{"admin_users":{"admin?":true}}', @response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user