Search code examples
ruby-on-railsrubycurrencymoney-rails

Ruby on rails currency switcher for whole site


I've been searching for hours and viewed all google results one by one... Read about RubyMoney, Money-Rails. I'm new to rails and I still can't understand how to get a currency switcher for my whole Ruby on Rails app.

  1. In my Gemfile

    gem 'money-rails' 
    
    gem 'json'
    
    gem 'bigdecimal'
    
  1. bundle
  2. Created config/initializers/money.rb

    MoneyRails.configure do |config|
      config.default_currency = :eur
    end
    
  3. <%= humanized_money_with_symbol (item.item_price) %> formats that number as a currency in the view.

  4. How do I get a currency selector like Airbnb for instance to change all the prices of the website with automatic rate update from EU bank or google? I can't understand the RubyMoney documentation regarding this...


Solution

  • Here's where I would do as starting point.

    1) Add to Gemfile:

    gem 'google_currency'
    

    2) Add to config/initializers/money.rb:

    MoneyRails.configure do |config|
      config.default_currency = :eur
    
      # set default bank to instance of GoogleCurrency
      Money::Bank::GoogleCurrency.ttl_in_seconds = 86400
      config.default_bank = Money::Bank::GoogleCurrency.new
    end
    

    3) Add a currency selector somewhere in application layout or wherever you think it should go with all currencies you are interested in (:usd, :eur, etc.). Use either on change javascript event of this select box or a button to trigger a rails action that saves the selected currency on a session variable (let's call it from now on session[:currency]) and that also refreshes the current page (in case you are showing some prices on the current page - if you're using just a button a simple redirect_to :back would do otherwise if your response is in js format use window.location.reload();). Action would look something like this:

    def save_currency
      session[:currency] = params[:currency]
      respond_to do |format|
        format.html { redirect_to :back }
      end
    end
    

    And on your routes.rb (replace controller for whatever you want) something like this:

    post '/controller/save_currency', to: 'controller#save_currency'
    

    Just the form with select box and submit button missing - do this as you please :)

    4) Add a helper method to render prices (I'm assuming all your price attributes are defined as monetize using the money-rails gem). All prices that you want to show on your site should use this method to be rendered on a view. Method would look something like this (you can put it on your application_helper.rb if you don't see other place it would fit better):

    def converted_price(price)
      if session[:currency].present?
        humanized_money_with_symbol(price.exchange_to(session[:currency]))
      else
        humanized_money_with_symbol(price)
      end
    end
    

    This is a simplified version, you should also add some exception handling look at google_currency gem for more documentation on this.

    5) On all the views where you want to print a price, use a code similar to this (I'm using the same example as you did):

    <%= converted_price(item.item_price) %>
    

    Hope it helps!