Skip to content

Commit f35ad97

Browse files
author
Michaël Villeneuve
committed
Fix json root
1 parent 2fb1ce3 commit f35ad97

File tree

5 files changed

+29
-16
lines changed

5 files changed

+29
-16
lines changed

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
active_mappers (0.1.1)
4+
active_mappers (0.2.0)
55
activesupport (>= 4.2)
66

77
GEM
@@ -12,7 +12,7 @@ GEM
1212
i18n (>= 0.7, < 2)
1313
minitest (~> 5.1)
1414
tzinfo (~> 1.1)
15-
concurrent-ruby (1.0.5)
15+
concurrent-ruby (1.1.3)
1616
i18n (1.1.1)
1717
concurrent-ruby (~> 1.0)
1818
minitest (5.11.3)

active_mappers-0.2.0.gem

5.5 KB
Binary file not shown.

active_mappers.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'active_mappers'
3-
s.version = '0.1.1'
3+
s.version = '0.2.0'
44
s.date = '2018-10-14'
55
s.summary = 'Slick, fast view layer for you Rails API.'
66
s.description = 'Fast, simple, declarative way to design your API\'s view layer'

lib/active_mappers.rb

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'active_support'
22
require 'active_support/core_ext/object/try'
33
require 'active_support/core_ext/string/inflections'
4+
require 'active_support/core_ext/string'
45
require_relative 'core_ext/hash'
56

67
module ActiveMappers
@@ -34,21 +35,21 @@ def self.relation(key, mapper = nil, optional_path = nil)
3435

3536
each do |resource|
3637
mapper ||= "::#{resource.send(key).class.name}Mapper".constantize
37-
{ key => mapper.with(path.to_s.split('.').inject(resource, :try)) }
38+
{ key => mapper.with(path.to_s.split('.').inject(resource, :try), rootless: true) }
3839
end
3940
end
4041

4142
def self.polymorphic(key)
4243
each do |resource|
4344
resource_mapper = "::#{resource.send("#{key}_type")}Mapper".constantize
44-
{ key => resource_mapper.with(resource.send(key)) }
45+
{ key => resource_mapper.with(resource.send(key), rootless: true) }
4546
end
4647
end
4748

4849
def self.acts_as_polymorph
4950
each do |resource|
5051
mapper = "::#{resource.class}Mapper".constantize
51-
mapper.with(resource)
52+
mapper.with(resource, rootless: true)
5253
rescue NameError
5354
raise NotImplementedError, 'No mapper found for this type of resource'
5455
end
@@ -58,8 +59,21 @@ def self.each(&block)
5859
@@renderers[name] = (@@renderers[name] || []) << block
5960
end
6061

61-
def self.with(args)
62-
args.respond_to?(:each) ? all(args) : one(args)
62+
def self.with(args, options = {})
63+
if options[:rootless]
64+
args.respond_to?(:each) ? all(args) : one(args)
65+
else
66+
render_with_root(args, options)
67+
end
68+
end
69+
70+
def self.render_with_root(args, options = {})
71+
resource = options[:root] || self.name.gsub('Mapper', '').downcase
72+
if args.respond_to?(:each)
73+
{ resource.tableize.gsub('/', '_').to_sym => all(args) }
74+
else
75+
{ resource.to_sym => one(args) }
76+
end
6377
end
6478

6579
def self.all(collection)

test/test_active_mappers.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,23 @@ def test_can_render_a_list_of_resources
4040

4141
mapped_users = UserMapper.with(users)
4242

43-
assert_equal 5, mapped_users.size
44-
assert_equal '123', mapped_users[0][:id]
43+
assert_equal 5, mapped_users[:users].size
44+
assert_equal '123', mapped_users[:users][0][:id]
4545
end
4646

4747
def test_can_render_a_single_resource
4848
user = User.new('123', 'Michael', nil)
49-
assert_equal user.id, UserMapper.with(user)[:id]
49+
assert_equal user.id, UserMapper.with(user)[:user][:id]
5050
end
5151

5252
def test_each_can_be_used_to_declare_custom_attrs
5353
user = User.new('123', 'Michael', nil)
54-
assert_equal 'lol', UserMapper.with(user)[:lol]
54+
assert_equal 'lol', UserMapper.with(user)[:user][:lol]
5555
end
5656

5757
def test_each_can_be_chained
5858
user = User.new('123', 'Michael', nil)
59-
assert_equal 'lola', UserMapper.with(user)[:lola]
59+
assert_equal 'lola', UserMapper.with(user)[:user][:lola]
6060
end
6161

6262
class FriendShipMapper < ActiveMappers::Base
@@ -66,8 +66,7 @@ class FriendShipMapper < ActiveMappers::Base
6666
def test_relation_can_query_other_mapepr
6767
friend = Friend.new('124', 'Nicolas', nil)
6868
user = User.new('123', 'Michael', friend)
69-
70-
assert_equal 'Nicolas', FriendShipMapper.with(user)[:friend][:name]
69+
assert_equal 'Nicolas', FriendShipMapper.with(user, root: :user)[:user][:friend][:name]
7170
end
7271

7372
class ProfileMapper < ActiveMappers::Base
@@ -76,7 +75,7 @@ class ProfileMapper < ActiveMappers::Base
7675
def test_delegate_can_remap_attributes
7776
friend = Friend.new('124', 'Nicolas', nil)
7877
user = User.new('123', 'Michael', friend)
79-
assert_equal 'Nicolas', ProfileMapper.with(user)[:name]
78+
assert_equal 'Nicolas', ProfileMapper.with(user, root: :user)[:user][:name]
8079
end
8180

8281
def test_core_extensions_work_as_expected

0 commit comments

Comments
 (0)