Move code from localize_input gem into Foodsoft
This commit is contained in:
parent
c59715381e
commit
6a3636d8cb
9 changed files with 33 additions and 8 deletions
25
app/models/concerns/localize_input.rb
Normal file
25
app/models/concerns/localize_input.rb
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
module LocalizeInput
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def localize_input_of(*attr_names)
|
||||
attr_names.flatten.each do |attr|
|
||||
define_method "#{attr}=" do |input|
|
||||
begin
|
||||
if 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
|
||||
end
|
||||
self[attr] = input
|
||||
rescue
|
||||
Rails.logger.warn "Can't localize input: #{input}"
|
||||
self[attr] = input
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue