Ruby Developer Guide
Other documentationsDemoCommunityGitHub
  • Forest Admin
  • Getting started
    • How it works
    • Quick start
      • Ruby on Rails
    • Create your agent
    • Troubleshooting
    • Migrating legacy agents
      • Pre-requisites
      • Recommendations
      • Migration steps
      • Code transformations
        • API Charts
        • Live Queries
        • Smart Charts
        • Route overrides
        • Smart Actions
        • Smart Fields
        • Smart Relationships
        • Smart Segments
  • Data Sources
    • Getting Started
      • Collection selection
      • Naming conflicts
      • Query interface and Native Queries
        • Fields and projections
        • Filters
        • Aggregations
    • Provided data sources
      • ActiveRecord
        • Polymorphic relationships
      • Mongoid
    • Write your own
      • Translation strategy
        • Structure declaration
        • Capabilities declaration
        • Read implementation
        • Write implementation
        • Intra-data source Relationships
      • Contribute
  • Agent customization
    • Getting Started
    • Actions
      • Scope and context
      • Result builder
      • Static Forms
      • Widgets in Forms
      • Dynamic Forms
      • Form layout customization
      • Related data invalidation
    • Charts
      • Value
      • Objective
      • Percentage
      • Distribution
      • Leaderboard
      • Time-based
    • Fields
      • Add fields
      • Move, rename and remove fields
      • Override binary field mode
      • Override writing behavior
      • Override filtering behavior
      • Override sorting behavior
      • Validation
    • Hooks
      • Collection hook
      • Collection override
    • Pagination
    • Plugins
      • Write your own
    • Relationships
      • To a single record
      • To multiple records
      • Computed foreign keys
      • Under the hood
    • Search
    • Segments
  • Frontend customization
    • Smart Charts
      • Create a table chart
      • Create a bar chart
      • Create a cohort chart
      • Create a density map
    • Smart Views
      • Create a Map view
      • Create a Calendar view
      • Create a Shipping view
      • Create a Gallery view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
  • Deploying to production
    • Environments
      • Deploy on AWS
      • Deploy on Heroku
      • Deploy on GCP
      • Deploy on Ubuntu
    • Development workflow
    • Using branches
    • Deploying your changes
    • Forest Admin CLI commands
      • init
      • login
      • branch
      • switch
      • set-origin
      • push
      • environments:create
      • environments:reset
      • deploy
  • Under the hood
    • .forestadmin-schema.json
    • Data Model
      • Typing
      • Relationships
    • Security & Privacy
Powered by GitBook
On this page
  • Columns
  • Examples
  • Typing
  • Validation
  • Relationships
  • Examples
  • Typing

Was this helpful?

  1. Data Sources
  2. Write your own
  3. Translation strategy

Structure declaration

This is the official documentation of the agent_ruby Ruby agent.

Creating a custom data source always starts with declaring the structure of the data

  • Which collections are present?

  • What fields do they contain?

  • What are their types?

Columns

Examples

module App
  module Collections
    class Movie < ForestAdminDatasourceToolkit::Collection
      include ForestAdminDatasourceToolkit::Schema
      include ForestAdminDatasourceToolkit::Components::Query:: ConditionTree

      def initialize(datasource)
        super(datasource, 'Movie')

        add_field('id', ColumnSchema.new(
          column_type: 'Number',
          is_primary_key: true,
        ))

        add_field('title', ColumnSchema.new(
          column_type: 'String',
          filter_operators: [Operators::PRESENT],
        ))

        add_field('mpa_rating', ColumnSchema.new(
          column_type: 'Enum',
          default_value: 'G',
          enum_values: %w[G PG PG-13 R NC-17],
        ))

        add_field('stars', ColumnSchema.new(
          column_type: [
            {
              'firstName' => 'String',
              'lastName'  => 'String',
            },
          ]
        ))
    end
  end
end

Typing

The typing system for columns is the same as the one used when declaring fields in the agent customization step.

Validation

When using primitive type fields, Forest Admin supports declaring a validation clause, which will be imported into the UI of the admin panel to validate records before creating/updating them.

{
  "aggregator": "and",
  "conditions": [
    { "operator": "present" },
    { "operator": "like", "value": "found%" },
    { "operator": "today" }
  ]
}

Relationships

Only intra-data source relationships should be declared at the collection level.

You can declare relationships at the collection level, but that means that the data source you are making is responsible for handling them.

Examples

module App
  module Collections
    class Movie < ForestAdminDatasourceToolkit::Collection
      include ForestAdminDatasourceToolkit::Schema
      # [...]

      add_field('director', ManyToOneSchema.new(
        foreign_key: 'director_id',
        foreign_key_target: 'id',
        foreign_collection: 'People'
      ))

      add_field('actors', ManyToManySchema.new(
        origin_key: 'movie_id',
        origin_key_target: 'id',
        foreign_key: 'actor_id',
        foreign_key_target: 'id',
        foreign_collection: 'People',
        through_collection: 'ActorsOnMovies'
      ))
    end
  end
end

Typing

The typing system for relationships is the same as the one used when declaring jointures in the agent customization step.

PreviousTranslation strategyNextCapabilities declaration

Last updated 4 months ago

Was this helpful?

You can read all about it in .

The API for validation is the same as with , besides the fact that there is no "field" entry.

For inter-data source relationships, you should use .

This will work out of the box for data sources using the "local-cache" strategy, however, please read , before starting with the "query translation" strategy.

You can read all about it in .

"Under the hood > Data Model > Typing"
jointures at the customization step
"Intra-data source Relationships"
"Under the hood > Data Model > Relationships"
condition trees