-
Notifications
You must be signed in to change notification settings - Fork 153
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
Fix or workaround for duplicate exposure caveat? #56
Comments
That would be a bug. Start by writing a test that demonstrates the problem. |
Added proof of concept here: Please let me know if you have any questions. |
@nickls Can you please confirm whether this is fixed on HEAD? |
This issue documented in caveats. Result
Workaround: class MediaEntity < Grape::Entity
expose :media_sound, as: :media, using: SoundEntity, if: lambda { |object, _| object.sound? } { |object, _| object.media }
expose :media_photo, as: :media, using: PhotoEntity, if: lambda { |object, _| object.photo? } { |object, _| object.media }
end |
Would love a pull request that addresses the spec failure mentioned above. |
Cool, I'll see what I can do. |
I took @donbobka's test and adapted it a bit: https://github.com/Morred/grape-entity/tree/double-exposure The test fails, and what happens is technically exactly what you're describing in the caveat section of the Readme, the second exposure overwrites the first exposure. Don't know if this is considered a bug (because you do document it in the Readme), but it would certainly be a lot more convenient if this did not happen. |
Completely forgot the pull request >.< It's there now. |
I don't see a bug here. Both https://gist.github.com/nickls/9560205 and #124 demonstrate documented behavior: exposure with the same name overwrite previously defined. However, It would be good to not overwrite them. I think # Grape::Entity:
expose :a
expose :a, if: -> (object, options) { object.some_condition? }
# Regular ruby code:
expose object.a
expose object.a if object.some_condition? Looks similar, right? I definitely want this feature to be implemented and I think I could do it. But it will break backward compatibility (i.e. that caveat in README about double exposures will gone) and it requires big changes in code. So what should be done in examples: expose :x, using: E1
expose :x, using: E2 Here expose :x, using: E1, if: -> { some_condition? }
expose :x, using: E2 Here expose :x, using: E1
expose :x, using: E2, if: -> { some_condition? } Here expose :x, using: E1, if: -> { first_condition? }
expose :x, using: E2, if: -> { second_condition? } Here:
I could write detailed failing specs for this and try to construct an implementation. |
@marshall-lee, I think of |
Yes and I like it. I just said it's not a bug but a documented caveat. Double exposure overwrites previously defined — not matter they're conditional or not. And since it's documented, implementing it the right way is not backward compatible. Though I think that it would be better not being compatible with quirky behavior :) |
I am ok with breaking backward compatibility here as long as we have an upgrade path for someone who wants that behavior. |
This one reaches many goals at once: - Fixes ruby-grape#56: now `exposures` is not a hash table but an array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - For ones who want previous rewriting behavior a `rewrite` option for `exposure` is introduced. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb` - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56: now `exposures` is not a hash table but an array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - For ones who want previous rewriting behavior a `rewrite` option for `exposure` is introduced. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb` - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56: now `exposures` is not a hash table but an array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - For ones who want previous rewriting behavior a `rewrite` option for `exposure` is introduced. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb` - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56: now `exposures` is not a hash table but an array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112 - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb` - Fixes ruby-grape#152 - Fixes ruby-grape#153 - Fixes ruby-grape#155 - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56: now `exposures` is not a hash table but an array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112 - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb` - Fixes ruby-grape#152 - Fixes ruby-grape#153 - Fixes ruby-grape#155 - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%. Version is bumbed to 0.5.0.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%. Version is bumbed to 0.5.0.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%. Version is bumbed to 0.5.0.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 20-30%.
This one reaches many goals at once: - Fixes ruby-grape#56. - `exposures` hash table is removed and substituted with `root_exposures` array. - Due to previous point, tree structure `nested_exposures` based on flat hash table with keys like `root_node__node__node` is removed because now double exposures don't rewrite previously defined so such tree structure is simply incorrect. `NestingExposure` with an array of children is introduced instead. - Ones who want an old rewriting behavior should manually `unexpose`. - Fixes ruby-grape#112. - Fixes ruby-grape#149: runtime `options` are now wrapped in an `Options` object. - Fixes ruby-grape#150: see new `spec/grape_entity/exposure_spec.rb`. - Fixes ruby-grape#152. - Fixes ruby-grape#153. - Fixes ruby-grape#155. - Fixes ruby-grape#156. - All exposure configuration and exposing strategies are extracted to the separate classes. - `key_for`, `name_for` internal methods are removed. - Much of the overhead is gone so performance is increased by 15-30%.
I'm looking to expose using different entities if a user is anonymous or not:
This doesn't work as you documented in the caveat section. Is there a good workaround?
Also, do you have a solution in mind for this?
The text was updated successfully, but these errors were encountered: