grape-entity 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: effe94d496aff5e6a72b380ddaac6daa7a12b07d
4
- data.tar.gz: 324af303ff5358862861de75be31cb3b0d5d5eab
3
+ metadata.gz: 4165ed088e82e10584f53bf9436053d18c647b41
4
+ data.tar.gz: be2d0321743d7d27dcc1d3c1f30a9e5483e5c92d
5
5
  SHA512:
6
- metadata.gz: 53357805d3b83631c727a22ce88d81ecb1fdba53a225a063bb67a68d711ac8c86ea67d6297938f4d2192dbf00f591833aff7f00e0af60b8ca984292840e09c9c
7
- data.tar.gz: 02d43ad57fb18f7eb9b3927a346f447233fd42d6f73f0591a296c8f0378a5dc16c7ff8cbe53c606c6720b9040894ea917e530618c7738814d87f5ccb5ef30f1f
6
+ metadata.gz: 2059bf88e77709d197f923e0c4659747d83ed2a859325cecdcbb286cdc828a82a07a8c6571df886be941059b6c2c98110327c04809d453824ff377d387919c5e
7
+ data.tar.gz: e2c16d535392af2daf37b834589f63933f66f960c15e199956419dd894a3bd95ab06b76a53b3ca594714f854845c4137b65add706b8be02c8d6e580b7d3e2062
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ 0.4.2 (2014-04-03)
2
+ ==================
3
+
4
+ * [#60](https://github.com/intridea/grape-entity/issues/59): Performance issues introduced by nested exposures - [@AlexYankee](https://github.com/AlexYankee).
5
+ * [#60](https://github.com/intridea/grape-entity/issues/57): Nested exposure double-exposes a field - [@AlexYankee](https://github.com/AlexYankee).
6
+
1
7
  0.4.1 (2014-02-13)
2
8
  ==================
3
9
 
@@ -136,9 +136,13 @@ module Grape
136
136
  options[:proc] = block if block_given? && block.parameters.any?
137
137
 
138
138
  @nested_attributes ||= []
139
+
139
140
  args.each do |attribute|
140
141
  unless @nested_attributes.empty?
141
142
  attribute = "#{@nested_attributes.last}__#{attribute}"
143
+ options[:nested] = true
144
+ nested_exposures_hash[@nested_attributes.last.to_sym] ||= {}
145
+ nested_exposures_hash[@nested_attributes.last.to_sym][attribute.to_sym] = options
142
146
  end
143
147
 
144
148
  exposures[attribute.to_sym] = options
@@ -180,6 +184,24 @@ module Grape
180
184
  @exposures
181
185
  end
182
186
 
187
+ class << self
188
+ attr_accessor :_nested_exposures_hash
189
+
190
+ def nested_exposures_hash
191
+ self._nested_exposures_hash ||= {}
192
+ end
193
+
194
+ def nested_exposures
195
+ value = nested_exposures_hash
196
+
197
+ if superclass.respond_to? :nested_exposures
198
+ value = superclass.nested_exposures.deep_merge(value)
199
+ end
200
+
201
+ value
202
+ end
203
+ end
204
+
183
205
  # Returns a hash, the keys are symbolized references to fields in the entity,
184
206
  # the values are document keys in the entity's documentation key. When calling
185
207
  # #docmentation, any exposure without a documentation key will be ignored.
@@ -324,7 +346,7 @@ module Grape
324
346
  end
325
347
 
326
348
  def valid_exposures
327
- exposures.select do |attribute, exposure_options|
349
+ exposures.reject { |a, options| options[:nested] }.select do |attribute, exposure_options|
328
350
  valid_exposure?(attribute, exposure_options)
329
351
  end
330
352
  end
@@ -390,7 +412,7 @@ module Grape
390
412
  end
391
413
 
392
414
  def self.nested_exposures_for(attribute)
393
- exposures.select { |a, _| a.to_s =~ /^#{attribute}__/ }
415
+ nested_exposures[attribute] || {}
394
416
  end
395
417
 
396
418
  def value_for(attribute, options = {})
@@ -1,3 +1,3 @@
1
1
  module GrapeEntity
2
- VERSION = '0.4.1'
2
+ VERSION = '0.4.2'
3
3
  end
@@ -92,9 +92,9 @@ describe Grape::Entity do
92
92
 
93
93
  subject.exposures.should == {
94
94
  awesome: {},
95
- awesome__nested: {},
96
- awesome__nested__moar_nested: { as: 'weee' },
97
- awesome__another_nested: { using: 'Awesome' }
95
+ awesome__nested: { nested: true },
96
+ awesome__nested__moar_nested: { as: 'weee', nested: true },
97
+ awesome__another_nested: { using: 'Awesome', nested: true }
98
98
  }
99
99
  end
100
100
 
@@ -110,6 +110,68 @@ describe Grape::Entity do
110
110
  }
111
111
  end
112
112
 
113
+ it 'does not represent attributes, declared inside nested exposure, outside of it' do
114
+ subject.expose :awesome do
115
+ subject.expose(:nested) { |_| "value" }
116
+ subject.expose(:another_nested) { |_| "value" }
117
+ subject.expose :second_level_nested do
118
+ subject.expose(:deeply_exposed_attr) { |_| "value" }
119
+ end
120
+ end
121
+
122
+ subject.represent({}).serializable_hash.should == {
123
+ awesome: {
124
+ nested: "value",
125
+ another_nested: "value",
126
+ second_level_nested: {
127
+ deeply_exposed_attr: "value"
128
+ }
129
+ }
130
+ }
131
+ end
132
+
133
+ it "complex nested attributes" do
134
+ class ClassRoom < Grape::Entity
135
+ expose(:parents, using: 'Parent') { |_| [{}, {}] }
136
+ end
137
+
138
+ class Person < Grape::Entity
139
+ expose :user do
140
+ expose(:in_first) { |_| 'value' }
141
+ end
142
+ end
143
+
144
+ class Student < Person
145
+ expose :user do
146
+ expose(:user_id) { |_| 'value' }
147
+ expose(:user_display_id, as: :display_id) { |_| 'value' }
148
+ end
149
+ end
150
+
151
+ class Parent < Person
152
+ expose(:children, using: 'Student') { |_| [{}, {}] }
153
+ end
154
+
155
+ ClassRoom.represent({}).serializable_hash.should == {
156
+ parents: [
157
+ {
158
+ user: { in_first: 'value' },
159
+ children: [
160
+ { user: { in_first: 'value', user_id: "value", display_id: "value" } },
161
+ { user: { in_first: 'value', user_id: "value", display_id: "value" } }
162
+ ]
163
+ },
164
+ {
165
+ user: { in_first: 'value' },
166
+ children: [
167
+ { user: { in_first: 'value', user_id: "value", display_id: "value" } },
168
+ { user: { in_first: 'value', user_id: "value", display_id: "value" } }
169
+ ]
170
+ }
171
+ ]
172
+ }
173
+ end
174
+
113
175
  it 'is safe if its nested exposures are safe' do
114
176
  subject.with_options safe: true do
115
177
  subject.expose :awesome do
@@ -121,6 +183,7 @@ describe Grape::Entity do
121
183
  end
122
184
 
123
185
  valid_keys = subject.represent({}).valid_exposures.keys
186
+
124
187
  valid_keys.include?(:awesome).should == true && \
125
188
  valid_keys.include?(:not_awesome).should == false
126
189
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-13 00:00:00.000000000 Z
11
+ date: 2014-04-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport