diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-29 13:17:47 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-29 13:17:47 -0600 |
commit | fb8ac4f7b8487e4bb5c241dc0ba74da30f21ce9f (patch) | |
tree | 6e4e708b0cdffbaf26a2d19e49b4b14fa0282173 /activerecord/lib | |
parent | 9ca0f8da2abe735c57eacd9b687ee7c3fae3685d (diff) | |
download | rails-fb8ac4f7b8487e4bb5c241dc0ba74da30f21ce9f.tar.gz rails-fb8ac4f7b8487e4bb5c241dc0ba74da30f21ce9f.tar.bz2 rails-fb8ac4f7b8487e4bb5c241dc0ba74da30f21ce9f.zip |
Use the type object for sending JSON to the database
Diffstat (limited to 'activerecord/lib')
3 files changed, 10 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb index a865c5c310..b0a161ff32 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/cast.rb @@ -34,28 +34,12 @@ module ActiveRecord 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: from = object.begin.respond_to?(:infinite?) && object.begin.infinite? ? '' : object.begin to = object.end.respond_to?(:infinite?) && object.end.infinite? ? '' : object.end "[#{from},#{to}#{object.exclude_end? ? ')' : ']'}" end - def string_to_json(string) # :nodoc: - if String === string - ActiveSupport::JSON.decode(string) - else - string - end - end - private HstorePair = begin diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb index ab1165f301..e12ddd9901 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb @@ -10,11 +10,19 @@ module ActiveRecord end 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 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 def accessor diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb index 4caed77952..2818633495 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb @@ -30,14 +30,12 @@ module ActiveRecord when Array case sql_type when 'point' then super(PostgreSQLColumn.point_to_string(value)) - when 'json' then super(PostgreSQLColumn.json_to_string(value)) else super(value, array_column(column)) end when Hash case sql_type when 'hstore' then super(PostgreSQLColumn.hstore_to_string(value), column) - when 'json' then super(PostgreSQLColumn.json_to_string(value), column) else super end when Float @@ -92,14 +90,12 @@ module ActiveRecord when Array case column.sql_type when 'point' then PostgreSQLColumn.point_to_string(value) - when 'json' then PostgreSQLColumn.json_to_string(value) else super(value, array_column(column)) end when Hash case column.sql_type when 'hstore' then PostgreSQLColumn.hstore_to_string(value, array_member) - when 'json' then PostgreSQLColumn.json_to_string(value) else super(value, column) end else |