rails different currency formats - ruby-on-rails

I need to show user amount presented in different currencies. e.q. :
Your balance: $ 100 000.00
€ 70 000.00
3 000 000,00 руб.
So I need to use number_to_currency three times with different locales(en, eu, ru). What is the right way to do it?

I don't think you actually need different locales, because you have just balances in different currencies. You can simply pass additional arguments to number_to_currency. Something like this:
number_to_currency(70000.00, :unit => "€", :separator => ".", :delimiter => " ", :format => "%u %n")
This will display: € 70 000.00
Additionally it seems that you can set :locale option when calling number_to_currency. It's not documented, but here is the part of the number_to_currency code:
defaults = I18n.translate('number.format''number.format', :locale => options[:locale], :raise => true) rescue {}
currency = I18n.translate('number.currency.format''number.currency.format', :locale => options[:locale], :raise => true) rescue {}
So you should be able to do something like:
number_to_currency(70000.00, :locale => :ru)

Related

Phony Rails Normalize Number in View

I'm trying to display a normalized phone number in one of my views with phony_rails. In their documentation they say to use:
<%= "010-12341234".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-') %>
I'm using HAML so my markup looks like this:
= f.input :personal_phone.phony_formatted(:normalize => :US, :spaces => '-'), label: false
This returns an error undefined method "phony_formatted" for :personal_phone:Symbol.
I get a similar error if I try to just use a string of digits:
= "5551234567".phony_formatted(:normalize => :US, :spaces => '-'), label: false
I would like to format the number inside of the field, can anyone offer any advice on how to accomplish that? Thanks.

Displaying Only the first x words of a string in rails

<%= message.content %>
I can display a message like this, but in some situations I would like to display only the first 5 words of the string and then show an ellipsis (...)
In rails 4.2 you can use truncate_words.
'Once upon a time in a world far far away'.truncate_words(4)
=> "Once upon a time..."
you can use truncate to limit length of string
truncate("Once upon a time in a world far far away", :length => 17, :separator => ' ')
# => "Once upon a..."
with given space separator it won't cut your words.
If you want exactly 5 words you can do something like this
class String
def words_limit(limit)
string_arr = self.split(' ')
string_arr.count > limit ? "#{string_arr[0..(limit-1)].join(' ')}..." : self
end
end
text = "aa bb cc dd ee ff"
p text.words_limit(3)
# => aa bb cc...
Try the following:
'this is a line of some words'.split[0..3].join(' ')
=> "this is a line"
# Message helper
def content_excerpt(c)
return unlessc
c.split(" ")[0..4].join + "..."
end
# View
<%= message.content_excerpt %>
But the common way is truncate method
# Message helper
def content_excerpt(c)
return unless c
truncate(c, :length => 20)
end

How to format decimals

I need to format float like price for two decimal places and thousands spaces, like this:
"1 082 233.00"
Use number_to_currency or number_with_precision:
number_with_precision(1082233, :precision => 2,
:separator => '.',
:delimiter => ' ')
# => '1 082 233.00'
See the NumberHelper documentation for details.

Rails -- Find condition with id's all over the place

I have this find condition pulling from my model that currently looks like this.
#major_set = Interest.find(:all, :conditions => {:id => 1..21})
I'd like to add some more individual ids that I've just added which are like 120...130. I tried to do...
#major_set = Interest.find(:all, :conditions => {:id => 1..21, 120..130})
but got the error. " syntax error, unexpected '}', expecting tASSOC ...ns => {:id => 1..21, 122..130})"
How can I select multiple sets of id's as well as maybe some individual ids, ie ( :conditions => {:id => 1..21, 121..140, 144, 155 }??
You can convert the ranges to arrays and add them together. E.g.
#major_set = Interest.find(:all,
:conditions => {:id => (1..21).to_a + (120..130).to_a})
If you then want to add in individual ids then you can just add them to the array. E.g.
ids_to_find = (1..21).to_a + (120..140).to_a
ids_to_find << 144
ids_to_find << 145
#major_set = Interest.find(:all, :conditions => { :id => ids_to_find })
If you type {:id => 1..21, 122..130} inside irb you will get an error because it is not valid ruby syntax. This is interpreted as a hash where the first element is :id => 1..21 and the second is missing its key.
In order to make it a valid ruby expression, you would need to type:
{:id=>[1..21, 122..130]}
But I don't think that ActiveRecord will accept that syntax. So you may need to:
:conditions => "id BETWEEN 1 AND 21 OR id BETWEEN 122 AND 130"
This works in MySQL. I don't know if it will work in other databases.

Specifying a param from a route

Consider a PersonController which has a list action. A user can choose to list all people, or only males or females; currently, to do that, they'd have to go to /people/m or /people/f, corresponding to the route
map.list_people "people/:type",
:conditions => { :method => :get },
:requirements => { :type => /a|m|f/ },
:defaults => { :type => 'a' }
(/people/a works the same as just /people/, and lists all people).
I want to change my routing so that I could have two routes, /males/ and /females/ (instead of people/:type), both of which would go to PersonController#list (DRY -- aside from an extra parameter to what's being searched, everything else is identical), but will inherently set the type -- is there a way to do this?
map.with_options(:controller => "people", :action => "index") do |people|
people.males 'males', :type => "m"
people.females 'females', :type => "f"
end
Then you should be able to do males_path or males_url to get the path for this, and I'm sure you can guess what you do to get the path to females.

Resources