Centsable
The gem provides ability to store your price values in cents.
Installation
Add this line to your application's Gemfile:
gem 'centsable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install centsable
Usage
Add a column which will store a value in cents:
add_column :product, :price, :integer
Tell a model that you use price in cents:
# app/models/product.rb
class Product < ActiveRecord::Base
centsable :price # or just centsable
end
So you can do this:
product = Product.first
product.price # => 23.34
product.price_in_cents # => 2334
Product.in_cents(56.78) # => 5678
product.price = 67.99
product.price_in_cents # => 6799
If you need to have some attributes which store cents you can push these names of attributes into the centsable method:
# app/models/price_history.rb
class PriceHistory < ActiveRecord::Base
belongs_to :product
centsable :price, :price_old
end
Contributing
- Fork it ( https://github.com/ekondr/centsable/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request