Last active
August 29, 2015 14:07
-
-
Save aurels/1a1bb7236f3dfa0d4c91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="input short"> | |
<%= data_f.label :number_format, _("Style de formatage des nombres") %> | |
<%= data_f.select :number_format, NumberFormat.options_for_select %> | |
</div> | |
<div class="input short"> | |
<%= data_f.label :date_format, _("Style de formatage des dates") %> | |
<%= data_f.select :date_format, DateFormat.options_for_select %> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NumberFormat.format(current_user.number_format, @invoice.amount) | |
DateFormat.format(current_user.date_format, @invoice.issue_date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DateFormat | |
CODES = ['DMY', 'MDY'] | |
DATE_FORMATTING_STYLES = [ | |
['31/12/2001', '%d/%m/%Y'], | |
['12/31/2001', '%m/%d/%Y'] | |
] | |
def self.format(code, date) | |
if code == 'DMY' | |
date.strftime('%d/%m/%Y') | |
elsif code == 'MDY' | |
date.strftime('%m/%d/%Y') | |
else | |
raise "Unknown date format code #{code}" | |
end | |
end | |
def self.options_for_select | |
[ | |
['31/12/2001', 'DMY'], | |
['12/31/2001', 'MDY'] | |
] | |
end | |
def self.for_jQuery(code) | |
if code == 'DMY' | |
'dd/mm/yy' | |
elsif code == 'MDY' | |
'mm/dd/yy' | |
else | |
raise "Unknown date format code #{code}" | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
include Mongoid::Document | |
field :locale, type: String | |
field :number_format, type: String | |
field :date_format, type: String | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NumberFormat | |
CODES = ['COMMA', 'DOT', 'SWISS'] | |
def self.format(code, number) | |
if code == 'COMMA' | |
separator = ',' | |
delimiter = ' ' | |
elsif code == 'DOT' | |
separator = '.' | |
delimiter = ',' | |
elsif code == 'SWISS' | |
separator = '.' | |
delimiter = "'" | |
else | |
raise "Unknown number format : #{code}" | |
end | |
number_with_precision(number, { | |
:separator => separator, | |
:delimiter => delimiter, | |
:precision => 2, | |
:strip_insignificant_zeros => false | |
})#.html_safe | |
end | |
def self.options_for_select | |
[ | |
['10 926,42', 'COMMA'], | |
['10,926.42', 'DOT'], | |
["10'926,42", 'SWISS'] | |
] | |
end | |
def self.number_with_precision(number, options) | |
ActionController::Base.helpers.number_with_precision(number, options) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment