Last active
June 28, 2017 11:36
-
-
Save ievgrafov/30fef46eae8bcec9fd27 to your computer and use it in GitHub Desktop.
rails 4.2 comma as decimal sparator
This file contains 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
# A monkey-patch that allows you to use comma as decimal separator in ActiveRecord | |
# Notice for rails 5.0: in Rails 5.0 module Type with all nested classes is moved into ActiveModel, | |
# so don't forget to substitute 'module ActiveRecord' with 'module ActiveModel' after update | |
module ActiveRecord | |
module Type | |
class Decimal | |
private | |
alias_method :cast_value_without_comma_separator, :cast_value | |
def cast_value(value) | |
value = value.tr(',', '.') if value.is_a?(::String) | |
cast_value_without_comma_separator(value) | |
end | |
end | |
class Float | |
private | |
alias_method :cast_value_without_comma_separator, :cast_value | |
def cast_value(value) | |
value = value.tr(',', '.') if value.is_a?(::String) | |
cast_value_without_comma_separator(value) | |
end | |
end | |
end | |
end | |
## Since NumericalityValidator uses for validation value before typecast | |
## we need to patch it too. | |
module ActiveModel | |
module Validations | |
class NumericalityValidator | |
protected | |
def parse_raw_value_as_a_number(raw_value) | |
raw_value = raw_value.tr(',', '.') if raw_value.is_a?(::String) | |
Kernel.Float(raw_value) if raw_value !~ /\A0[xX]/ | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment