Skip to content

Commit 48e5be7

Browse files
committed
Merge pull request #117 from ntxcode/fix-only-fields
Fix: using 'only' option with exposed attribute that is an entity
2 parents f7d1c5c + 14ec7b0 commit 48e5be7

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

.rubocop_todo.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
# This configuration was generated by `rubocop --auto-gen-config`
2-
# on 2015-03-16 03:40:21 -0300 using RuboCop version 0.28.0.
2+
# on 2015-03-22 20:12:08 -0300 using RuboCop version 0.28.0.
33
# The point is for the user to remove these configuration records
44
# one by one as the offenses are removed from the code base.
55
# Note that changes in the inspected code, or installation of new
66
# versions of RuboCop, may require this file to be generated again.
77

8-
# Offense count: 7
8+
# Offense count: 8
99
Metrics/AbcSize:
1010
Max: 51
1111

1212
# Offense count: 1
1313
# Configuration parameters: CountComments.
1414
Metrics/ClassLength:
15-
Max: 330
15+
Max: 337
1616

1717
# Offense count: 5
1818
Metrics/CyclomaticComplexity:
1919
Max: 17
2020

21-
# Offense count: 187
21+
# Offense count: 175
2222
# Configuration parameters: AllowURI, URISchemes.
2323
Metrics/LineLength:
24-
Max: 147
24+
Max: 146
2525

2626
# Offense count: 7
2727
# Configuration parameters: CountComments.
2828
Metrics/MethodLength:
2929
Max: 32
3030

31-
# Offense count: 4
31+
# Offense count: 5
3232
Metrics/PerceivedComplexity:
3333
Max: 15
3434

lib/grape_entity/entity.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,8 +499,8 @@ def should_return_attribute?(attribute, options)
499499
only_fields(options).include?(self.class.key_for(attribute))
500500
end
501501

502-
def only_fields(options)
503-
return {} unless options[:only]
502+
def only_fields(options, for_attribute = nil)
503+
return nil unless options[:only]
504504

505505
@only_fields ||= options[:only].inject({}) do |allowed_fields, attribute|
506506
if attribute.is_a?(Hash)
@@ -514,6 +514,12 @@ def only_fields(options)
514514

515515
allowed_fields
516516
end
517+
518+
if for_attribute && @only_fields[for_attribute].is_a?(Array)
519+
@only_fields[for_attribute]
520+
elsif for_attribute.nil?
521+
@only_fields
522+
end
517523
end
518524

519525
alias_method :as_json, :serializable_hash
@@ -650,7 +656,8 @@ def options_for_using(attribute, options)
650656
using_options = options.dup
651657
using_options.delete(:collection)
652658
using_options[:root] = nil
653-
using_options[:only] = only_fields(using_options)[attribute]
659+
using_options[:only] = only_fields(using_options, attribute)
660+
654661
using_options
655662
end
656663

spec/grape_entity/entity_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,19 @@ class Parent < Person
546546
expect(representation).to eq(id: nil, title: nil)
547547
end
548548
end
549+
550+
context 'attribute that is an entity itself' do
551+
it 'returns correctly the children entity attributes' do
552+
user_entity = Class.new(Grape::Entity)
553+
user_entity.expose(:id, :name, :email)
554+
555+
subject.expose(:id, :name, :phone)
556+
subject.expose(:user, using: user_entity)
557+
558+
representation = subject.represent(OpenStruct.new(user: {}), only: [:id, :name, :user], serializable: true)
559+
expect(representation).to eq(id: nil, name: nil, user: { id: nil, name: nil, email: nil })
560+
end
561+
end
549562
end
550563
end
551564

0 commit comments

Comments
 (0)