Skip to content

Allow default exposed value to be false or any empty data #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

* Your contribution here.
* [#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).

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

Expand Down
2 changes: 1 addition & 1 deletion lib/grape_entity/exposure/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def valid_value(entity, options)
return unless valid?(entity)

output = value(entity, options)
output.blank? && @default_value.present? ? @default_value : output
output.blank? && !@default_value.nil? ? @default_value : output
end

def should_return_key?(options)
Expand Down
4 changes: 2 additions & 2 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ def initialize(a, b, c)
context 'when default option is set' do
it 'exposes default values for attributes' do
subject.expose(:a, default: 'a')
subject.expose(:b, default: 'b')
subject.expose(:b, default: false)
subject.expose(:c, default: 'c')
expect(subject.represent(model).serializable_hash).to eq(a: 'a', b: 'b', c: 'value')
expect(subject.represent(model).serializable_hash).to eq(a: 'a', b: false, c: 'value')
end
end

Expand Down