before_validation :normalize_color_fields
private
# could use refactoring to reduce redundancy
def normalize_color_fields
(1..3).each do |number|
normalize_field("color#{number}")
end
end
def normalize_field(name)
return unless if attribute_present?(name)
content = send(name)
content.gsub!(/[^0-9A-F]/i, "")
send("#{name}=", content)
end