module RailheadAutoformat

Public Class Methods

included(base) click to toggle source
# File lib/railhead_autoformat.rb, line 3
def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    class_attribute :format_options
    before_validation :format_fields
  end
end

Public Instance Methods

format_fields() click to toggle source
# File lib/railhead_autoformat.rb, line 11
def format_fields
  self.class.columns.each do |column|
    next unless column.type == :string or column.type == :text
    field = column.name.to_sym
    if self[field].is_a?(String) and not (format_options and format_options[:except].include?(field))
      self[field] = self[field].gsub(/ +/, ' ').strip
    end
  end
end