Model Formatter plugin

Plugin details

The ModelFormatter module allows you to easily handle fields that need to be formatted or stripped of formatting as the are set or retrieved from the database.

Websitehttp://brad.folkens.com/formatting-data-in-rails-models Repositoryhttps://hattenschwetter.com/svn/hattenschwetter/rails_plugins/model_formatter/trunk/ Tags model, format LicenseMIT

Documentation

Install the plugin:
ruby script/plugin install https://hattenschwetter.com/svn/hattenschwetter/rails_plugins/model_formatter/trunk/
  class Widget < ActiveRecord::Base
    # Set an integer field as a symbol
    format_column :some_integer, :as => :integer

    # Specify the type as a class
    format_column :sales_tax, :as => Formatters::FormatCurrency
    format_column :sales_tax, :as => Formatters::FormatCurrency.new(:precision => 4)

    # Change the prefix of the generated methods and specify type as a symbol
    format_column :sales_tax, :prefix => 'fmt_', :as => :currency, :options => {:precision => 4}

    # Use specific procedures to convert the data +from+ and +to+ the target
    format_column :area, :from => Proc.new {|value, options| number_with_delimiter sprintf('%2d', value)},
                         :to => Proc.new {|str, options| str.gsub(/,/, '')}

    # Use a block to define the formatter methods
    format_column :sales_tax do
      def from(value, options = {})
        number_to_currency value
      end
      def to(str, options = {})
        str.gsub(/[\$,]/, '')
      end
    end

    ...
  end


The methods of this module are automatically included into ActiveRecord::Base as class methods, so that you can use them in your models.

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | Back in time (1 older version) | Last edited by: scott, 7 months ago