Hi there, my short question: Assume I have these 2 cases in my code : =========================== :name => h(agent.firstname) [...] :name => h(agent.sharedname) =========================== To stay DRY, I want this to become just 1 line, by replacing "first"/"shared" by a variable that I would set at the beginning. How would the code of this 1 new line and the initiation of the variable look like? Thanks for your help! Tom -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I think that''s overdoing the DRY thing a little bit, but if you want: name_attr = :"#{some_condition ? ''first'' : ''shared''}name" :name => h(agent.send(name_attr)) On Fri, Feb 12, 2010 at 09:35, Tom Ha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi there, > > my short question: > > Assume I have these 2 cases in my code : > > ===========================> :name => h(agent.firstname) > > [...] > > :name => h(agent.sharedname) > ===========================> > To stay DRY, I want this to become just 1 line, by replacing > "first"/"shared" by a variable that I would set at the beginning. > > How would the code of this 1 new line and the initiation of the variable > look like? > > Thanks for your help! > Tom > -- > Posted via http://www.ruby-forum.com/. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 Feb 12, 2:35 pm, Tom Ha <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> To stay DRY, I want this to become just 1 line, by replacing > "first"/"shared" by a variable that I would set at the beginning. > > How would the code of this 1 new line and the initiation of the variable > look like? >Well you could either use send, but to be quite honest I really wouldn''t - You''ll end up with something that is probably longer and certainly more complicated. Fred> Thanks for your help! > Tom > -- > Posted viahttp://www.ruby-forum.com/.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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 12 February 2010 14:35, Tom Ha <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> How would the code of this 1 new line and the initiation of the variable > look like?Difficult to suggest an ideal solution with such a little info, but maybe add a "first_or_shared_name" method to your Agent model, which determines which name to show, and then call that in the views instead of either firstname or sharedname: # agent.rb def first_or_shared_name case some_method_or_variable_that_determines_which_name when :first self. firstname when :shared self.sharedname else raise "invalid value passed to my cool new method" end end # view :name => h(agent.first_or_shared_name) then if you want/need to, you could pass the variable to the model method from the view if it''s likely to change lots: # agent.rb def first_or_shared_name(first_or_shared) case first_or_shared ... etc end end # view :name => h(agent.first_or_shared_name :shared) :name => h(agent.first_or_shared_name :first) Coming back to the point, you might just be making work for the sake of it, but I''ve used approaches like this when, for instance, I want to show "firstname lastname" or "title lastname" depending on whether I have the firstname or not. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 February 2010 14:39, Mat Brown <mat-BtJC2HJPhyMAvxtiuMwx3w@public.gmane.org> wrote:> I think that''s overdoing the DRY thing a little bit, but if you want: > > name_attr = :"#{some_condition ? ''first'' : ''shared''}name" > > :name => h(agent.send(name_attr))That''s putting business logic in the view - avoid wherever possible for sleep-filled nights... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
While I agree that your solution of having a method in the model that delegates to the appropriate attribute is elegant (if the model has access to the appropriate data to make that decision), I wonder what reason we have to believe this is going on in the view? On Fri, Feb 12, 2010 at 09:49, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 12 February 2010 14:39, Mat Brown <mat-BtJC2HJPhyMAvxtiuMwx3w@public.gmane.org> wrote: >> I think that''s overdoing the DRY thing a little bit, but if you want: >> >> name_attr = :"#{some_condition ? ''first'' : ''shared''}name" >> >> :name => h(agent.send(name_attr)) > > That''s putting business logic in the view - avoid wherever possible > for sleep-filled nights... > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 February 2010 14:52, Mat Brown <mat-BtJC2HJPhyMAvxtiuMwx3w@public.gmane.org> wrote:> While I agree that your solution of having a method in the model that > delegates to the appropriate attribute is elegant (if the model has > access to the appropriate data to make that decision), I wonder what > reason we have to believe this is going on in the view? > >>> :name => h(agent.send(name_attr))the call to "h" gave me a clue, but if it''s not a view (and he''s calling view helpers in models or controllers), the principle of keeping model logic in the model is fairly sound. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 February 2010 14:52, Mat Brown <mat-BtJC2HJPhyMAvxtiuMwx3w@public.gmane.org> wrote:> (if the model has > access to the appropriate data to make that decision)that''s what occurred to me as I wrote it, hence the suggestion to pass the determination in as a parameter to the method. Like I said, with such little info from the OP, it''s hard to be conclusive. If we''ve given him a few different choices, one of them might be what he needs :-) -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks a lot for your quick help, guys ! Actually, all of your input is very helpful (some of it right away, some of it later)! -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.