avo-hq / avo

Build Ruby on Rails apps 10x faster
https://avohq.io
Other
1.53k stars 256 forks source link

Add automatic field detection in resources #3390

Open adrianthedev opened 2 weeks ago

adrianthedev commented 2 weeks ago

Feature

Some users would like to get all or some fields automatically from it's model class. We can check the model attributes and foreign keys similar to how we discover them in the resource generator.

Current workarounds

Declare them manually as fields or build your own method.

Possible API

[!NOTE]
Naming is TBD

# == Schema Information
#
# Table name: users
#
#  id                     :bigint           not null, primary key
#  name                   :string
#  age                    :string
#  is_admin               :integer
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
class User < ApplicationRecord
end

# before
class Avo::Resources::User < Avo::BaseResource
  def fields
    field :id, as: :id
    field :name, as: :text
    field :age, as: :number
    field :is_admin, as: :boolean
    field :created_at, as: :date_time

    field :comments, as: :has_many
  end
end

# after
class Avo::Resources::User < Avo::BaseResource
  def fields
    # adds fields for all attributes
    discover_fields

    # selectively pick fields
    discover_fields only_attributes: [:id, :name]

    discover_fields except_attributes: [:age]

    # associations 
    discover_associations
  end
end
github-actions[bot] commented 1 day ago

This issue has been marked as stale because there was no activity for the past 15 days.