Skip to content

Commit 4e12353

Browse files
norydevLeFnord
andauthored
Allow default exposed value to be false or any empty data (#371)
Using `present?` limits what can be set as `:default` and restricts falsy values like `false`, `''`, `[]` for example. I don't really see why not allow those (I have a use case for `false` for example, which is to ensure a nullable boolean value is always rendered as a boolean). Co-authored-by: peter scholz <[email protected]>
1 parent 3393516 commit 4e12353

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

99
* Your contribution here.
10+
* [#371](https://github.com/ruby-grape/grape-entity/pull/371): Allow default exposed value to be `false` or any empty data - [@norydev](https://github.com/norydev).
1011

1112
* [#352](https://github.com/ruby-grape/grape-entity/pull/369): Remove `FetchableObject` behavior. - [@danielvdao](https://github.com/danielvdao).
1213

lib/grape_entity/exposure/base.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def valid_value(entity, options)
8989
return unless valid?(entity)
9090

9191
output = value(entity, options)
92-
output.blank? && @default_value.present? ? @default_value : output
92+
output.blank? && !@default_value.nil? ? @default_value : output
9393
end
9494

9595
def should_return_key?(options)

spec/grape_entity/entity_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ def initialize(a, b, c)
247247
context 'when default option is set' do
248248
it 'exposes default values for attributes' do
249249
subject.expose(:a, default: 'a')
250-
subject.expose(:b, default: 'b')
250+
subject.expose(:b, default: false)
251251
subject.expose(:c, default: 'c')
252-
expect(subject.represent(model).serializable_hash).to eq(a: 'a', b: 'b', c: 'value')
252+
expect(subject.represent(model).serializable_hash).to eq(a: 'a', b: false, c: 'value')
253253
end
254254
end
255255

0 commit comments

Comments
 (0)