Skip to content
This repository has been archived by the owner on Nov 27, 2017. It is now read-only.
/ acts_as_euro Public archive

minimal currency parameters plugin

Notifications You must be signed in to change notification settings

pzac/acts_as_euro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

acts_as_euro is a minimal module I've written to handle currencies stored in a db as integers. Given a parameter such as price it will create a couple accessor methods to work with the whole and cents part of the price. To use it just put it in lib/ and then require the file in your application.rb. In your model add something like this:

class CardItemPrice < ActiveRecord::Base
  include ActsAsEuro
  acts_as_euro :price
end

The module will create these methods:

  • price_whole
  • price_whole=
  • price_cents
  • price_cents=

That will extract the cents and whole parts from the main price attribute. They will be added to the attr_accessible list.

A simple test:

describe Product do
  it { should validate_presence_of :price }

  it "should deal correctly with cents and whole euros" do
    p = Product.new
    p.price = 123
    p.price_whole.should == 1
    p.price_cents.should == "23"

    p.price_whole = 2
    p.price_cents = 99
    p.price.should == 299
  end
end

I'm sure it can be improved, it was written in 5 minutes.

About

minimal currency parameters plugin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages