Hi, I''m trying to make a helper in my application DRYer. Here is an edited version of the current relevant code: module MyControllerHelper PERSON_FIELDS = [''last_name'', ''first_name''] ADDRESS_FIELDS = [''address_1'', ''address_2'', ''city'', ''state'', ''zip''] def get_errors_on(section) errors = '''' @person.errors.each do |attr,msg| case section when ''person'' errors += "#{attr.humanize} #{msg}<br>" if PERSON_FIELDS.include? attr when ''address'' errors += "#{attr.humanize} #{msg}<br>" if ADDRESS_FIELDS.include? attr end end errors end # def get_errors_on(section) end # module WizardHelper I have tried different versions of the following but always unsuccessfully: @person.errors.each do |attr,msg| errors += "#{attr.humanize} #{msg}<br>" if (section.upcase + ''_FIELDS'').constantize.include? attr end I always get a message saying that, for example, PERSON_FIELDS constant has not been defined, although it is defined at the top of the module. Any ideas? Thank you. -- 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.
pepe, you should be able to use eval(section.upcase + ''_FIELDS'') instead of constantize. That being said, I can never suggest using eval unless I accompany that with a warning, a la http://www.railsrocket.com/articles/the-controversial-eval-function. Also, I always valued readability over being DRY. Being DRY in your case might be useful if you can reasonably expect to have more than just PERSON and ADDRESS fields. Otherwise, you might be sacrificing readability without cause. Liam On Jan 17, 1:49 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> Hi, > > I''m trying to make a helper in my application DRYer. Here is an edited > version of the current relevant code: > > module MyControllerHelper > PERSON_FIELDS = [''last_name'', ''first_name''] > ADDRESS_FIELDS = [''address_1'', ''address_2'', ''city'', ''state'', ''zip''] > > def get_errors_on(section) > errors = '''' > > @person.errors.each do |attr,msg| > case section > when ''person'' > errors += "#{attr.humanize} #{msg}<br>" if > PERSON_FIELDS.include? attr > when ''address'' > errors += "#{attr.humanize} #{msg}<br>" if > ADDRESS_FIELDS.include? attr > end > end > > errors > end # def get_errors_on(section) > end # module WizardHelper > > I have tried different versions of the following but always > unsuccessfully: > @person.errors.each do |attr,msg| > errors += "#{attr.humanize} #{msg}<br>" if (section.upcase + > ''_FIELDS'').constantize.include? attr > end > > I always get a message saying that, for example, PERSON_FIELDS > constant has not been defined, although it is defined at the top of > the module. > > Any ideas? > > Thank you.-- 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.
Hi Liam, I tried to use eval but couldn''t make it work in my prior attempts. I''ll give it a shot again. About the readability of the code I agree with you, my priority is always make my code easy to read, then make it DRY. I currently have a total of 4 sections that I have to run this code for but based on what I know I very possibly will need to run the code for many more sections in the future. If I get this one working right it will be a big help in the future, but when the code gets ''obscure'' I always leave extensive comments for my sanity and that of the person coming behind. Thanks a lot. On Jan 17, 5:35 pm, Liam Morley <imo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> pepe, you should be able to use eval(section.upcase + ''_FIELDS'') > instead of constantize. > > That being said, I can never suggest using eval unless I accompany > that with a warning, a lahttp://www.railsrocket.com/articles/the-controversial-eval-function. > Also, I always valued readability over being DRY. Being DRY in your > case might be useful if you can reasonably expect to have more than > just PERSON and ADDRESS fields. Otherwise, you might be sacrificing > readability without cause. > > Liam > > On Jan 17, 1:49 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote: > > > Hi, > > > I''m trying to make a helper in my application DRYer. Here is an edited > > version of the current relevant code: > > > module MyControllerHelper > > PERSON_FIELDS = [''last_name'', ''first_name''] > > ADDRESS_FIELDS = [''address_1'', ''address_2'', ''city'', ''state'', ''zip''] > > > def get_errors_on(section) > > errors = '''' > > > @person.errors.each do |attr,msg| > > case section > > when ''person'' > > errors += "#{attr.humanize} #{msg}<br>" if > > PERSON_FIELDS.include? attr > > when ''address'' > > errors += "#{attr.humanize} #{msg}<br>" if > > ADDRESS_FIELDS.include? attr > > end > > end > > > errors > > end # def get_errors_on(section) > > end # module WizardHelper > > > I have tried different versions of the following but always > > unsuccessfully: > > @person.errors.each do |attr,msg| > > errors += "#{attr.humanize} #{msg}<br>" if (section.upcase + > > ''_FIELDS'').constantize.include? attr > > end > > > I always get a message saying that, for example, PERSON_FIELDS > > constant has not been defined, although it is defined at the top of > > the module. > > > Any ideas? > > > Thank you.-- 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.
eval worked indeed. Thanks again. The solution, as usual, was simpler than what I thought it would be. :) On Jan 17, 5:35 pm, Liam Morley <imo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> pepe, you should be able to use eval(section.upcase + ''_FIELDS'') > instead of constantize. > > That being said, I can never suggest using eval unless I accompany > that with a warning, a lahttp://www.railsrocket.com/articles/the-controversial-eval-function. > Also, I always valued readability over being DRY. Being DRY in your > case might be useful if you can reasonably expect to have more than > just PERSON and ADDRESS fields. Otherwise, you might be sacrificing > readability without cause. > > Liam > > On Jan 17, 1:49 pm, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote: > > > Hi, > > > I''m trying to make a helper in my application DRYer. Here is an edited > > version of the current relevant code: > > > module MyControllerHelper > > PERSON_FIELDS = [''last_name'', ''first_name''] > > ADDRESS_FIELDS = [''address_1'', ''address_2'', ''city'', ''state'', ''zip''] > > > def get_errors_on(section) > > errors = '''' > > > @person.errors.each do |attr,msg| > > case section > > when ''person'' > > errors += "#{attr.humanize} #{msg}<br>" if > > PERSON_FIELDS.include? attr > > when ''address'' > > errors += "#{attr.humanize} #{msg}<br>" if > > ADDRESS_FIELDS.include? attr > > end > > end > > > errors > > end # def get_errors_on(section) > > end # module WizardHelper > > > I have tried different versions of the following but always > > unsuccessfully: > > @person.errors.each do |attr,msg| > > errors += "#{attr.humanize} #{msg}<br>" if (section.upcase + > > ''_FIELDS'').constantize.include? attr > > end > > > I always get a message saying that, for example, PERSON_FIELDS > > constant has not been defined, although it is defined at the top of > > the module. > > > Any ideas? > > > Thank you.-- 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.