Skip to content

Instantly share code, notes, and snippets.

@boosty
Created August 16, 2009 19:00
Show Gist options
  • Save boosty/168706 to your computer and use it in GitHub Desktop.
Save boosty/168706 to your computer and use it in GitHub Desktop.
# Include me in your ActiveRecord model and get string attributes
# trimmed/stripped before validation.
module TrimStringAttributes
def self.included(base)
base.class_eval <<-EOF
before_validation :trim_string_attributes
EOF
end
protected
def trim_string_attributes
attributes.each do |k,v|
self[k] = v.mb_chars.strip if v.respond_to?(:mb_chars)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment