Socket
Book a DemoInstallSign in
Socket

multi_currency

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multi_currency

0.1.5
bundlerRubygems
Version published
Maintainers
1
Created
Source

MultiCurrency

TODO: Write a gem description

Installation

Add this line to your application's Gemfile:

gem 'multi_currency', github: 'xsoulsyndicate/multi_currency'

And then execute:

$ bundle

Usage

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)

Configuration

Add an initializer e.g config/initializers/multi_currency.rb

MultiCurrency.configure do |config|
  config.default_currency = 'eur'
end

Open Exchange Rates Converter

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'

Contributing

  • 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

FAQs

Package last updated on 05 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.