mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Merge pull request #632 from gauthier-delacroix/Default_namespace_inheritance
Default namespace option
This commit is contained in:
commit
c19a254154
@ -24,6 +24,8 @@
|
||||
|
||||
* Added a "prefix" option in case you want to use a different version of serializer.
|
||||
|
||||
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
|
||||
|
||||
# VERSION 0.8.1
|
||||
|
||||
* Fix bug whereby a serializer using 'options' would blow up.
|
||||
|
||||
@ -80,6 +80,7 @@ module ActionController
|
||||
|
||||
def build_json_serializer(resource, options = {})
|
||||
options = default_serializer_options.merge(options)
|
||||
@namespace_for_serializer = options.fetch(:namespace, nil)
|
||||
|
||||
if serializer = options.fetch(:serializer, default_serializer(resource))
|
||||
options[:scope] = serialization_scope unless options.has_key?(:scope)
|
||||
|
||||
@ -35,7 +35,7 @@ module ActiveModel
|
||||
|
||||
def serializer_for(item)
|
||||
serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer
|
||||
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic)
|
||||
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
|
||||
end
|
||||
|
||||
def serializable_object
|
||||
|
||||
@ -131,6 +131,7 @@ end
|
||||
@except = options[:except] ? Array(options[:except]) : nil
|
||||
@key_format = options[:key_format]
|
||||
@context = options[:context]
|
||||
@namespace = options[:namespace]
|
||||
end
|
||||
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
|
||||
|
||||
@ -216,7 +217,8 @@ end
|
||||
end
|
||||
|
||||
def association_options_for_serializer(association)
|
||||
prefix = association.options[:prefix]
|
||||
prefix = association.options[:prefix]
|
||||
namespace = association.options[:namespace] || @namespace || self.namespace
|
||||
|
||||
{ scope: scope }.tap do |opts|
|
||||
opts[:namespace] = namespace if namespace
|
||||
|
||||
@ -43,5 +43,54 @@ module ActionController
|
||||
assert_serializer CommentSerializer
|
||||
end
|
||||
end
|
||||
|
||||
class OptionNamespacedSerializationTest < ActionController::TestCase
|
||||
class MyController < ActionController::Base
|
||||
def default_serializer_options
|
||||
{
|
||||
namespace: TestNamespace
|
||||
}
|
||||
end
|
||||
|
||||
def render_profile_with_namespace_option
|
||||
render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
|
||||
end
|
||||
|
||||
def render_profiles_with_namespace_option
|
||||
render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
|
||||
end
|
||||
|
||||
def render_comment
|
||||
render json: Comment.new(content: 'Comment 1')
|
||||
end
|
||||
|
||||
def render_comments
|
||||
render json: [Comment.new(content: 'Comment 1')]
|
||||
end
|
||||
end
|
||||
|
||||
tests MyController
|
||||
|
||||
def test_render_profile_with_namespace_option
|
||||
get :render_profile_with_namespace_option
|
||||
assert_serializer TestNamespace::ProfileSerializer
|
||||
end
|
||||
|
||||
def test_render_profiles_with_namespace_option
|
||||
get :render_profiles_with_namespace_option
|
||||
assert_serializer TestNamespace::ProfileSerializer
|
||||
end
|
||||
|
||||
def test_fallback_to_a_version_without_namespace
|
||||
get :render_comment
|
||||
assert_serializer CommentSerializer
|
||||
end
|
||||
|
||||
def test_array_fallback_to_a_version_without_namespace
|
||||
get :render_comments
|
||||
assert_serializer CommentSerializer
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue
Block a user