ruby-grape / ruby-grape/grape-entity
Be able to use `using`, `proc` and `format_with` together
Nobody has claimed this yet.
- Dominant language
- Ruby
- Stars
- 729
- Forks
- 154
- PR merge metrics
- No merged PRs in 30d
Description
I am reporting as an issue what I found https://github.com/ruby-grape/grape-entity/pull/225.
When calling format_with: with a block or using: option, the formatter is ignored:
# formatter `foo` defined in a helper
Grape::Entity.format_with :foo do |value|
binding.pry
end
# formatter `foo` is not called, totally ignored
expose :users, using: Entities::MyEntity, format_with: :foo do |instance, _opt|
instance.users
end
# formatter `foo` is not called, totally ignored
expose :users, using: Entities::MyEntity, format_with: :foo
# formatter `foo` is not called, totally ignored
with_options(format_with: :foo) do
expose :users, using: Entities::MyEntity
end
# only this works
expose :users, format_with: :foo
I can even call a non-existing formatter and any error is raised:
# formatter `bar` does not exist, again, totally ignored
expose :users, using: Entities::MyEntity, format_with: :bar do |instance, _opt|
instance.users
end
# formatter `bar` does not exist, again, totally ignored
expose :users, using: Entities:: MyEntity, format_with: :bar
# formatter `bar` does not exist, again, totally ignored
with_options(format_with: :bar) do
expose :users, using: Entities::MyEntity
end
# only this raises the error:
# NoMethodError: undefined method `bar' for #<Entities...
expose :users, format_with: :bar
The way I found to format in the cases above where the formatter is ignored is to mokey-patching Entity.
# Gemfile
gem "grape", "~> 0.16.2"
gem "grape-entity", "~> 0.5.1"
I found out we in fact cannot use both using, proc, format_with and nesting together as coded https://github.com/ruby-grape/grape-entity/blob/master/lib/grape_entity/exposure.rb#L17.
It would be a nice feature.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in lib/grape_entity/exposure.rb around line 17 and reproduce the shown expose and with_options combinations using using, proc, format_with, and nesting. Trace how these options are handled; done when format_with is honored for the combinations and missing formatters are not silently ignored.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100