Sha256: 6c108896de259202fc8fa9333ce7e32fcdb3b7f5d3d3a82788aba90ac5931c66

Contents?: true

Size: 749 Bytes

Versions: 1

Compression:

Stored size: 749 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_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

  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.3 lib/railhead_autoformat.rb