Created
January 21, 2013 01:31
-
-
Save allspiritseve/4582992 to your computer and use it in GitHub Desktop.
Mixin to handle storing currency as integers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Centsify | |
extend ActiveSupport::Concern | |
module ClassMethods | |
def centsify(*columns) | |
columns = [columns] unless columns.is_a?(Array) | |
columns.each do |column| | |
define_method column do | |
self.send("#{column}_cents").to_i / 100.0 | |
end | |
define_method "#{column}=" do |amount| | |
self.send("#{column}_cents=",(BigDecimal(amount.is_a?(String) ? amount : amount.to_s) * 100).to_i) | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment