Skip to content

Commit c19a254

Browse files
committed
Merge pull request #632 from gauthier-delacroix/Default_namespace_inheritance
Default namespace option
2 parents 21fe89e + 5ae536c commit c19a254

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
* Added a "prefix" option in case you want to use a different version of serializer.
2626

27+
* Serializers default namespace can be set in `default_serializer_options` and inherited by associations.
28+
2729
# VERSION 0.8.1
2830

2931
* Fix bug whereby a serializer using 'options' would blow up.

lib/action_controller/serialization.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def serialization_scope
8080

8181
def build_json_serializer(resource, options = {})
8282
options = default_serializer_options.merge(options)
83+
@namespace_for_serializer = options.fetch(:namespace, nil)
8384

8485
if serializer = options.fetch(:serializer, default_serializer(resource))
8586
options[:scope] = serialization_scope unless options.has_key?(:scope)

lib/active_model/array_serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def json_key
3535

3636
def serializer_for(item)
3737
serializer_class = @each_serializer || Serializer.serializer_for(item, namespace: @namespace) || DefaultSerializer
38-
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic)
38+
serializer_class.new(item, scope: scope, key_format: key_format, only: @only, except: @except, polymorphic: @polymorphic, namespace: @namespace)
3939
end
4040

4141
def serializable_object

lib/active_model/serializer.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def initialize(object, options={})
131131
@except = options[:except] ? Array(options[:except]) : nil
132132
@key_format = options[:key_format]
133133
@context = options[:context]
134+
@namespace = options[:namespace]
134135
end
135136
attr_accessor :object, :scope, :root, :meta_key, :meta, :key_format, :context, :polymorphic
136137

@@ -216,7 +217,8 @@ def build_serializer(association)
216217
end
217218

218219
def association_options_for_serializer(association)
219-
prefix = association.options[:prefix]
220+
prefix = association.options[:prefix]
221+
namespace = association.options[:namespace] || @namespace || self.namespace
220222

221223
{ scope: scope }.tap do |opts|
222224
opts[:namespace] = namespace if namespace

test/integration/action_controller/namespaced_serialization_test.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,54 @@ def test_array_fallback_to_a_version_without_namespace
4343
assert_serializer CommentSerializer
4444
end
4545
end
46+
47+
class OptionNamespacedSerializationTest < ActionController::TestCase
48+
class MyController < ActionController::Base
49+
def default_serializer_options
50+
{
51+
namespace: TestNamespace
52+
}
53+
end
54+
55+
def render_profile_with_namespace_option
56+
render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
57+
end
58+
59+
def render_profiles_with_namespace_option
60+
render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
61+
end
62+
63+
def render_comment
64+
render json: Comment.new(content: 'Comment 1')
65+
end
66+
67+
def render_comments
68+
render json: [Comment.new(content: 'Comment 1')]
69+
end
70+
end
71+
72+
tests MyController
73+
74+
def test_render_profile_with_namespace_option
75+
get :render_profile_with_namespace_option
76+
assert_serializer TestNamespace::ProfileSerializer
77+
end
78+
79+
def test_render_profiles_with_namespace_option
80+
get :render_profiles_with_namespace_option
81+
assert_serializer TestNamespace::ProfileSerializer
82+
end
83+
84+
def test_fallback_to_a_version_without_namespace
85+
get :render_comment
86+
assert_serializer CommentSerializer
87+
end
88+
89+
def test_array_fallback_to_a_version_without_namespace
90+
get :render_comments
91+
assert_serializer CommentSerializer
92+
end
93+
end
94+
4695
end
4796
end

0 commit comments

Comments
 (0)