TODO: Write a gem description
Add this line to your application's Gemfile:
gem 'multi_currency', github: 'xsoulsyndicate/multi_currency'
And then execute:
$ bundle
NOTE: Still in development
For example you already had 'price' attribute in Product model and want to add multi currency support for it.
class Product < ActiveRecord::Base
attr_accessible :price, :price_rate_date, :price_currency, :price_source_amount, :price_source_currency # For Rails 3
multi_currency_for [:price]
before_save :do_currency_exchange
end
Add required migration. Create a migration and add this columns:
add_column :products, :price_rate_date, :date
add_column :products, :price_currency, :string
add_column :products, :price_source_amount, :decimal
add_column :products, :price_source_currency, :string
All columns are prefixed with :price in this case. Adjust the prefix according to the column name.
So when you save a product, you can just do it like this.
product = Product.new(price_source_amount: 100, price_source_currency: 'USD')
product.save
The required attributes to set is only _source_currency and _source_amount. You can set _rate_date if you want to specify in which date the currency exchange rate will be fetched.
After that you can get price in any currency:
product = Product.first
product.price_in('EUR')
Default exchange rate is rate from _rate_date column. If you want to get price with currency exchange rate in other date, just pass a date:
product.price_in('EUR', Date.yesterday)
You can also get total of price in other currency:
Product.sum_price('EUR', Date.today)
Add an initializer e.g config/initializers/multi_currency.rb
MultiCurrency.configure do |config|
config.default_currency = 'eur'
end
Set default_converter = 'Openexchange' on config/initializers/multi_currency.rb
MultiCurrency.configure do |config|
config.default_converter = 'Openexchange'
end
You will need APP ID to use OpenExchangeRate, you can get from https://openexchangerates.org for free. Set OPEN_EXCHANGE_RATES_APP_ID environment variable.
OPEN_EXCHANGE_RATES_APP_ID = 'APP_ID'
- Fork it ( http://github.com/xsoulsyndicate/multi_currency/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 new Pull Request