Mendable.com home

Correct Format Plugin Released

04 Jul 2009

I have released a new Plugin for Rails, called Correct-Format.

Github URL: github.com/mendable/correct-format

This plugin allows you to automatically correct simple user input mistakes and format user-input without raising an ActiveRecord Error and without inserting inconsistently formatted data into your database. Using this plugin will enhance the usability and user-friendlyness of your application and increase your data integrity.

You can automatically:

Installation

$ git submodule add git://github.com/mendable/correct-format.git vendor/plugins/correct-format
$ git commit -am "add correct-format plugin"

Ensure that you have this in your config/deploy.rb:

set :git_enable_submodules, true

so that the submodules are pulled down when you deploy your application.

Example Usage

  class User < ActiveRecord::Base
    # Make usernames consistently lower case
    correct_format_downcase :username

    # Replace comma's with periods in email address, and make email address all lower case
    correct_format_email :email

    # Capitalize first letter of first word, and downcase the rest
    correct_format_capitalize :username

    # Capitalize all first letters of ALL WORDS in the string
    correct_format_capitalize_each :address_line_1, :address_line_2

    # UK Postcodes are upper case
    correct_format_upcase :postcode

    # apply a function to EVERY string field in a record
    correct_format_capitalize self.attributes.select{|k, v| self.column_for_attribute(k).type == :string }.map(&amp;:first)
  end

Feedback and patches welcome.