Class: RuboCop::Cop::DevDoc::Rails::ApplicationModelBase

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/dev_doc/rails/application_model_base.rb

Overview

Inherit the app's ApplicationModel root instead of including ActiveModel::Model directly in a class under app/models.

Rationale

Table-less form-backed domain objects follow the three-way rule (backend/03_model.md item 9): persisted classes inherit ApplicationRecord, form-backed POROs inherit ApplicationModel (a thin app root over Glib::Model, which carries the shared mechanics such as attr_id_list), and plain domain logic inherits PlainModel. A direct include ActiveModel::Model bypasses the shared root: the class silently misses the shared mechanics, and the author never makes the which-kind-is-this decision the rule exists to force. This is the ActiveModel analog of Rails/ApplicationRecord.

This cop gives editor-time feedback on the single most common direct mistake; totality (every model class descends from one of the three bases, through any intermediate family base) is enforced at test time by DevDoc::Test::Lints::DomainClassBase, since resolving indirect ancestry is beyond per-file static analysis.

Disabled by default: enable it in projects whose ApplicationModel root exists (it requires glib-web's Glib::Model).

Examples:

# bad
class BulkOperation
  include ActiveModel::Model
end

# good
class BulkOperation < ApplicationModel
end

Constant Summary collapse

MSG =
'Inherit `ApplicationModel` instead of including `ActiveModel::Model` directly — ' \
'the shared root carries the form-object mechanics (backend/03_model.md item 9).'.freeze
RESTRICT_ON_SEND =
%i[include].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



47
48
49
50
51
# File 'lib/rubocop/cop/dev_doc/rails/application_model_base.rb', line 47

def on_send(node)
  return unless active_model_include?(node)

  add_offense(node)
end