Skip to content

Instantly share code, notes, and snippets.

@allspiritseve
Created January 21, 2013 01:31
Show Gist options
  • Save allspiritseve/4582992 to your computer and use it in GitHub Desktop.
Save allspiritseve/4582992 to your computer and use it in GitHub Desktop.
Mixin to handle storing currency as integers.
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