Rails [AXLSX]: Change Currency - ruby-on-rails

I need to change the currency format on an Excel file that is exported.
The default currency pattern is US (1,000.00) [One Thousand] and I need it to become PT-BR format (1.000,00) [One Thousand].
I have tried implementing this way:
formated = workbook.styles.add_style(format_code: '$#.##0,00')
but the only thing it did, was add more zeroes after the .
How should I do it?

Welcome to SO :)
Yo might try this:
formated = workbook.styles.add_style(:format_code => '#.###,##')

Related

Rails - Alphanumeric field validation

In rails 4, I need to validate the alphanumeric field which can accept only dot(.), hyphen(-), slash(/) and space in between the characters.
Eg: AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34
I have tried with /^[0-9]+#$/ and /^\d+([.,]\d+)?$/ and /^[0-9]+#$/ but it is not working as per the requirement.
Value should be accept as per the examples. Please help me to validate this field.
I think this might help you:
/^[A-Za-z0-9-\/\.\s]+$/
This worked for all the examples you have provided
AB123-GH345 or AB45.NH744 or KHJ3/SD34 or HJS23 JKA34
and rejected when I inserted a character like ? in the middle(HJS23?JKA34).
Update
If you don't want multiline anchors then you can use it like this:
/\A[A-Za-z0-9-\/\.\s]+\z/
You can use this Rubular site to validate your Regex codes.
Try this:
/^[a-zA-Z\d\.\-\/# ]+$/
try this regex
^[a-zA-Z0-9\. /'\-]+$
Hope this will help

Proper way to parse and format a DateTime object?

In rails I have DateTime object as:
2014-08-21 18:14:12 UTC
I want to display it as:
08-21-2014
I can hack this as follows:
"2014-08-21 18:14:12 UTC".to_s.sub(/(\d*)(-)(\d*-\d*)/, '\3\2\1').first(10)
But I prefer the proper Rails/ Ruby way and not having to convert the Date object to a string. Thanks!
See DateTime#strftime:
> DateTime.now.strftime '%m-%d-%Y'
=> "08-22-2014"
And please, please, please don’t use that format, unless you have no choice. Hyphens should only be used for the Y-m-d format. Use slashes or dots if possible. (Even better, use Y-m-d!)
Just use strftime to get various formats of date & time.
> DateTime.now.strftime("%m-%d-%Y")
=> "08-23-2014"
Read more about strftime

Change faker gem phone number format

Is there a way to control the format of the Phone number generated by faker?
When I call:
Faker::PhoneNumber.cell_phone.to_i
I end up getting the wrong value.
I also would like to not have extensions.
You can set custom format on the fly like this:
Faker::Base.numerify('+90(###) ### ####')
This will solve your problem.
Faker::PhoneNumber.cell_phone is basically just calling numerify with one of the predefined phone_number_formats.
So you could just use numerify with your own format. For e.g. If you want 10 digits number, you would do:
Faker.numerify('#########')
If you'd still like to use Faker::PhoneNumber.cell_phone but would like to get rid of the hyphens, you could use gsub to replace the hyphens as:
Faker::PhoneNumber.cell_phone.gsub(/-/, '')

Displaying text in differnt locales on one page

I need to display sentences in my GSP in differnt locales.
The following documentation states that g:message takes a locale param.
I can't find an example of this anywhere. Has anyone done this?
<p><g:message code="welcome.into.text1" locale="sv_SE"/></p>
<p><g:message code="welcome.into.text1" locale="en_US"/></p>
Obviosuly, I have messages_sv.properties & messages_en_US.properties.
Thanks
To specify locale explicitely use Locale object instead of String, e.g.:
<g:message code="welcome.into.text1" locale="${Locale.US}"/>
http://grails.org/doc/latest/guide/i18n.html
Grails how to change the current locale

Clean up & style characters from text

I am getting text from a feed that has alot of characters like:
Insignia&#153; 2.0 Stereo Computer Speaker System (2-Piece) - Black
4th-Generation Apple® iPod® touch
Is there an easy way to get rid of these, or do I have to anticipate which characters I want to delete and use the delete method to remove them? Also, when I try to remove
&
with
str.delete("&")
It leaves behind "amp;" Is there a better way to delete this type of character? Do I need to re-encode the text?
String#delete is certainly not what you want, as it works on characters, not the string as a whole.
Try
str.gsub /&/, ""
You may also want to try replacing the & with a literal ampersand, such as:
str.gsub /&/, "&"
If this is closer to what you really want, you may get the best results unescaping the HTML string. If so try this:
CGI::unescapeHTML(str)
Details of the unescapeHTML method are here.
If you are getting data from a 'feed', aka RSS XML, then you should be using an XML parser like Nokogiri to process the XML. This will automatically unescape HTML entities and allow you to get the proper string representation directly.
For removing try to use gsub method, something like this:
text = "foo&bar"
text.gsub /\b&\b/, "" #=> foobar

Resources