Class: Glib::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model
Defined in:
app/models/glib/model.rb

Overview

Base class for table-less FORM-BACKED domain objects — the ActiveModel counterpart of Glib::ApplicationRecord. Apps inherit through a thin ApplicationModel < Glib::Model root (parallel to ApplicationRecord), so app code always reads < ApplicationModel.

Choosing a base for a class under app/models (the three-way rule; see dev-doc best_practices/backend/en/03_model.md item 9):

ApplicationRecord — the class is persisted (it has a table).
ApplicationModel  — table-less, but it BACKS A FORM/REQUEST: it is built
                  from params (`assign_attributes(create_params)`),
                  validates one user transaction, reports errors back
                  to the dialog, and pairs by name with a controller
                  and policy. (This class.)
PlainModel        — neither: plain domain logic with no framework
                  machinery — catalogs, derivations, tallies,
                  write-orchestration helpers (see Glib::PlainModel).

If you are reaching for a bare include ActiveModel::Model, you want ApplicationModel instead — the shared mechanics live here.

Class Method Summary collapse

Class Method Details

.attr_id_list(*attr_names) ⇒ Object

Declares multi-select id-list attributes: a reader plus a normalizing writer that drops the blank entries browsers submit for empty multi-selects and coerces the surviving ids to integers.

attr_id_list :member_ids, :assignee_group_ids


30
31
32
33
34
35
36
37
38
# File 'app/models/glib/model.rb', line 30

def self.attr_id_list(*attr_names)
  attr_names.each do |attr_name|
    attr_reader attr_name

    define_method("#{attr_name}=") do |ids|
      instance_variable_set("@#{attr_name}", Array(ids).map(&:presence).compact.map(&:to_i))
    end
  end
end