Enhance Rails' delegate to allow customizing of the value returned when the delegation target is absent

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Enhance Rails' delegate to allow customizing of the value returned when the delegation target is absent

Bruno Sofiato
Hi guys. 

How do you feel about an enhancement to Rails' delegate method that enables us to define an arbitrary value which is bound to be returned whenever the delegation target is nil

I've came across an use case on which I was required to do so.  What I did was include the on_nil: option to delegate. I can use this new option the value that should be returned. The value can be either a literal or a Proc. When on_nil is not supplied the current behavior is followed (i.e. nil is returned). Here is an example:

class User < ActiveRecord::Base
  has_one :profile 
  delegate :age, to: :profile, allow_nil: true, on_nil: 21
end

User.new.age # 21 

Do you guys think this would be an useful enhancement to Rails' core ?
Best Regards


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Enhance Rails' delegate to allow customizing of the value returned when the delegation target is absent

Roque Pinel
Please see https://github.com/rails/rails/pull/11908

On Thursday, August 10, 2017 at 5:21:34 AM UTC-4, Bruno Sofiato wrote:
Hi guys. 

How do you feel about an enhancement to Rails' delegate method that enables us to define an arbitrary value which is bound to be returned whenever the delegation target is nil

I've came across an use case on which I was required to do so.  What I did was include the on_nil: option to delegate. I can use this new option the value that should be returned. The value can be either a literal or a Proc. When on_nil is not supplied the current behavior is followed (i.e. nil is returned). Here is an example:

class User < ActiveRecord::Base
  has_one :profile 
  delegate :age, to: :profile, allow_nil: true, on_nil: 21
end

User.new.age # 21 

Do you guys think this would be an useful enhancement to Rails' core ?
Best Regards


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Enhance Rails' delegate to allow customizing of the value returned when the delegation target is absent

Bruno Sofiato
Yeah. That's pretty much the same features. Well no harm in asking ! Thanks for pointing that out.

Em quinta-feira, 10 de agosto de 2017 09:01:34 UTC-3, Roque Pinel escreveu:
Please see <a href="https://github.com/rails/rails/pull/11908" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F11908\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFW0tWlu3hpy1_MtysnKmX1Il0-Fg&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F11908\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFW0tWlu3hpy1_MtysnKmX1Il0-Fg&#39;;return true;">https://github.com/rails/rails/pull/11908

On Thursday, August 10, 2017 at 5:21:34 AM UTC-4, Bruno Sofiato wrote:
Hi guys. 

How do you feel about an enhancement to Rails' delegate method that enables us to define an arbitrary value which is bound to be returned whenever the delegation target is nil

I've came across an use case on which I was required to do so.  What I did was include the on_nil: option to delegate. I can use this new option the value that should be returned. The value can be either a literal or a Proc. When on_nil is not supplied the current behavior is followed (i.e. nil is returned). Here is an example:

class User < ActiveRecord::Base
  has_one :profile 
  delegate :age, to: :profile, allow_nil: true, on_nil: 21
end

User.new.age # 21 

Do you guys think this would be an useful enhancement to Rails' core ?
Best Regards


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Loading...