public
Fork of dramsay/comma_parser
Description: Rails plugin providing various numeric model utilities. Such as allowing commas, $, %, etc for numeric fields
Homepage:
Clone URL: git://github.com/twerth/sexy_numerics.git
name age message
file .gitignore Mon Oct 06 13:17:08 -0700 2008 Forcing Rails to recognize that attribute has b... [Doug Ramsay]
file MIT-LICENSE Thu Nov 13 19:17:45 -0800 2008 Added acts_as_booleani and added documentation [twerth]
file README.textile Mon Nov 17 17:29:19 -0800 2008 Updated readme, removing acts_as_boolean [twerth]
file Rakefile Mon Oct 06 08:30:02 -0700 2008 Committing first version of plugin [Doug Ramsay]
file init.rb Tue Oct 14 12:59:08 -0700 2008 minor fix [twerth]
directory lib/ Mon Nov 17 12:58:59 -0800 2008 Removed acts_as_boolean, which is now in its ow... [twerth]
directory test/ Mon Nov 17 12:58:59 -0800 2008 Removed acts_as_boolean, which is now in its ow... [twerth]
README.textile

Sexy Numerics

This plugin provides a variety of utilities for numeric columns in an ActiveRecord model.

It allows you to accept commas, currency symbols, and percent symbols from users for integer and float columns.

(This is a fork of Doug Ramsay’s CommaParser, which I think is an elegant solution for commas, so I mod’d his code instead of writing it from scratch. http://github.com/dramsay/comma_parser/tree/master).

Examples


create_table :cities do |t|
  t.string  :name,          :limit => 20,                    :null => false
  t.integer :population,                      :default => 0, :null => true
  t.float   :annual_budget, :annual_expenses, :default => 0, :null => true
  t.float   :percent_unemployed,              :default => 0, :null => true
  t.integer :historic,                        :default => 0, :null => true
end

class City < ActiveRecord::Base
  allow_commas            :population
  allow_currency_symbols  :annual_budget, :annual_expenses
  allow_percent_symbols   :percent_unemployed

  # rest of class
end

Now the user can type population “1,000,000” into a form and it will still save correctly as “1000000” instead of “1”. This saves you the hassle of stripping out commas either in JavaScript or in the controller.

You can also strip any arbitrary characters, like so:


class City < ActiveRecord::Base
  allow_characters :annual_budget, annual_expenses, :characters => '.€|_' 
end

or as an array-like object


class City < ActiveRecord::Base
  allow_characters :annual_budget, annual_expenses, :characters => %w[. € | _]
end

Install

For Rails 2.1 and above:


ruby script/plugin install git://github.com/twerth/sexy_numerics.git

To upgrade:


ruby script/plugin install git://github.com/twerth/sexy_numerics.git —force

Future

My intention is to add other numeric Model based features.

License

Released under the MIT license