codica2 / administrate-field-jsonb

A plugin to show and edit JSON objects within Administrate.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Displays/assigns null when showing/editing empty object

sedubois opened this issue · comments

There are 3 related issues when a JSONB field contains a non-null but empty object ([] or {}).

1/ It is rendered as - on show view:
Screenshot 2019-10-31 at 14 01 59
2/ It is rendered as null on edit view:
Screenshot 2019-10-31 at 14 02 06
3/ When saving the form, even though I have not modified anything, instead of keeping the original value it tries to assign the value null, which breaks my non-null DB constraint and throws an error.

Instead, it should render the empty array or empty object, as below:
Screenshot 2019-10-31 at 14 05 28
Screenshot 2019-10-31 at 14 08 33

My code:

# db/schema.rb
  create_table "post_translations", force: :cascade do |t|
    ...
    t.jsonb "tags", default: [], null: false
end

# app/dashboards/post/translation_dashboard.rb
class Post::TranslationDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    ...
    tags: Field::JSONB,
    ...
  }.freeze
  ...
end

(+ using workaround described in #1 (comment))

Using administrate-field-jsonb 0.4.1.