pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/codica2/administrate-field-jsonb

origen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-9e07ff8eaaaff3a3.css" /> GitHub - codica2/administrate-field-jsonb: A plugin to show and edit JSON objects within Administrate. · GitHub
Skip to content

codica2/administrate-field-jsonb

Repository files navigation

Administrate::Field::Jsonb

A plugin to show and edit JSON objects within Administrate. inspired by Administrate::Field::JSON.

This gem uses jsoneditor.

Installation

Add this line to your application's Gemfile:

gem 'administrate-field-jsonb'

And then execute:

bundle

If you are using asset pipeline, add the following lines to your manifest.js:

//= link administrate-field-jsonb/application.css
//= link administrate-field-jsonb/application.js

The manifest file is at app/assets/config by default.

Usage

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB
}.freeze

If you have some kind of serialization, you can call methods on your object with transform option.

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB.with_options(
    transform: %w[to_h symbolize_keys]
  )
}.freeze

It also supports Proc.

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB.with_options(
    transform: [:transformation, Proc.new { |item| item.merge({ foo: 'bar' }) }]
  )
}.freeze

And there is a built in parse_json option, it will call JSON.parse(your_object) on your object.

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB.with_options(
    transform: [:parse_json, :some_other_stuff]
  )
}.freeze

If you want to edit json displaying on show page, you can use advanced_view option (both JSON and arrays are supported).

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB.with_options(transform: %i[to_h symbolize_keys], advanced_view: {
    company:  Field::String,
    position: Field::String,
    skills: Field::JSONB.with_options(advanced_view: {
      'name'  => Field::String,
      'years' => Field::Number.with_options(suffix: ' years')
    })
  }),
  languages: Field::JSONB.with_options(advanced_view: {
    'title' => Field::String,
    'code'  => Field::String,
  })
}.freeze

NOTE: don't define advanced_view option if you want to display JSON with the jsoneditor.

To customize what to display if you have an empty value, use blank_sign option, by default it's -.

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB.with_options(
    blank_sign: 'oops, missed'
  )
}.freeze

In some situations you may preffer not to nullify empty records. For example you store your JSONB in not null database column and always have at least an empty json object {}. In such situation you can use nil_blank option:

ATTRIBUTE_TYPES = {
  # ...
  details: Field::JSONB.with_options(
    nil_blank: false
  )
}.freeze

How it looks like

Form

Show

jsoneditor mode

advanced_view mode

advanced_view object

advanced_view array

Recipes

If you want to store your JSON in hash format and not a string add this to your model.

def your_field_name=(value)
  self[:your_field_name] = value.is_a?(String) ? JSON.parse(value) : value
end

Example:

def details=(value)
  self[:details] = value.is_a?(String) ? JSON.parse(value) : value
end

If you don't see details in advanced_view, try to add this

transform: %i[to_h symbolize_keys]

or use string keys.

languages: Field::JSONB.with_options(advanced_view: {
  'title' => Field::String,
  'code'  => Field::String,
})

License

Copyright © 2015-2025 Codica. It is released under the MIT License.

About Codica

Codica logo

Administrate::Field::Jsonb is maintained by Codica. The names and logos for Codica are trademarks of Codica.

We love open source software! See our other projects or hire us to design, develop, and grow your product.

About

A plugin to show and edit JSON objects within Administrate.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy