Better practice for time zone and currency converting with Rails - ruby-on-rails

My web app is about looking up airplane tickets.
The raw prices is stored in departure country currency and departure city time .
I wonder what's the better practice to convert the price in user's preferable currency.
How about create an currency field in User model, and convert the price according it.
And I want to show the updated time depends on the user's time zone.
If the user comes from Japan it will show in Japan time.
If the user comes from Taiwain it will show Taipei time.
Currently I hard code the time in that way,
flight.updated_at.in_time_zone("Taipei").strftime('%m-%d %H:%M')
Any suggestion or idea, thanks~

Currency:
The Money gem can probably help you. It has methods like these:
Money.new(1000, "USD").exchange_to("EUR").format # => "€906.86"
where "EUR" can be the currency stored in your User model.
Exchange rates can be retrieved from several online services with additional plugins (see the gem's README).
Time zone:
The local_time gem will give you this helper:
<%= local_time(flight.updated_at) %>
It will render a time tag, and update its content with javascript to match each visitor's local time.

Related

Supporting Timezones in Rails

I've got an application that allows users within the same company to create job records, view, edit, and search these jobs. The users are spread out all over the United States. A user in California may need to update a job for a user in New York and visa versa.
I read through an article that suggested setting Time.zone in the controller with a before action, but if I do this, I assume that Rails would then save that record in the current time zone for like the California user. Then, if the New York user updates the same record then updated_at time would then be in a different time zone than the created_at attribute. Ideally, I see all of my records having a UTC time and then when a given user accesses/creates records, the time is displayed to them in their set timezone but saved in the database as UTC. I'm not sure how to do this or if it's even the correct approach. Can anyone provide some guidance?
Thanks in advance.
#Cannon Moyer if you see table structure of any table. updated_at and created_at columns store timestamp without timezone. When displaying, These values are automatically converted with timezone as application's timezone(UTC or IST) is set. so don't need to worry about time when a given user accesses/creates records.
For your case as #iGian suggested this gem is useful for you. It sets the Rails timezone to the browser's configured timezone for each request.

get rate with Ruby Money gem

I'm using the Ruby Money gem (For rails) for my app and i want to save the used currency rate for every order.
My base currency is USD and i give my users the option to pay in EUR, on a order save i want to record the used currency conversion rate.
I just can't find a method to get the used rate from this lib, anybody knows how to do this?
I'm also looking for best practice on this, for now i'm planning on saving the prices in order_lines in the users currency and save the used currency per line. As my original prices are in dollars i'm also saving the price in dollar per order line as reference.
Thanks in advance!
For this example bank:
# config/initializers/money.rb
dev_bank = Money::Bank::VariableExchange.new
dev_bank.add_rate("EUR", "USD", 1.35)
dev_bank.add_rate("USD", "EUR", 1/1.35)
Money.default_bank = dev_bank
you can do this:
# somewhere else in your code
Money.default_bank.get_rate('EUR', 'USD')
See more info in the documentation
It looks like you're supposed to set up exchange rates in your configuration code using the exchange bank object or the money.rb initializer, in which case you already have access to the exchange rate in your code.
If for some reason you have access only to the input and output of the exchange conversion, you should be able to calculate the exchange rate yourself.

Rails applications and TimeZone

Does Rail handle TimeZone by itself or we should code for it?
For example a doctor orders medicine for a patient in NewYork like 6 hours a day... then flies to California and open the chart of that patient whom had visited in NewYork and looks at his medicine schedule ... How does Rails handle this situations?
It's my understanding that Rails converts all the time types (:datetime, :time, :timestamp) to UTC before storing them in the database.
If you want to ensure local time is always shown, then you need to call TimeZone#utc_to_local on your data before displaying it.
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html#method-i-local
There is some more good background information in this question:
In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?
I hope that helps.

changing the value of a currency based on a selected country in rails

I am building a rails ecommerce application and would like to integrate a currency exchange system were visitors can click on a check box and a list of countries will be displayed and when clicked on the value of the currencies of products on the site changes to the value of the selected countries currency. Is there a rails gem that connects to a currency exchange server and authomaticall converts the currency for me or any ideas of how i can accomplish this. Thank you.
The first thing to do is have the currency code like USD NGN EUR,so which ever currency the user selects has its currency code saved in a session or cookie having in mind that your default currency is set to which ever one you want to use.
Then Install the google currency gem which gives you access to real time exchange rate.
Assuming all these is set what we will do is
require 'money'
require 'money/bank/google_currency'
bank = Money::Bank::GoogleCurrency.new
rate = bank.get_rate(:NGN, :USD).to_f
For this example, i am simply getting the rate of 1 naira to $1
now that i no the rate all i have to do is find a way to convert the item price to selected currency.
Assuming the user selects $ then the session[:selected_currency] = "USD"
in our product model we have the following
##Assuming default currency is NGN so it defaults to NGN if no currency code
require 'money'
require 'money/bank/google_currency'
def converted_price(currency = :NGN)
bank = Money::Bank::GoogleCurrency.new
rate = bank.get_rate(:NGN, currency).to_f
price = item_amount / rate
price
end
so in our view we can have <%= #product.converted_price(session[:selected_currency])%>
I haven't tested this but this would give you an idea of how i solved the issue.
Check out the money gem
There seems to be some workaround to do exactly what you want to do, however it'll give you a decent base to what you're aiming for.

How to handle time difference in Rails application?

In my Rails application I would like to store the last_login time of a user in a database. Then I would like to display the last_login time to user based on his local time (i.e. it should be X if user is in Canada now, and Y if user is in Australia now).
Question 1
How would you recommend to handle the time difference ? Should I store in database the last_login translated to UTC, and when displaying translate it again to the local time, or is there a better method ?
Question 2
How could I identify the local timezone for displaying the last_login ?
I think you are right about storing last_login_at in UTC (or whatever server time is). It will be stored in that time by default. Then you can convert it to user local time:
user.last_login_at.in_time_zone('Eastern Time (US & Canada)')
In Rails Api you can find more about timezones and about time with zone.
To determine user timezone you can use some GeoIP tool, for example this one (follow description there). But it's not 100% accurate detection. The most reliable way to ask user select his timezone when he signing up.
To collect all timezones you can run:
ActiveSupport::TimeZone.all.map(&:name)

Resources