diff --git a/docs/3.0/fields.md b/docs/3.0/fields.md index 655c9320..902dfd9b 100644 --- a/docs/3.0/fields.md +++ b/docs/3.0/fields.md @@ -38,6 +38,104 @@ That will add a few fields in your new Avo app. On the and views, we'll get a new text column of that record's database value. Finally, on the and views, we will get a text input field that will display & update the `name` field on that model. +### Specific methods for each view + +The `fields` method in your resource is invoked whenever non-specific view methods are present. To specify fields for each view or a group of views, you can use the following methods: + +`index` view -> `index_fields`
+`show` view -> `show_fields`
+`edit` / `update` views -> `edit_fields`
+`new` / `create` views -> `new_fields` + +You can also register fields for a specific group of views as follows: + +`index` / `show` views -> `display_fields`
+`edit` / `update` / `new` / `create` views -> `form_fields` + +When specific view fields are defined, they take precedence over view group fields. If neither specific view fields nor view group fields are defined, the fields will be retrieved from the `fields` method. + +The below example use two custom helpers methods to organize the fields through `display_fields` and `form_fields` + +:::code-group +```ruby [display_fields] +def display_fields + base_fields + tool_fields +end +``` + +```ruby [form_fields] +def form_fields + base_fields + tool_fields + tool Avo::ResourceTools::CityEditor, only_on: :forms +end +``` + +```ruby [tool_fields (helper method)] +# Notice that even if those fields are hidden on the form, we still include them on `form_fields`. +# This is because we want to be able to edit them using the tool. +# When submitting the form, we need this fields declared on the resource in order to know how to process them and fill the record. +def tool_fields + with_options hide_on: :forms do + field :name, as: :text, help: "The name of your city", filterable: true + field :population, as: :number, filterable: true + field :is_capital, as: :boolean, filterable: true + field :features, as: :key_value + field :image_url, as: :external_image + field :tiny_description, as: :markdown + field :status, as: :badge, enum: ::City.statuses + end +end +``` + +```ruby [base_fields (helper method)] +def base_fields + field :id, as: :id + field :coordinates, as: :location, stored_as: [:latitude, :longitude] + field :city_center_area, + as: :area, + geometry: :polygon, + mapkick_options: { + style: "mapbox://styles/mapbox/satellite-v9", + controls: true + }, + datapoint_options: { + label: "Paris City Center", + tooltip: "Bonjour mes amis!", + color: "#009099" + } + field :description, + as: :trix, + attachment_key: :description_file, + visible: -> { resource.params[:show_native_fields].blank? } + field :metadata, + as: :code, + format_using: -> { + if view.edit? + JSON.generate(value) + else + value + end + }, + update_using: -> do + ActiveSupport::JSON.decode(value) + end + + field :created_at, as: :date_time, filterable: true +end +``` +::: + +:::warning In some scenarios fields require presence even if not visible +In certain situations, fields must be present in your resource configuration, even if they are hidden from view. Consider the following example where `tool_fields` are included within `form_fields` despite being wrapped in a `with_options hide_on: :forms do ... end` block. + +For instance, when using `tool Avo::ResourceTools::CityEditor, only_on: :forms`, it will render the `features` field, which is of type `key_value`. When the form is submitted, Avo relies on the presence of the `features` field to determine its type and properly parse the submitted value. + +If you omit the declaration of `field :features, as: :key_value, hide_on: :forms`, Avo will be unable to update that specific database column. +::: + + ## Field conventions When we declare a field, we pinpoint the specific database row for that field. Usually, that's a snake case value. diff --git a/yarn.lock b/yarn.lock index 1b755363..1aa302e1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -635,9 +635,9 @@ camelcase-css@^2.0.1: integrity sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA== caniuse-lite@^1.0.30001370, caniuse-lite@^1.0.30001373: - version "1.0.30001480" - resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001480.tgz" - integrity sha512-q7cpoPPvZYgtyC4VaBSN0Bt+PJ4c4EYRf0DrduInOz2SkFpHD5p3LnvEpqBp7UnJn+8x1Ogl1s38saUxe+ihQQ== + version "1.0.30001546" + resolved "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001546.tgz" + integrity sha512-zvtSJwuQFpewSyRrI3AsftF6rM0X80mZkChIt1spBGEvRglCrjTniXvinc8JKRoqTwXAgvqTImaN9igfSMtUBw== chokidar@^3.5.3: version "3.5.3"