codica2 / administrate-field-jsonb

A plugin to show and edit JSON objects within Administrate.
MIT License
45 stars 17 forks source link

Advanced View, but with user-defined keys #14

Open allthesignals opened 4 years ago

allthesignals commented 4 years ago

First of all, I love this gem — amazing that it just works perfectly.

Perhaps I'm misunderstanding the documentation, but is there a way to use advanced view but allow for custom key objects?

For example:

      "user_defined_key_here": {  
        table: "2018",
        sources: [
          {name: "lcgms", version: '2018-12-19', minYear: 2018, maxYear: 2019},
          {name: "bluebook", minYear: 2017, maxYear: 2018}
        ]
      },

With advanced view, I find it can enforce an up-front structure of the JSON schema. Is this possible? For now, I can just use the jsoneditor, but I find your advanced view to be much simpler.

That said, this may be a sign that I need to use some other kinds of objects in Rails (read: models), but this is great for now.

volkov-sergey commented 4 years ago

Hi @allthesignals thanks and sorry for late response.

If I got the point you want to get something like this:

Screenshot from 2020-07-17 12-05-55

I did it with the code:

ATTRIBUTE_TYPES = {
  ...
  jsonb_field: Field::JSONB.with_options(transform: %i[symbolize_keys], advanced_view: {
    user_defined_key_here: Field::JSONB.with_options(transform: %i[symbolize_keys], advanced_view: {
      table: Field::String,
      sources: Field::JSONB.with_options(transform: %i[symbolize_keys], advanced_view: {
        name: Field::String,
        version: Field::String,
        minYear: Field::String,
        maxYear: Field::String
      })
    })
  })
}.freeze

Please update me if I didn't got your point correct. Thanks!