Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automatic field detection in resources #3390

Open
adrianthedev opened this issue Nov 6, 2024 · 0 comments
Open

Add automatic field detection in resources #3390

adrianthedev opened this issue Nov 6, 2024 · 0 comments

Comments

@adrianthedev
Copy link
Collaborator

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

1 participant