Commit 5ba814e90daf22652b74dc2fbda7218b5acbc3d0

Authored by Andrew Kane
1 parent b18c7b11

Cast big decimals to floats due to Rails BigDecimal JSON serialization

Showing 2 changed files with 29 additions and 0 deletions   Show diff stats
lib/searchkick/model.rb
... ... @@ -61,6 +61,28 @@ module Searchkick
61 61 source[field] = source[field].map(&:to_f).reverse if source[field]
62 62 end
63 63  
  64 + # change all BigDecimal values to floats due to
  65 + # https://github.com/rails/rails/issues/6033
  66 + cast_big_decimal =
  67 + proc do |obj|
  68 + case obj
  69 + when BigDecimal
  70 + obj.to_f
  71 + when Hash
  72 + obj.each do |k, v|
  73 + obj[k] = cast_big_decimal.call(v)
  74 + end
  75 + when Enumerable
  76 + obj.map! do |v|
  77 + cast_big_decimal.call(v)
  78 + end
  79 + else
  80 + obj
  81 + end
  82 + end
  83 +
  84 + cast_big_decimal.call(source)
  85 +
64 86 source.to_json
65 87 end
66 88  
... ...
test/sql_test.rb
... ... @@ -155,6 +155,13 @@ class TestSql < Minitest::Unit::TestCase
155 155 assert_first "blue", "Blue B", fields: [:name, :color]
156 156 end
157 157  
  158 + def test_big_decimal
  159 + store [
  160 + {name: "Product", latitude: 100.0}
  161 + ]
  162 + assert_search "product", ["Product"], where: {latitude: {gt: 99}}
  163 + end
  164 +
158 165 # load
159 166  
160 167 def test_load_default
... ...