Avo::Meta
Overview
Avo::Meta equips your Avo application with the ability to add arbitrary "meta" attributes to your resources in a no-code manner.
Under the hood, it leverages a JSON database column and StoreModel to manage it.
Installation
Add this line to your application's Gemfile:
gem "avo-meta", source: "https://packager.dev/avo-hq/"And then execute:
$ bundleTo use Avo::Meta, an additional database table containing the schemas for each resource has to be created. For this, simply install the necessary migrations:
$ bin/rails avo_meta:install:migrations
$ bin/rails db:migrateUsage
Preparation
First, add a meta JSON column to your model:
$ bin/rails g migration AddMetaToUsers meta:json
$ bin/rails db:migrateAfter that, simply include the Avo::Metaable module in said model:
class User < ApplicationRecord
include Avo::Metaable
endThis will create two things under the hood:
- a "Meta Properties" resource available in the sidebar. Here, you can add new meta properties for each associated resource by adding a name and a type. Under the hood, it modifies the
avo_meta_schemasdatabase table created via the migrations.
NOTE
If the meta JSON column is missing on your model, a reminder to add it will be emitted.
- a
metaclass attribute for the associated Avo resource (in the example above, theUserresource). This class attribute contains the schema definition (essentially a hash containingname,type,as, and other attributes) of the meta entries stored with the model. This will result in a "Meta" panel in your resourceNew/Show/Editviews where you can edit the defined attributes.
To display the meta panel in your resource view to modify these attributes, simply add meta_panel to your def fields definition:
class Avo::Resources::User < Avo::BaseResource
# ...
def fields
# ...
meta_panel
end
endAccessing Meta Attributes
To use the meta attributes in your application, simply access them like you would access a has_one association:
@user.meta.shoe_size
Menu entry
By default, "Meta Properties" will automatically appear in the sidebar if you are not using a custom menu.
If you are using a custom menu, you need to explicitly add the menu entry:
# config/initializers/avo.rb
Avo.configure do |config|
config.main_menu = -> {
section "Meta" do
resource :meta_schema
end
}
end