Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning].
Changes:
- Drop support for Rails < 7.0.

Feature:
- Add `cache_enabled` to config to be able to enable/disable cache.

Fix:
- Make `RateStore` marshalable so a bank using it can be marshaled.

Expand Down
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ ActiveCurrency::AddRates.call(currencies: %w[EUR USD])
You can then exchange money by using the Money gem helpers:

```rb
10.to_money('EUR').exchange_to('USD').cents
10.to_money("EUR").exchange_to("USD").cents
```

If you need to look up the previous currency rates:

```rb
ActiveCurrency::Rate.value_for('EUR', 'USD', 1.month.ago)
ActiveCurrency::Rate.value_for("EUR", "USD", 1.month.ago)
# => 1.151
ActiveCurrency::Rate.where(from: 'EUR', to: 'USD').pluck(:value)
ActiveCurrency::Rate.where(from: "EUR", to: "USD").pluck(:value)
# => [1.162, 1.162, 1.161, 1.161, 1.163, …]
```

Expand Down Expand Up @@ -88,7 +88,7 @@ add the following to your application’s initializers:
```rb
ActiveCurrency.configure do |config|
config.remote_bank = :open_exchange_rates
config.open_exchange_rates_app_id = '…'
config.open_exchange_rates_app_id = "…"
end
```

Expand All @@ -101,16 +101,21 @@ You can provide any Money-compatible bank when calling
ActiveCurrency::AddRates.call(…, bank: …)
```

## Apply a multiplier

If you want to increase or decrease the currency rates by a given multiplier,
you can do so by setting the `multiplier` option:
## Configuration

```rb
ActiveCurrency.configure do |config|
config.multiplier = {
["EUR", "USD"] => 1.01,
}
# Use the money-open-exchange-rates bank instead.
# config.remote_bank = :open_exchange_rates
# config.open_exchange_rates_app_id = "…"

# Disable `Rails.cache`.
# config.cache_enabled = false

# Increase or decrease the currency rates by a given multiplier.
# config.multiplier = {
# ["EUR", "USD"] => 1.01,
# }
end
```

Expand All @@ -124,8 +129,8 @@ For that, you can use a fake rate store in your `rails_helper.rb`:
```rb
MoneyRails.configure do |config|
rate_store = Money::RatesStore::Memory.new.tap do |store|
store.add_rate('USD', 'EUR', 1.5)
store.add_rate('EUR', 'USD', 0.67)
store.add_rate("USD", "EUR", 1.5)
store.add_rate("EUR", "USD", 0.67)
end
config.default_bank = Money::Bank::VariableExchange.new(rate_store)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/active_currency/cacheable_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module CacheableStore
include AfterCommitEverywhere

def get_rate(from, to, date = nil)
return super unless ActiveCurrency.configuration.cache_enabled?
return super if date

Rails.cache.fetch(cache_key(from, to), expires_in: 1.hour) do
Expand All @@ -17,6 +18,8 @@ def get_rate(from, to, date = nil)
def add_rate(from, to, rate, date = nil)
super

return unless ActiveCurrency.configuration.cache_enabled?

after_commit do
Rails.cache.delete(cache_key(from, to))
end
Expand Down
8 changes: 7 additions & 1 deletion lib/active_currency/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ def initialize
@remote_bank = :eu_central_bank
@open_exchange_rates_app_id = nil
@multiplier = {}
@cache_enabled = true
end

attr_accessor :remote_bank,
:open_exchange_rates_app_id,
:multiplier
:multiplier,
:cache_enabled

def cache_enabled?
!!@cache_enabled
end
end
end