Module: RailheadAutoformat

Defined in:
lib/railhead_autoformat.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
# 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

Instance Method Details

#format_fieldsObject



11
12
13
14
15
16
17
18
19
# 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