I have a before_save filter called :strip_whitespaces
on a Rails model like this
before_save :strip_whitespaces
The strip_whitespaces
filter is a private method which is defined in the following way:
private
def strip_whitespaces
self.name = name.split.join(" ") if attribute_present?("name")
self.description = description.split.join(" ") if attribute_present?("description")
self.aliases = aliases.split.join(" ") if attribute_present?("aliases")
end
How do I use ruby's send method to make this method DRY-er? This also helps once I have to add more fields to this filter.
I had something like this in mind but it does not work
%W[name description aliases].each do |attr|
self.send(attr) = self.send(attr).split.join(" ") if attribute_present?(attr)
end