viehlieb
4b5775e107
use filetypes for manual uploading bnn, odin, foodsoft file use opts in .parse adapt specs to include file format add specs for odin, bnn, foodsoft files adapt localize input to remove ',' separator and replace with '.' remove depr foodsoftfile.rb and spreadsheet.rb remove todo
27 lines
858 B
Ruby
27 lines
858 B
Ruby
module LocalizeInput
|
|
extend ActiveSupport::Concern
|
|
|
|
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, ".") or input.gsub!(",", ".") # Replace separator with db compatible character
|
|
input
|
|
rescue
|
|
Rails.logger.warn "Can't localize input: #{input}"
|
|
input
|
|
end
|
|
|
|
class_methods do
|
|
def localize_input_of(*attr_names)
|
|
attr_names.flatten.each do |attr|
|
|
define_method "#{attr}=" do |input|
|
|
self[attr] = LocalizeInput.parse(input)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|