dimanche 23 juin 2019

How do I handle persistent currency switching throughout my app?

I support 3 currencies in my app GBP, JMD, USD.

When a user selects a currency on one page, it reloads the page and sets the session[:currency] variable accordingly. The challenge is, depending on the currency they select of the 3, the behavior isn't always predictable and the same.

I may have just been looking at this logic too long, but I can't figure out why it doesn't work and would love an extra pair of eyes.

So I started with this in my application_controller.rb:

unless params[:currency].blank?
  if params[:currency] != session[:currency] || session[:previous_currency].blank?
    session[:previous_currency] = session[:currency]
    session[:currency] = params[:currency] 
  end
else
  if session[:currency].blank?
    session[:currency] = 'JMD'
  end
end

That didn't really capture USD, GBP.

So that has since been modified to this:

unless params[:currency].blank? && session[:currency].blank?
  if (params[:currency].eql? "JMD") || (session[:currency].eql? "JMD")
    session[:currency] = 'JMD'
    session[:locale] = :en
    session[:previous_currency] = 'GBP'
  elsif (params[:currency].eql? "GBP") || (session[:currency].eql? "GBP")
    session[:locale] = :"en-GB"
  elsif (params[:currency].eql? "USD") || (session[:currency].eql? "USD")
    session[:locale] = :en
  elsif params[:currency] != session[:currency] || session[:previous_currency].blank?
    session[:previous_currency] = session[:currency]
    session[:currency] = params[:currency]
  end
else
  session[:currency] = 'JMD'
  session[:locale] = :en
end

The issue is this one doesn't work when I am going from any currency to GBP, and I can't quite figure out why.

What I mean is, say I change from USD to GBP. If I go to another page (say the homepage) it reverts to the default JMD currency. When I want it to stay GBP just like it does if I switch to USD and change pages everything stays in USD.

What am I missing?

Aucun commentaire:

Enregistrer un commentaire