|
| 1 | +--- |
| 2 | +outline: [2,3] |
| 3 | +version: "3.17.0" |
| 4 | +license: community |
| 5 | +betaStatus: Beta |
| 6 | +--- |
| 7 | + |
| 8 | +# Field Discovery |
| 9 | + |
| 10 | + |
| 11 | +`discover_columns` and `discover_associations` automatically detect and configure fields for your Avo resources based on your model's database structure. |
| 12 | + |
| 13 | +```rb{6-7} |
| 14 | +# app/avo/resources/user.rb |
| 15 | +class Avo::Resources::User < Avo::BaseResource |
| 16 | + # ... |
| 17 | +
|
| 18 | + def fields |
| 19 | + discover_columns |
| 20 | + discover_associations |
| 21 | + end |
| 22 | +end |
| 23 | +``` |
| 24 | + |
| 25 | +## Options |
| 26 | + |
| 27 | +<Option name="`only`"> |
| 28 | + |
| 29 | +Specify which fields should be discovered, excluding all others. |
| 30 | + |
| 31 | +```rb{6-7} |
| 32 | +# app/avo/resources/post.rb |
| 33 | +class Avo::Resources::Post < Avo::BaseResource |
| 34 | + # ... |
| 35 | +
|
| 36 | + def fields |
| 37 | + discover_columns only: [:title, :body, :published_at] |
| 38 | + discover_associations only: [:author, :comments] |
| 39 | + end |
| 40 | +end |
| 41 | +``` |
| 42 | + |
| 43 | +##### Default value |
| 44 | + |
| 45 | +`nil` |
| 46 | + |
| 47 | +#### Possible values |
| 48 | + |
| 49 | +Array of symbols representing column or association names |
| 50 | + |
| 51 | +</Option> |
| 52 | + |
| 53 | +<Option name="`except`"> |
| 54 | + |
| 55 | +Specify which fields should be excluded from discovery. |
| 56 | + |
| 57 | +```rb{6-7} |
| 58 | +# app/avo/resources/post.rb |
| 59 | +class Avo::Resources::Post < Avo::BaseResource |
| 60 | + # ... |
| 61 | +
|
| 62 | + def fields |
| 63 | + discover_columns except: [:metadata, :internal_notes] |
| 64 | + discover_associations except: [:audit_logs] |
| 65 | + end |
| 66 | +end |
| 67 | +``` |
| 68 | + |
| 69 | +##### Default value |
| 70 | + |
| 71 | +`nil` |
| 72 | + |
| 73 | +#### Possible values |
| 74 | + |
| 75 | +Array of symbols representing column or association names |
| 76 | + |
| 77 | +</Option> |
| 78 | + |
| 79 | +<Option name="`column_names_mapping`"> |
| 80 | + |
| 81 | +Override how specific column names are mapped to field types globally. |
| 82 | + |
| 83 | +```rb{5-8} |
| 84 | +# config/initializers/avo.rb |
| 85 | +Avo.configure do |config| |
| 86 | + # ... |
| 87 | +
|
| 88 | + config.column_names_mapping = { |
| 89 | + published_at: { field: :date_time, timezone: 'UTC' }, |
| 90 | + role: { field: :select, enum: -> { User.roles } } |
| 91 | + } |
| 92 | +end |
| 93 | +``` |
| 94 | + |
| 95 | +##### Default value |
| 96 | + |
| 97 | +`{}` |
| 98 | + |
| 99 | +#### Possible values |
| 100 | + |
| 101 | +Hash mapping column names to field configurations |
| 102 | + |
| 103 | +</Option> |
| 104 | + |
| 105 | +<Option name="`column_types_mapping`"> |
| 106 | + |
| 107 | +Override how database column types are mapped to field types globally. |
| 108 | + |
| 109 | +```rb{5-8} |
| 110 | +# config/initializers/avo.rb |
| 111 | +Avo.configure do |config| |
| 112 | + # ... |
| 113 | +
|
| 114 | + config.column_types_mapping = { |
| 115 | + jsonb: { field: :code, language: 'json' }, |
| 116 | + decimal: { field: :number, decimals: 2 } |
| 117 | + } |
| 118 | +end |
| 119 | +``` |
| 120 | + |
| 121 | +##### Default value |
| 122 | + |
| 123 | +`{}` |
| 124 | + |
| 125 | +#### Possible values |
| 126 | + |
| 127 | +Hash mapping database column types to field configurations |
| 128 | + |
| 129 | +</Option> |
| 130 | + |
| 131 | +## Examples |
| 132 | + |
| 133 | +### Basic Discovery |
| 134 | + |
| 135 | +```rb{6-7} |
| 136 | +# app/avo/resources/user.rb |
| 137 | +class Avo::Resources::User < Avo::BaseResource |
| 138 | + # ... |
| 139 | +
|
| 140 | + def fields |
| 141 | + discover_columns |
| 142 | + discover_associations |
| 143 | + end |
| 144 | +end |
| 145 | +``` |
| 146 | + |
| 147 | +### Custom Field Options |
| 148 | + |
| 149 | +This will add the provided options to every discovered field or association. This is particularly useful when having duplicative configurations across many fields. |
| 150 | + |
| 151 | +```rb{6-7} |
| 152 | +# app/avo/resources/post.rb |
| 153 | +class Avo::Resources::Post < Avo::BaseResource |
| 154 | + # ... |
| 155 | +
|
| 156 | + def fields |
| 157 | + discover_columns help: "Automatically discovered fields" |
| 158 | + discover_associations searchable: false |
| 159 | + end |
| 160 | +end |
| 161 | +``` |
| 162 | + |
| 163 | +### Combining Manual and Discovered Fields |
| 164 | + |
| 165 | +```rb{6,8-9,11} |
| 166 | +# app/avo/resources/project.rb |
| 167 | +class Avo::Resources::Project < Avo::BaseResource |
| 168 | + # ... |
| 169 | +
|
| 170 | + def fields |
| 171 | + field :custom_field, as: :text |
| 172 | +
|
| 173 | + discover_columns except: [:custom_field] |
| 174 | + discover_associations |
| 175 | +
|
| 176 | + field :another_custom_field, as: :boolean |
| 177 | + end |
| 178 | +end |
| 179 | +``` |
| 180 | + |
| 181 | +## Automatic Type Mapping |
| 182 | + |
| 183 | +Field discovery maps database column types to Avo field types automatically. |
| 184 | +e.g. |
| 185 | + |
| 186 | +- `string` → `:text` |
| 187 | +- `integer` → `:number` |
| 188 | +- `float` → `:number` |
| 189 | +- `datetime` → `:datetime` |
| 190 | +- `boolean` → `:boolean` |
| 191 | +- `json/jsonb` → `:code` |
| 192 | + |
| 193 | +The full, up-to-date list can be found [here](https://github.com/avo-hq/avo/blob/main/lib/avo/mappings.rb) |
| 194 | + |
| 195 | +## Association Discovery |
| 196 | + |
| 197 | +The following associations are automatically configured: |
| 198 | + |
| 199 | +- `belongs_to` → `:belongs_to` |
| 200 | +- `has_one` → `:has_one` |
| 201 | +- `has_many` → `:has_many` |
| 202 | +- `has_one_attached` → `:file` |
| 203 | +- `has_many_attached` → `:files` |
| 204 | +- `has_rich_text` → `:trix` |
| 205 | +- `acts-as-taggable-on :tags` → `:tags` |
| 206 | + |
| 207 | +The full, up-to-date list can be found [here](https://github.com/avo-hq/avo/blob/main/lib/avo/mappings.rb) |
0 commit comments