Sha256: 28e5cb9ecc5935e65c14c828e64601692cced00ea09682cdab239a2af25533f3

Contents?: true

Size: 786 Bytes

Versions: 1

Compression:

Stored size: 786 Bytes

Contents

module RailheadAutoformat

  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      class_attribute :format_options
      before_validation :format_fields
    end
  end

  def format(text)
    text.gsub(/ +/, ' ').strip
  end

  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) && format_options && format_options[:except].include?(field)
        self[field] = format(self[field])
      end
    end
  end

  module ClassMethods

    def auto_format(options = {})
      self.format_options = {
        :except => (options[:except] || [])
      }
    end
  end
end

ActiveRecord::Base.send :include, RailheadAutoformat

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
railhead_autoformat-0.0.1 lib/railhead_autoformat.rb