|
| 1 | +--- |
| 2 | +# license: |
| 3 | +betaStatus: Alpha 🧪 (experimental) |
| 4 | +outline: [2,3] |
| 5 | +--- |
| 6 | + |
| 7 | +# Avo::Meta |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +`Avo::Meta` equips your [Avo](https://github.com/avo-hq/avo/) application with the ability to add arbitrary _"meta"_ attributes to your resources in a no-code manner. |
| 12 | + |
| 13 | +Under the hood, it leverages a JSON database column and [StoreModel](https://github.com/DmitryTsepelev/store_model) to manage it. |
| 14 | + |
| 15 | +## Installation |
| 16 | +Add this line to your application's Gemfile: |
| 17 | + |
| 18 | +```ruby |
| 19 | +gem "avo-meta", source: "https://packager.dev/avo-hq/" |
| 20 | +``` |
| 21 | + |
| 22 | +And then execute: |
| 23 | +```bash |
| 24 | +$ bundle |
| 25 | +``` |
| 26 | + |
| 27 | +Additionally, you have to mount the engine, e.g.: |
| 28 | + |
| 29 | +``` |
| 30 | +mount Avo::Meta::Engine, at: "#{Avo.configuration.root_path}/avo_meta/" |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +To use `Avo::Meta`, an additional database table containing the _schemas_ for each resource has to be created. For this, simply install the necessary migrations: |
| 35 | + |
| 36 | +```bash |
| 37 | +$ bin/rails avo_meta:install:migrations |
| 38 | +$ bin/rails db:migrate |
| 39 | +``` |
| 40 | + |
| 41 | +## Usage |
| 42 | + |
| 43 | +### Preparation |
| 44 | + |
| 45 | +First, add a `meta` JSON column to your model: |
| 46 | + |
| 47 | +```sh |
| 48 | +$ bin/rails g migration AddMetaToUsers meta:json |
| 49 | +$ bin/rails db:migrate |
| 50 | +``` |
| 51 | + |
| 52 | +After that, simply include the `Avo::Metaable` module in said model: |
| 53 | + |
| 54 | +```rb |
| 55 | +class User < ApplicationRecord |
| 56 | + include Avo::Metaable |
| 57 | +end |
| 58 | +``` |
| 59 | + |
| 60 | +This will create two things under the hood: |
| 61 | + |
| 62 | +- 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_schemas` database table created via the migrations. |
| 63 | + |
| 64 | +> [!NOTE] |
| 65 | +> If the `meta` JSON column is missing on your model, a reminder to add it will be emitted. |
| 66 | +
|
| 67 | +- a `meta` class attribute for the associated Avo resource (in the example above, the `User` resource). This class attribute contains the schema definition (essentially a hash containing `name`, `type`, `as`, and other attributes) of the meta entries stored with the model. This will result in a "Meta" panel in your resource `New/Show/Edit` views where you can edit the defined attributes. |
| 68 | + |
| 69 | +To display the meta panel in your resource view to modify these attributes, simply add `meta_panel` to your `def fields` definition: |
| 70 | + |
| 71 | +```rb |
| 72 | +class Avo::Resources::User < Avo::BaseResource |
| 73 | + # ... |
| 74 | + |
| 75 | + def fields |
| 76 | + # ... |
| 77 | + |
| 78 | + meta_panel |
| 79 | + end |
| 80 | +end |
| 81 | +``` |
| 82 | + |
| 83 | + |
| 84 | +### Accessing Meta Attributes |
| 85 | + |
| 86 | +To use the meta attributes in your application, simply access them like you would access a `has_one` association: |
| 87 | + |
| 88 | +`@user.meta.shoe_size` |
0 commit comments