Class: AgentCode::AgentCodeModel

Inherits:
ApplicationRecord
  • Object
show all
Includes:
HasAgentCode, HasAutoScope, HasValidation, HidableColumns
Defined in:
lib/agentcode/models/agentcode_model.rb

Overview

AgentCodeModel -- Pre-composed base class for AgentCode-powered ActiveRecord models.

Extends ApplicationRecord and includes the most commonly needed concerns for AgentCode's automatic REST API generation. Subclass this instead of ApplicationRecord to get query building, validation, column hiding, and auto-scopes out of the box.

Quick Start

class Post < AgentCode::AgentCodeModel
agentcode_filters :status, :user_id
agentcode_sorts :created_at, :title
agentcode_default_sort '-created_at'
agentcode_includes :user, :comments
agentcode_search :title, :content

# Standard Rails validations for type/format (NOT presence — use allow_nil: true)
validates :title, length: { maximum: 255 }, allow_nil: true
validates :status, inclusion: { in: %w[draft published] }, allow_nil: true

# Field permissions are controlled by the policy (PostPolicy).
# See: permitted_attributes_for_create / permitted_attributes_for_update

belongs_to :user
has_many :comments
end

Included Concerns

Concern           | Purpose
------------------|-----------------------------------------------------------
HasAgentCode         | Query builder DSL (filters, sorts, includes, etc.)
HasValidation     | Format validation for request data
HidableColumns    | Dynamic column hiding from API responses
HasAutoScope      | Auto-discovery of ModelScopes::{Model}Scope classes

Optional Concerns (add manually when needed)

These concerns are NOT included in AgentCodeModel because they require additional database columns, gems, or relationships. Include them in your model subclass as needed:

Concern                     | Purpose
----------------------------|---------------------------------------------------
AgentCode::HasAuditTrail       | Automatic change logging to +audit_logs+ table
AgentCode::HasUuid             | Auto-generated UUID on creation
AgentCode::BelongsToOrganization | Multi-tenant organization scoping
AgentCode::HasPermissions      | Permission checking (User model only)
Discard::Model              | Soft deletes via the Discard gem

class Invoice < AgentCode::AgentCodeModel
include AgentCode::HasAuditTrail
include AgentCode::BelongsToOrganization
include Discard::Model

agentcode_filters :status, :client_id
agentcode_sorts :created_at, :amount

validates :amount, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
validates :client_id, numericality: { only_integer: true }, allow_nil: true

end

Direct Known Subclasses

AgentCodeModel

Constant Summary

Constants included from HidableColumns

HidableColumns::BASE_HIDDEN_COLUMNS

Method Summary

Methods included from HidableColumns

#agentcode_computed_attributes, #as_agentcode_json, #hidden_columns_for

Methods included from HasValidation

#validate_for_action