Administrate: Field::JSON Needed

Created on 17 Dec 2016  路  5Comments  路  Source: thoughtbot/administrate

Im excited to use administrate for my Rails 5 API app. Unfortunately, its treating my model's JSON attribute as a String, which then causes some callbacks to fail as they use methods available to enumerables like Hash.

This is likely a common issue that anyone using devise_token_auth will run into. For example, running into this error when editing a resource:

NoMethodError (undefined method `delete_if' for #<String ...

Now the solution proposed by @eddietejeda with his Field::JSON gem looks great. It looks like itl likely solve my problem for me, or at least, it has given me a better insight into creating my own custom Field that will handle JSON attributes like Hashes rather than Strings. Unfortunately it has a dependency set to a version of Administrate _before_ Rails 5 support is added, so for now Ill need to roll my own.

Just has me thinking - since Rails commonly supports JSON APIs, and JSON is a common column type for all the most popular SQL RDBMS vendors, it should be expected that many adopters will want support for Administrate::Field that handles JSON model attributes.

Could there be a Field type that handles JSON or similar data structures for model attributes in the future?

Most helpful comment

Hi there, we have built the gem for this https://github.com/codica2/administrate-field-jsonb

All 5 comments

I agree that JSON fields are becoming much more common, but I feel that I'd rather Administrate had a wide collection of associated gems which added support for more custom things like this. For example, you might want to use a different style editor for JSON.

I've gone ahead an opened a PR on @eddietejeda's repo: https://github.com/eddietejeda/administrate-field-json/pull/11 which will fix Rails 5 support for you. I'd recommend working off my branch and trying to contribute back any fixes there.

Totally understand your position. Thanks!

I would suggest adding a basic support for all database column types. Currently there isn't a way to handle JSON model attributes, because they get converted to String objects on update.

@preist That seems reasonable! If you have any thoughts on this, please open another issue (or better, a PR!) where we can discuss it!

Hi there, we have built the gem for this https://github.com/codica2/administrate-field-jsonb

Was this page helpful?
0 / 5 - 0 ratings