diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-03-07 16:42:14 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-05-30 18:26:03 +0900 |
commit | 1c4be603c121a8853a4f1d6c13cb91f9b66bef9e (patch) | |
tree | b189e2cc2c8a1eaaa35b605e910818971b6208f0 /activerecord/lib/active_record/type/json.rb | |
parent | 5d9d6a4fbf72029266bfa643929a9a80f9286fbe (diff) | |
download | rails-1c4be603c121a8853a4f1d6c13cb91f9b66bef9e.tar.gz rails-1c4be603c121a8853a4f1d6c13cb91f9b66bef9e.tar.bz2 rails-1c4be603c121a8853a4f1d6c13cb91f9b66bef9e.zip |
Consolidate database specific JSON types to `Type::Json`
Diffstat (limited to 'activerecord/lib/active_record/type/json.rb')
-rw-r--r-- | activerecord/lib/active_record/type/json.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/json.rb b/activerecord/lib/active_record/type/json.rb new file mode 100644 index 0000000000..c4732fe388 --- /dev/null +++ b/activerecord/lib/active_record/type/json.rb @@ -0,0 +1,28 @@ +module ActiveRecord + module Type + class Json < ActiveModel::Type::Value + include ActiveModel::Type::Helpers::Mutable + + def type + :json + end + + def deserialize(value) + return value unless value.is_a?(::String) + ActiveSupport::JSON.decode(value) rescue nil + end + + def serialize(value) + ActiveSupport::JSON.encode(value) unless value.nil? + end + + def changed_in_place?(raw_old_value, new_value) + deserialize(raw_old_value) != new_value + end + + def accessor + ActiveRecord::Store::StringKeyedHashAccessor + end + end + end +end |