Module: LocalizeInput

Extended by:
ActiveSupport::Concern
Included in:
Article, ArticlePrice, BankTransaction, FinancialTransaction, GroupOrderArticle, Invoice
Defined in:
app/models/concerns/localize_input.rb

Class Method Summary collapse

Class Method Details

.parse(input) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/models/concerns/localize_input.rb', line 4

def self.parse(input)
  return input unless input.is_a? String

  Rails.logger.debug { "Input: #{input.inspect}" }
  separator = I18n.t('separator', scope: 'number.format')
  delimiter = I18n.t('delimiter', scope: 'number.format')
  input.gsub!(delimiter, '') if input.match(/\d+#{Regexp.escape(delimiter)}+\d+#{Regexp.escape(separator)}+\d+/) # Remove delimiter
  input.gsub!(separator, '.') # Replace separator with db compatible character
  input
rescue StandardError
  Rails.logger.warn "Can't localize input: #{input}"
  input
end