Repository navigation
Object#presence should take a block - #13416
stephenprater wants to merge 1 commit into
Conversation
In order to have parallel behavior with methods like #each, presence should yield to a provided block if the object is present. This is similar to an anaphoric if in Scheme, and is discussed at length here: https://github.com/raganwald/homoiconic/blob/master/2012/05/anaphora.m You can achieve similar functionality by appending `try` in its block form to a presence call - but having presence evaluate the block itself avoids the (confusing) call to try.
|
Can you showcase a real use case for this? I can't really think of anything right now (too late here already :D) |
|
The use case for this is basically the same as an anaphoric if or an assign in a conditional. For an actual use case: ordered_products = Product.long_and_complicated_query
if ordered_products.present?
if sort.present?
ordered_products = order_products.reorder( by: sort.order_by)
else
ordered_products = order_products.reorder
end
end
ordered_products
# vs.
Product.long_and_complicated_query.presence do |ordered_products|
ordered_products.reorder by: sort.presence.order_by
endThat's not ACTUAL production code - but it's the gist of it. |
|
I think In any case, I think we could maybe write your code with something like this: ordered_products = Product.long_and_complicated_query
ordered_products = order_products.reorder(by: sort.order_by) if sort.present?
ordered_productsIn this example I believe you don't need to check for the presence of Thanks! |
|
You're right - it would actually be As for the other point - you'd be right - but uhh... confession time.... remember when I said it was the "gist" of the production code? That's because that's not the ActiveRecord reorder method and |
|
FTR I didn't write the terrible method - I'm just trying not to pile additional crap on top of it. |
|
On the other hand, I could see an argument for the block being called when the value is not present, consistent with
What is the purpose of having the block option for the first bullet point? |
|
Basically, calling the block is parallel to the behavior of |
|
I found these for you, maybe relevant: https://bugs.ruby-lang.org/issues/6373 In any case, this is a feature request and such discussion happens on the rails-core mailing list. Rails Github issues are meant for bugs: you might get better traction with this question on the mailing list. |
|
I still not convinced we need this feature. I also agree with @avit that That said I'm closing this one. Thank you for the contribution. |
In order to have parallel behavior with methods like #each, presence should
yield to a provided block if the object is present. This is
similar to an anaphoric if in Scheme, and is discussed at length here:
https://github.com/raganwald/homoiconic/blob/master/2012/05/anaphora.md
You can achieve similar functionality by appending
tryin its blockform to a presence call - but having presence evaluate the block itself
avoids the (confusing) call to try.