foodsoft/app/models/concerns/localize_input.rb
Philipp Rothmann fb2b4d8a8a chore: rubocop
chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
2023-06-09 17:35:05 +02:00

27 lines
847 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, '.') # Replace separator with db compatible character
input
rescue StandardError
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