aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/internal/abstract_json.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2015-08-21 17:45:46 -0600
committerSean Griffin <sean@thoughtbot.com>2015-08-21 17:45:46 -0600
commitffc4710c2bff273b82ddb76675701f986d82ef4f (patch)
tree2e9a3fe31f804e5d08fc258789005a0c93acccfc /activerecord/lib/active_record/type/internal/abstract_json.rb
parent90bcb6dea7f8fc8b93b4266180465fc0c0785c01 (diff)
downloadrails-ffc4710c2bff273b82ddb76675701f986d82ef4f.tar.gz
rails-ffc4710c2bff273b82ddb76675701f986d82ef4f.tar.bz2
rails-ffc4710c2bff273b82ddb76675701f986d82ef4f.zip
JSON is still an adapter specific type.
Several changes were made in #21110 which I am strongly opposed to. (this is what I get for going on vacation. :trollface:) No type should be introduced into the generic `ActiveRecord::Type` namespace, and *certainly* should not be registered into the registry unconstrained unless it is supported by *all* adapters (which basically means that it was specified in the ANSI SQL standard). I do not think `# :nodoc:` ing the type is sufficient, as it still makes the code of Rails itself very unclear as to what the role of that class is. While I would argue that this shouldn't even be a super class, and that MySql and PG's JSON types are only superficially duplicated (they might look the same but will change for different reasons in the future). However, I don't feel strongly enough about it as a point of contention (and the biggest cost of harming the blameability has already occured), so I simply moved the superclass into a namespace where its role is absolutely clear. After this change, `attribute :foo, :json` will once again work with MySQL and PG, but not with Sqlite3 or any third party adapters. Unresolved questions -------------------- The types that and adapter publishes (at least those are unique to that adapter, and not adding additional behavior like `MysqlString` should probably be part of the adapter's public API. Should we standardize the namespace for these, and document them?
Diffstat (limited to 'activerecord/lib/active_record/type/internal/abstract_json.rb')
-rw-r--r--activerecord/lib/active_record/type/internal/abstract_json.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/internal/abstract_json.rb b/activerecord/lib/active_record/type/internal/abstract_json.rb
new file mode 100644
index 0000000000..963a8245d0
--- /dev/null
+++ b/activerecord/lib/active_record/type/internal/abstract_json.rb
@@ -0,0 +1,33 @@
+module ActiveRecord
+ module Type
+ module Internal # :nodoc:
+ class AbstractJson < Type::Value # :nodoc:
+ include Type::Helpers::Mutable
+
+ def type
+ :json
+ end
+
+ def deserialize(value)
+ if value.is_a?(::String)
+ ::ActiveSupport::JSON.decode(value) rescue nil
+ else
+ value
+ end
+ end
+
+ def serialize(value)
+ if value.is_a?(::Array) || value.is_a?(::Hash)
+ ::ActiveSupport::JSON.encode(value)
+ else
+ value
+ end
+ end
+
+ def accessor
+ ActiveRecord::Store::StringKeyedHashAccessor
+ end
+ end
+ end
+ end
+end