Created
February 11, 2014 15:05
-
-
Save ukasiu/8936514 to your computer and use it in GitHub Desktop.
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
# Using comma as decimal separator in Rails 4 based on | |
# http://stjhimy.com/posts/11-using-comma-as-decimal-separator | |
ActiveRecord::ConnectionAdapters::Column.class_eval do | |
class << self | |
def value_to_float(value) | |
if value.class == Float | |
value | |
elsif value.class == String | |
value.gsub(',', '.').to_f | |
else | |
value.to_f | |
end | |
end | |
def value_to_decimal_with_comma_separator(value) | |
if value.class == BigDecimal | |
value | |
elsif value.class == String | |
value.gsub(',', '.').to_d | |
elsif value.respond_to?(:to_d) | |
value.to_d | |
else | |
value.to_s.to_d | |
end | |
end | |
alias_method_chain :value_to_decimal, :comma_separator | |
end | |
def type_cast_with_comma_separator(value) | |
return nil if value.nil? | |
return coder.load(value) if encoded? | |
klass = self.class | |
case type | |
when :string, :text then value | |
when :integer then klass.value_to_integer(value) | |
when :float then klass.value to_float(value) | |
when :decimal then klass.value_to_decimal(value) | |
when :datetime, :timestamp then klass.string_to_time(value) | |
when :time then klass.string_to_dummy_time(value) | |
when :date then klass.value_to_date(value) | |
when :binary then klass.binary_to_string(value) | |
when :boolean then klass.value_to_boolean(value) | |
else value | |
end | |
end | |
alias_method_chain :type_cast, :comma_separator | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment