I find myself writing ugly code like the following a LOT: <td><%=emp.title%></td> <td><%=emp.department.name if !emp.department.nil? %></td> <td><%=emp.site.name if !emp.site.nil?%></td> <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? or ! emp.site.phone.empty?)) emp.site.phone else " " end%> </td> Notice all those nil? checks? Is there no easier or cleaner way of doing this? Thanks, Jake --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Or...if you use table borders, you end up doing an even uglier solution to avoid non-rendered cells for empty contents in Gecko-based browsers (like Firefox): <tr> <td><%=emp.last_name || " "%></td> <td><%=emp.first_name || " "%></td> <td><%=emp.title || " " %></td> <td><%=if !emp.department.nil?: emp.department.name else " "; end %></td> <td><%=emp.site.name if !emp.site.nil?%></td> <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? and !emp.site.phone.empty?)) emp.site.phone else " " end%> </td> <td><%=emp.extension || " " %></td> <td><%=emp.mobile || " "%></td> <td><%=emp.pager|| " "%></td> <td><%=emp.alternate_phone|| " "%></td> </tr> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> I find myself writing ugly code like the following a LOT: > > <td><%=emp.title%></td> > <td><%=emp.department.name if !emp.department.nil? %></td> > <td><%=emp.site.name if !emp.site.nil?%></td> > <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? or ! > emp.site.phone.empty?)) > emp.site.phone > else > " " > end%> > </td> > > Notice all those nil? checks? Is there no easier or cleaner way of > doing this?Well, you could disable the whiny nil in config/environments/.... or you could fix them in the controller/model before they get to your view, or you could do one of these which is a little nicer: obj.attr unless obj.attr.nil? obj.attr rescue '' '' You might also try .blank? instead of .nil? in your cases. -philip --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/10/07, Jake Cutter <cutter38-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I find myself writing ugly code like the following a LOT: > > <td><%=emp.title%></td> > <td><%=emp.department.name if !emp.department.nil? %></td> > <td><%=emp.site.name if !emp.site.nil?%></td> > <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? or ! > emp.site.phone.empty?)) > emp.site.phone > else > " " > end%> > </td> > > Notice all those nil? checks? Is there no easier or cleaner way of > doing this?Hmm, perhaps one of those "message-eating nils" [1] might have a place here: class Object def blanker Blanker.new(self) end end class Blanker instance_methods.each{|name| undef_method(name) unless name =~ /\A__/} def initialize(receiver) @receiver = receiver end def to_s @receiver.to_s end def method_missing(*args) unless @receiver.nil? @receiver = @receiver.send(*args) end self end end Now, <%= emp.blanker.department.name %> should do what you want. A few other points:> <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? or ! > emp.site.phone.empty?)) > emp.site.phone > else > " " > end%> > </td>activesupport lets you replace (foo.nil? || foo.empty?) with foo.blank? (I''m assuming you typo''d the logic a bit there...) That gets us to: <td><%=if !emp.site.nil? && !emp.site.phone.blank? emp.site.phone else " " end%> </td> As for the '' '' thing, you might like to define an #or_nbsp method: class Object def or_nbsp str = to_s str =~ /\S/ ? str : '' '' end end Which gets us to: <td><%=(emp.site && emp.site.phone).or_nbsp %></td> Or, combining with the #blanker thing above: <td><%=emp.blanker.site.phone.or_nbsp %></td>> Thanks, > JakeWelcome! George. [1] http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/17785 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
AS a general "good practice" (skinny controller & views, fat model) this is more for the Business logic job: theres a very conveniant function called delegate. Here''s more info about it : http://brian.maybeyoureinsane.net/blog/2006/12/15/law-of-demeter-or-how-to-avoid-coding-yourself-into-a-corner-in-rails/ On 11 fév, 11:23, "George Ogata" <george.og...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2/10/07, Jake Cutter <cutte...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > I find myself writing ugly code like the following a LOT: > > > <td><%=emp.title%></td> > > <td><%=emp.department.name if !emp.department.nil? %></td> > > <td><%=emp.site.name if !emp.site.nil?%></td> > > <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? or ! > > emp.site.phone.empty?)) > > emp.site.phone > > else > > " " > > end%> > > </td> > > > Notice all those nil? checks? Is there no easier or cleaner way of > > doing this? > > Hmm, perhaps one of those "message-eating nils" [1] might have a place here: > > class Object > def blanker > Blanker.new(self) > end > end > > class Blanker > instance_methods.each{|name| undef_method(name) unless name =~ /\A__/} > def initialize(receiver) > @receiver = receiver > end > def to_s > @receiver.to_s > end > def method_missing(*args) > unless @receiver.nil? > @receiver = @receiver.send(*args) > end > self > end > end > > Now, <%= emp.blanker.department.name %> should do what you want. > > A few other points: > > > <td><%=if (!emp.site.nil? and (!emp.site.phone.nil? or ! > > emp.site.phone.empty?)) > > emp.site.phone > > else > > " " > > end%> > > </td> > > activesupport lets you replace (foo.nil? || foo.empty?) with > foo.blank? (I''m assuming you typo''d the logic a bit there...) > > That gets us to: > > <td><%=if !emp.site.nil? && !emp.site.phone.blank? > emp.site.phone > else > " " > end%> > </td> > > As for the '' '' thing, you might like to define an #or_nbsp method: > > class Object > def or_nbsp > str = to_s > str =~ /\S/ ? str : '' '' > end > end > > Which gets us to: > > <td><%=(emp.site && emp.site.phone).or_nbsp %></td> > > Or, combining with the #blanker thing above: > > <td><%=emp.blanker.site.phone.or_nbsp %></td> > > > Thanks, > > Jake > > Welcome! > George. > > [1]http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/17785--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/12/07, Charly <charlysisto-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > AS a general "good practice" (skinny controller & views, fat model) > this is more for the Business logic job: > theres a very conveniant function called delegate. Here''s more info > about it : > http://brian.maybeyoureinsane.net/blog/2006/12/15/law-of-demeter-or-how-to-avoid-coding-yourself-into-a-corner-in-rails/Hi Charly, Thanks for the link. Yeah, that''s a good point about Demeter; I guess that''s another oft-cited reason to discourage the message-eating-nil thing. Unfortunately though, #delegate won''t check for nils. Not that you couldn''t define your own variant which does easily enough. George. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/12/07, George Ogata <george.ogata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > On 2/12/07, Charly <charlysisto-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > AS a general "good practice" (skinny controller & views, fat model) > > this is more for the Business logic job: > > theres a very conveniant function called delegate. Here''s more info > > about it : > > > http://brian.maybeyoureinsane.net/blog/2006/12/15/law-of-demeter-or-how-to-avoid-coding-yourself-into-a-corner-in-rails/ > > Hi Charly, > > Thanks for the link. > > Yeah, that''s a good point about Demeter; I guess that''s another > oft-cited reason to discourage the message-eating-nil thing. > Unfortunately though, #delegate won''t check for nils. Not that you > couldn''t define your own variant which does easily enough. > > George.This doesn''t sound right in my own ears but I can''t put my finger on why. What if the method_missing in Nil was replaced by something like class Nil def method_missing( method, *args, &block ) nil end end This way if there is a nil at any point along the chain at the end it will be a nil. Eg, if department is nil, name will also return nil <%= emp.department.name || "No Department Set" %> Just a thought, but is there anything wrong with this approach? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You could take a look at the Null Object pattern, if appropriate for your model. I''ve find this quite handy in my RoR apps. A link with an implementation for active record associations below: http://blog.craigambrose.com/articles/2006/09/22/active-record-associations-and-the-null-object-pattern Cheers, Roland --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/13/07, Roland Swingler <roland.swingler-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > You could take a look at the Null Object pattern, if appropriate for > your model. I''ve find this quite handy in my RoR apps. A link with an > implementation for active record associations below: > > > http://blog.craigambrose.com/articles/2006/09/22/active-record-associations-and-the-null-object-pattern > > Cheers, > RolandRoland, Thanx for the link, I noticed in the comments that Craig has shown nil chaining as well but distances himself from it. This confirms my suspicion that there may be something wrong with doing this but for the life of me I don''t know what. Cheers Daniel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/13/07, Daniel N <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 2/12/07, George Ogata <george.ogata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 2/12/07, Charly <charlysisto-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > AS a general "good practice" (skinny controller & views, fat model) > > > this is more for the Business logic job: > > > theres a very conveniant function called delegate. Here''s more info > > > about it : > > > > http://brian.maybeyoureinsane.net/blog/2006/12/15/law-of-demeter-or-how-to-avoid-coding-yourself-into-a-corner-in-rails/ > > > > Hi Charly, > > > > Thanks for the link. > > > > Yeah, that''s a good point about Demeter; I guess that''s another > > oft-cited reason to discourage the message-eating-nil thing. > > Unfortunately though, #delegate won''t check for nils. Not that you > > couldn''t define your own variant which does easily enough. > > > > George. > > > This doesn''t sound right in my own ears but I can''t put my finger on why. > What if the method_missing in Nil was replaced by something like > > class Nil > > def method_missing( method, *args, &block ) > nil > end > > end > > This way if there is a nil at any point along the chain at the end it will > be a nil. Eg, if department is nil, name will also return nil > > <%= emp.department.name || "No Department Set" %> > > Just a thought, but is there anything wrong with this approach?It''s been discussed at length before: http://rubyurl.com/S8A I think the main argument is it can hide errors easily (or make them harder to track down). For example, if you typo''d a method name, it''d treat it the same as if the attribute was nil. Regards, George. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/14/07, George Ogata <george.ogata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 2/13/07, Daniel N <has.sox-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 2/12/07, George Ogata <george.ogata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > On 2/12/07, Charly <charlysisto-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > > > AS a general "good practice" (skinny controller & views, fat model) > > > > this is more for the Business logic job: > > > > theres a very conveniant function called delegate. Here''s more info > > > > about it : > > > > > > http://brian.maybeyoureinsane.net/blog/2006/12/15/law-of-demeter-or-how-to-avoid-coding-yourself-into-a-corner-in-rails/ > > > > > > Hi Charly, > > > > > > Thanks for the link. > > > > > > Yeah, that''s a good point about Demeter; I guess that''s another > > > oft-cited reason to discourage the message-eating-nil thing. > > > Unfortunately though, #delegate won''t check for nils. Not that you > > > couldn''t define your own variant which does easily enough. > > > > > > George. > > > > > > This doesn''t sound right in my own ears but I can''t put my finger on why. > > What if the method_missing in Nil was replaced by something like > > > > class Nil > > > > def method_missing( method, *args, &block ) > > nil > > end > > > > end > > > > This way if there is a nil at any point along the chain at the end it will > > be a nil. Eg, if department is nil, name will also return nil > > > > <%= emp.department.name || "No Department Set" %> > > > > Just a thought, but is there anything wrong with this approach? > > It''s been discussed at length before: > > http://rubyurl.com/S8A > > I think the main argument is it can hide errors easily (or make them > harder to track down). For example, if you typo''d a method name, it''d > treat it the same as if the attribute was nil. > > Regards, > George.Thanx for the link. That was a great thread on the subject. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 2/11/07, George Ogata <george.ogata-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > class Object > def or_nbsp > str = to_s > str =~ /\S/ ? str : '' '' > end > endThis works nicely. Thanks George! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---