Saturday, October 17, 2009

[Rails] Re: How to DRY up these methods?

Thanks for the help. I used the latter to come up with:

@fields_to_normalize = %w{ address city country_id email_general fax
name phone phone_local state_id vacation_end_on vacation_notice
vacation_start_on zip_code }

@fields_to_normalize.each do |attr|
define_method("#{attr}=") do |value|
@blanks ||= [0, "0", "", " "]
write_attribute(attr, @blanks.include?(value) ? nil : value)
end
end

Is it possible to do something more Rails-ish so it reads:

normalize_fields :address, :city, :country_id

This additional re-factoring would allow re-use across models.

Bill
--
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@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate