mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use the type object for sending JSON to the database
This commit is contained in:
parent
9ca0f8da2a
commit
fb8ac4f7b8
4 changed files with 11 additions and 23 deletions
|
@ -34,28 +34,12 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def json_to_string(object) # :nodoc:
|
|
||||||
if Hash === object || Array === object
|
|
||||||
ActiveSupport::JSON.encode(object)
|
|
||||||
else
|
|
||||||
object
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def range_to_string(object) # :nodoc:
|
def range_to_string(object) # :nodoc:
|
||||||
from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin
|
from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin
|
||||||
to = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end
|
to = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end
|
||||||
"[#{from},#{to}#{object.exclude_end? ? ')' : ']'}"
|
"[#{from},#{to}#{object.exclude_end? ? ')' : ']'}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def string_to_json(string) # :nodoc:
|
|
||||||
if String === string
|
|
||||||
ActiveSupport::JSON.decode(string)
|
|
||||||
else
|
|
||||||
string
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
HstorePair = begin
|
HstorePair = begin
|
||||||
|
|
|
@ -10,11 +10,19 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_from_database(value)
|
def type_cast_from_database(value)
|
||||||
ConnectionAdapters::PostgreSQLColumn.string_to_json(value)
|
if value.is_a?(::String)
|
||||||
|
::ActiveSupport::JSON.decode(value)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def type_cast_for_database(value)
|
def type_cast_for_database(value)
|
||||||
ConnectionAdapters::PostgreSQLColumn.json_to_string(value)
|
if value.is_a?(::Array) || value.is_a?(::Hash)
|
||||||
|
::ActiveSupport::JSON.encode(value)
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def accessor
|
def accessor
|
||||||
|
|
|
@ -30,14 +30,12 @@ module ActiveRecord
|
||||||
when Array
|
when Array
|
||||||
case sql_type
|
case sql_type
|
||||||
when 'point' then super(PostgreSQLColumn.point_to_string(value))
|
when 'point' then super(PostgreSQLColumn.point_to_string(value))
|
||||||
when 'json' then super(PostgreSQLColumn.json_to_string(value))
|
|
||||||
else
|
else
|
||||||
super(value, array_column(column))
|
super(value, array_column(column))
|
||||||
end
|
end
|
||||||
when Hash
|
when Hash
|
||||||
case sql_type
|
case sql_type
|
||||||
when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column)
|
when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column)
|
||||||
when 'json' then super(PostgreSQLColumn.json_to_string(value), column)
|
|
||||||
else super
|
else super
|
||||||
end
|
end
|
||||||
when Float
|
when Float
|
||||||
|
@ -92,14 +90,12 @@ module ActiveRecord
|
||||||
when Array
|
when Array
|
||||||
case column.sql_type
|
case column.sql_type
|
||||||
when 'point' then PostgreSQLColumn.point_to_string(value)
|
when 'point' then PostgreSQLColumn.point_to_string(value)
|
||||||
when 'json' then PostgreSQLColumn.json_to_string(value)
|
|
||||||
else
|
else
|
||||||
super(value, array_column(column))
|
super(value, array_column(column))
|
||||||
end
|
end
|
||||||
when Hash
|
when Hash
|
||||||
case column.sql_type
|
case column.sql_type
|
||||||
when 'hstore' then PostgreSQLColumn.hstore_to_string(value, array_member)
|
when 'hstore' then PostgreSQLColumn.hstore_to_string(value, array_member)
|
||||||
when 'json' then PostgreSQLColumn.json_to_string(value)
|
|
||||||
else super(value, column)
|
else super(value, column)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
|
|
@ -77,7 +77,7 @@ class PostgresqlJSONTest < ActiveRecord::TestCase
|
||||||
column = JsonDataType.columns_hash["payload"]
|
column = JsonDataType.columns_hash["payload"]
|
||||||
|
|
||||||
data = "{\"a_key\":\"a_value\"}"
|
data = "{\"a_key\":\"a_value\"}"
|
||||||
hash = column.class.string_to_json data
|
hash = column.type_cast_from_database(data)
|
||||||
assert_equal({'a_key' => 'a_value'}, hash)
|
assert_equal({'a_key' => 'a_value'}, hash)
|
||||||
assert_equal({'a_key' => 'a_value'}, column.type_cast_from_database(data))
|
assert_equal({'a_key' => 'a_value'}, column.type_cast_from_database(data))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue