Skip to content

Commit da81c97

Browse files
committed
Merge pull request #651 from gauthier-delacroix/Plural-default-root
Add plural_default_root config option
2 parents 40d6655 + efa22d9 commit da81c97

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

lib/active_model/serializer.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ def serializer_for(resource, options = {})
7272
alias root= _root=
7373

7474
def root_name
75-
name.demodulize.underscore.sub(/_serializer$/, '') if name
75+
if name
76+
root_name = name.demodulize.underscore.sub(/_serializer$/, '')
77+
CONFIG.plural_default_root ? root_name.pluralize : root_name
78+
end
7679
end
7780

7881
def attributes(*attrs)

test/unit/active_model/serializer/config_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ def test_apply_config_to_associations
7878
assert !association.embed_objects?
7979
assert association.embed_in_root
8080
assert_equal :lower_camel, association.key_format
81+
assert_equal 'post', PostSerializer.root_name
82+
CONFIG.plural_default_root = true
83+
assert_equal 'posts', PostSerializer.root_name
8184
ensure
8285
PostSerializer._associations[:comments] = old_association
8386
CONFIG.clear

0 commit comments

Comments
 (0)