0.0
No commit activity in last 3 years
No release in over 3 years
A specified database column acts as a price and creates on the fly methods like 'price' and 'price_in_cents'. For more information visit: http://github.com/jeroeningen/acts_as_price
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

> 1.5.2

Runtime

> 3.0.7
> 0.8.7
 Project Readme

INSTALLATION¶ ↑

Plugin: rails plugin install git@github.com:jeroeningen/acts_as_price.git

Gem: If you’re on Ruby 1.8, please use version 0.2.3 of the Gem. If you’re on Ruby 1.9 please use version 0.2.4 or higher.

USAGE¶ ↑

Create a column in the table of your database which you want to acts as a price.

NOTE THAT THE COLUMN MUST BE OF THE TYPE INTEGER.

In your model add the following:

  • acts_as_price column_name, {:validates => true, :comma_seperated => true, :currency => currency}

column_name is the name of your database column e.g. price or price_per_liter.

validates is optional and add validation to the column

comma_seperated is optional and return the price comma seperated instead of dot seperated

currency is optional and adds a prefix for the currency to the price.

This plugin creates the following getters and setters:

  • ‘price_in_cents’ and ‘<column_name>_in_cents’ #sets and returns the price in cents

  • ‘price’ and ‘<column_name>’ #sets and returns the price

EXAMPLES¶ ↑

class Car < ActiveRecord::Base
 acts_as_price :price, :comma_seperated => true, :currency => "EUR"
end

car = Car.new :price => 12999

car.price -> EUR. 12999,00

car.price_in_cents -> 1299900

class Fueltype < ActiveRecord::Base
 acts_as_price :price_per_liter, :validates => true
end

fuel = Fueltype.new :price_per_liter => 1.12

fuel.price -> 1.12

fuel.price_in_cents -> 112

fuel.price_per_liter -> 1.12

fuel.price_per_liter_in_cents -> 1.12

RSPEC INTERGRATION¶ ↑

Acts As Price comes with a helper method to automatically test the plugin using Rspec.

To use this method please add the following to your spec_helper.rb file:

  • config.include(ActsAsPriceHelper)

  • and if you using the gem, please also add: require ‘acts_as_price_helper’

Now you may add the following code to your spec-files for your models:

context "given an valid model" do
  it "should acts as price" do
    test_acts_as_price_methods <column_name>, <fixture or stub>
  end
end

TESTING¶ ↑

To test the plugin use the command ‘rspec spec/**’ inside the dir ‘~/vendor/plugins/acts_as_price’

KNOWN BUGS¶ ↑

The command ‘rake test’ is not running properly at the moment.

When creating a text_field, by default Rails gets the ‘raw’ value from the database, so you probably get the wrong value in your text_field. To prevent this please do the following:

f.textfield :price, :value => @object.price

NOTES¶ ↑

This plugin comes with Rspec tests. However this plugin assumes that you have a database configuration, it actually don’t use the database for the Rspec tests.

FEEDBACK AND BUGS REPORTS¶ ↑

For feedback and bug reports contact: jeroeningen@gmail.com