Created
December 11, 2012 17:23
-
-
Save aalvesjr/4260441 to your computer and use it in GitHub Desktop.
Trabalhando com Numeros e Datas em input's do tipo :text
This file contains hidden or 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 Instalment < ActiveRecord::Base | |
# set para valores | |
def amount=(value) | |
# ex. value = "1.200,00" | |
value = value.gsub('.','') # value = "1200,00" | |
value = value.gsub(',','.') # value = "1200.00" | |
write_attribute(:amount, value.to_f) # salva 1200.00 | |
end | |
# get para valores | |
def amount | |
value = read_attribute(:amount) # Ex. amount = 2.1 | |
value = format("%.2f", value) # Retorna uma string com 2 casas decimais em "2.10" | |
value = value.gsub('.',',') # Retorna a string no formato "2,10" | |
value | |
end | |
# set para datas | |
def expiration_date=(date) | |
date = Date.parse(date) # Converte uma String para Date | |
write_attribute(:expiration_date, date) # Salva a data já convertida no banco | |
end | |
# get para datas | |
def expiration_date | |
date = read_attribute(:expiration_date) # Pensar uma solução depois caso o valor seja nil | |
date = date.strftime('%d/%m/%Y') # Devolve uma String no formato "01/01/2013" | |
date | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment