提交 0a8b212d 编写于 作者: J Jon Moss

Support AC::Parameters for PG HStore

As reported via #26904, there is a regression in how values for
Postgres' HStore column type are being processed, beginning in Rails 5.
Currently, the way that Active Record checks whether or not values need
to be serialized and put into the correct storage format is whether or
not it is a `Hash` object. Since `ActionController::Parameters` no
longer inherits from `Hash` in Rails 5, this conditional now returns
false. To remedy this, we are now checking to see whether the `value`
parameters being passed in responds to a certain method, and then
calling the `serialize` method, except this time with a real Hash
object. Keeping things DRY!

Fixes #26904.
上级 6e692e6e
* Allow `ActionController::Parameters`-like objects to be passed as
values for Postgres HStore columns.
Fixes #26904.
*Jon Moss*
* Added `stat` method to `ActiveRecord::ConnectionAdapters::ConnectionPool`
Example:
......
......@@ -24,6 +24,8 @@ def deserialize(value)
def serialize(value)
if value.is_a?(::Hash)
value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(", ")
elsif value.respond_to?(:to_unsafe_h)
serialize(value.to_unsafe_h)
else
value
end
......
......@@ -10,6 +10,12 @@ class Hstore < ActiveRecord::Base
store_accessor :settings, :language, :timezone
end
class FakeParameters
def to_unsafe_h
{ "hi" => "hi" }
end
end
def setup
@connection = ActiveRecord::Base.connection
......@@ -321,6 +327,10 @@ def test_schema_dump_with_shorthand
assert_match %r[t\.hstore "tags",\s+default: {}], output
end
def test_supports_to_unsafe_h_values
assert_equal("\"hi\"=>\"hi\"", @type.serialize(FakeParameters.new))
end
private
def assert_array_cycle(array)
# test creation
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册