Class: IronAdmin::Adapters::Base Abstract
- Inherits:
-
Object
- Object
- IronAdmin::Adapters::Base
- Defined in:
- lib/iron_admin/adapters/base.rb
Overview
Subclass and implement all methods to create a custom adapter.
Abstract base class defining the interface every data source adapter must implement.
IronAdmin uses this interface to introspect schemas, build queries, perform CRUD operations, and search records. The default ActiveRecord adapter wraps Rails' ActiveRecord ORM. Future adapters (Mongoid, HTTP, etc.) implement this same interface.
Direct Known Subclasses
Defined Under Namespace
Classes: ColumnDescriptor
Constant Summary collapse
- TRUTHY_VALUES =
Casts a string value to a boolean. Default implementation for non-AR adapters. ActiveRecord overrides to use ActiveModel::Type::Boolean.
%w[true 1 yes].freeze
Instance Attribute Summary collapse
-
#model_class ⇒ Class
readonly
The underlying model/data class this adapter wraps.
Instance Method Summary collapse
-
#all ⇒ Object
Returns a base scope/collection of all records.
-
#association(_name) ⇒ #name, ...
Returns a single association descriptor by name.
-
#associations(_kind = nil) ⇒ Array<#name, #klass, #foreign_key, #polymorphic?>
Returns association descriptors.
-
#attachments ⇒ Hash
Returns ActiveStorage attachment descriptors.
-
#build(_attrs = {}) ⇒ Object
Builds a new unsaved record.
-
#cast_boolean(value) ⇒ Object
rubocop:disable Naming/PredicateMethod.
-
#column_names ⇒ Array<String>
Returns column name strings.
-
#columns ⇒ Array<#name, #type>
Returns column descriptors for the model.
-
#count(_scope = nil) ⇒ Integer
Returns record count for a scope.
-
#create_record(attrs) ⇒ Object
Builds and persists a record, returning the record with validation errors if any.
-
#destroy!(_record) ⇒ void
Destroys a record.
-
#distinct_values(_column) ⇒ Array
Returns distinct values for a column.
-
#enums ⇒ Hash{String => Hash}
Returns enum definitions.
-
#filter(_scope, _column, _value) ⇒ Object
Filters a scope by a column value.
-
#find(_id) ⇒ Object
Finds a record by primary key.
-
#find_by(_attrs) ⇒ Object?
Finds a record by attributes.
-
#find_by_keys(attrs) ⇒ Object?
Finds a record by a configured import key.
-
#find_each(_scope) {|record| ... } ⇒ void
Iterates records in memory-efficient batches.
-
#has_column?(_name) ⇒ Boolean
Checks if a column exists.
-
#human_name ⇒ String
Human-readable model name (e.g., "User").
-
#initialize(model_class) ⇒ Base
constructor
A new instance of Base.
-
#limit(_scope, _max) ⇒ Object
Limits a scope to N records.
-
#order_by(_scope, _column, _direction) ⇒ Object
Orders a scope by column and direction.
-
#pagy_method ⇒ Symbol
Returns the Pagy backend method name for this adapter.
-
#pluck(_scope, _column) ⇒ Array
Extracts a single column from a scope.
-
#polymorphic_inverse_classes(_polymorphic_name) ⇒ Array<Class>
Returns model/document classes that declare an inverse association for a polymorphic belongs_to name.
-
#preload(_scope, _association_names) ⇒ Object
Eager loads associations on a scope.
-
#primary_key ⇒ String+
Primary key column name(s) for the underlying model.
-
#query_builder_class ⇒ Class
Returns the query builder class for operator-based filters.
-
#record_changes(_record) ⇒ Hash
Returns the changes hash after a save/update.
-
#resource_name ⇒ String
URL-friendly plural resource name (e.g., "users").
-
#rich_text_attributes ⇒ Array<Symbol>
Returns ActionText rich text attribute names.
-
#save(_record) ⇒ Boolean
Persists a record.
-
#search_column(_scope, _column, _query) ⇒ Object
Searches a single column with LIKE/ILIKE.
-
#search_columns(_scope, _columns, _query) ⇒ Object
Searches multiple columns with OR logic.
-
#table_name ⇒ String?
Database table name (or nil for non-SQL backends).
-
#transaction { ... } ⇒ Object
Wraps a block in a transaction.
-
#unscope_column(_scope, _column) ⇒ Object
Removes a WHERE condition on a specific column from a scope.
-
#update(_record, _attrs) ⇒ Boolean
Updates a record's attributes.
-
#update_record(record, attrs) ⇒ Boolean
Updates a record and returns true when persistence succeeds.
-
#wrap_rollback { ... } ⇒ void
Executes a block, converting IronAdmin::Rollback to the adapter-native rollback.
Constructor Details
#initialize(model_class) ⇒ Base
Returns a new instance of Base.
17 18 19 |
# File 'lib/iron_admin/adapters/base.rb', line 17 def initialize(model_class) @model_class = model_class end |
Instance Attribute Details
#model_class ⇒ Class (readonly)
Returns The underlying model/data class this adapter wraps.
14 15 16 |
# File 'lib/iron_admin/adapters/base.rb', line 14 def model_class @model_class end |
Instance Method Details
#all ⇒ Object
Returns a base scope/collection of all records.
116 117 118 |
# File 'lib/iron_admin/adapters/base.rb', line 116 def all raise NotImplementedError end |
#association(_name) ⇒ #name, ...
Returns a single association descriptor by name.
58 59 60 |
# File 'lib/iron_admin/adapters/base.rb', line 58 def association(_name) raise NotImplementedError end |
#associations(_kind = nil) ⇒ Array<#name, #klass, #foreign_key, #polymorphic?>
Returns association descriptors.
51 52 53 |
# File 'lib/iron_admin/adapters/base.rb', line 51 def associations(_kind = nil) raise NotImplementedError end |
#attachments ⇒ Hash
Returns ActiveStorage attachment descriptors.
64 65 66 |
# File 'lib/iron_admin/adapters/base.rb', line 64 def raise NotImplementedError end |
#build(_attrs = {}) ⇒ Object
Builds a new unsaved record.
221 222 223 |
# File 'lib/iron_admin/adapters/base.rb', line 221 def build(_attrs = {}) raise NotImplementedError end |
#cast_boolean(value) ⇒ Object
rubocop:disable Naming/PredicateMethod
328 329 330 |
# File 'lib/iron_admin/adapters/base.rb', line 328 def cast_boolean(value) # rubocop:disable Naming/PredicateMethod TRUTHY_VALUES.include?(value.to_s.downcase) end |
#column_names ⇒ Array<String>
Returns column name strings.
31 32 33 |
# File 'lib/iron_admin/adapters/base.rb', line 31 def column_names raise NotImplementedError end |
#columns ⇒ Array<#name, #type>
Returns column descriptors for the model.
25 26 27 |
# File 'lib/iron_admin/adapters/base.rb', line 25 def columns raise NotImplementedError end |
#count(_scope = nil) ⇒ Integer
Returns record count for a scope.
192 193 194 |
# File 'lib/iron_admin/adapters/base.rb', line 192 def count(_scope = nil) raise NotImplementedError end |
#create_record(attrs) ⇒ Object
Builds and persists a record, returning the record with validation errors if any.
243 244 245 246 247 |
# File 'lib/iron_admin/adapters/base.rb', line 243 def create_record(attrs) record = build(attrs) save(record) record end |
#destroy!(_record) ⇒ void
This method returns an undefined value.
Destroys a record.
260 261 262 |
# File 'lib/iron_admin/adapters/base.rb', line 260 def destroy!(_record) raise NotImplementedError end |
#distinct_values(_column) ⇒ Array
Returns distinct values for a column.
178 179 180 |
# File 'lib/iron_admin/adapters/base.rb', line 178 def distinct_values(_column) raise NotImplementedError end |
#enums ⇒ Hash{String => Hash}
Returns enum definitions.
44 45 46 |
# File 'lib/iron_admin/adapters/base.rb', line 44 def enums raise NotImplementedError end |
#filter(_scope, _column, _value) ⇒ Object
Filters a scope by a column value.
146 147 148 |
# File 'lib/iron_admin/adapters/base.rb', line 146 def filter(_scope, _column, _value) raise NotImplementedError end |
#find(_id) ⇒ Object
Finds a record by primary key.
123 124 125 |
# File 'lib/iron_admin/adapters/base.rb', line 123 def find(_id) raise NotImplementedError end |
#find_by(_attrs) ⇒ Object?
Finds a record by attributes.
130 131 132 |
# File 'lib/iron_admin/adapters/base.rb', line 130 def find_by(_attrs) raise NotImplementedError end |
#find_by_keys(attrs) ⇒ Object?
Finds a record by a configured import key.
137 138 139 |
# File 'lib/iron_admin/adapters/base.rb', line 137 def find_by_keys(attrs) find_by(attrs) end |
#find_each(_scope) {|record| ... } ⇒ void
This method returns an undefined value.
Iterates records in memory-efficient batches.
289 290 291 |
# File 'lib/iron_admin/adapters/base.rb', line 289 def find_each(_scope, &) raise NotImplementedError end |
#has_column?(_name) ⇒ Boolean
Checks if a column exists.
38 39 40 |
# File 'lib/iron_admin/adapters/base.rb', line 38 def has_column?(_name) # rubocop:disable Naming/PredicatePrefix raise NotImplementedError end |
#human_name ⇒ String
Human-readable model name (e.g., "User"). Uses ActiveModel::Naming (available on both AR and Mongoid models).
94 95 96 |
# File 'lib/iron_admin/adapters/base.rb', line 94 def human_name model_class.model_name.human end |
#limit(_scope, _max) ⇒ Object
Limits a scope to N records.
163 164 165 |
# File 'lib/iron_admin/adapters/base.rb', line 163 def limit(_scope, _max) raise NotImplementedError end |
#order_by(_scope, _column, _direction) ⇒ Object
Orders a scope by column and direction.
155 156 157 |
# File 'lib/iron_admin/adapters/base.rb', line 155 def order_by(_scope, _column, _direction) raise NotImplementedError end |
#pagy_method ⇒ Symbol
Returns the Pagy backend method name for this adapter.
317 318 319 |
# File 'lib/iron_admin/adapters/base.rb', line 317 def pagy_method raise NotImplementedError end |
#pluck(_scope, _column) ⇒ Array
Extracts a single column from a scope.
186 187 188 |
# File 'lib/iron_admin/adapters/base.rb', line 186 def pluck(_scope, _column) raise NotImplementedError end |
#polymorphic_inverse_classes(_polymorphic_name) ⇒ Array<Class>
Returns model/document classes that declare an inverse association for a polymorphic belongs_to name.
78 79 80 |
# File 'lib/iron_admin/adapters/base.rb', line 78 def polymorphic_inverse_classes(_polymorphic_name) [] end |
#preload(_scope, _association_names) ⇒ Object
Eager loads associations on a scope.
171 172 173 |
# File 'lib/iron_admin/adapters/base.rb', line 171 def preload(_scope, _association_names) raise NotImplementedError end |
#primary_key ⇒ String+
Primary key column name(s) for the underlying model.
Returns a String for single-column keys (e.g. "id", "slug") and an
Array["account_id", "scope_id"]).
108 109 110 |
# File 'lib/iron_admin/adapters/base.rb', line 108 def primary_key "id" end |
#query_builder_class ⇒ Class
Returns the query builder class for operator-based filters.
311 312 313 |
# File 'lib/iron_admin/adapters/base.rb', line 311 def query_builder_class raise NotImplementedError end |
#record_changes(_record) ⇒ Hash
Returns the changes hash after a save/update.
298 299 300 |
# File 'lib/iron_admin/adapters/base.rb', line 298 def record_changes(_record) raise NotImplementedError end |
#resource_name ⇒ String
URL-friendly plural resource name (e.g., "users"). Uses ActiveModel::Naming (available on both AR and Mongoid models).
87 88 89 |
# File 'lib/iron_admin/adapters/base.rb', line 87 def resource_name model_class.model_name.plural end |
#rich_text_attributes ⇒ Array<Symbol>
Returns ActionText rich text attribute names.
70 71 72 |
# File 'lib/iron_admin/adapters/base.rb', line 70 def rich_text_attributes raise NotImplementedError end |
#save(_record) ⇒ Boolean
Persists a record.
228 229 230 |
# File 'lib/iron_admin/adapters/base.rb', line 228 def save(_record) raise NotImplementedError end |
#search_column(_scope, _column, _query) ⇒ Object
Searches a single column with LIKE/ILIKE.
203 204 205 |
# File 'lib/iron_admin/adapters/base.rb', line 203 def search_column(_scope, _column, _query) raise NotImplementedError end |
#search_columns(_scope, _columns, _query) ⇒ Object
Searches multiple columns with OR logic.
212 213 214 |
# File 'lib/iron_admin/adapters/base.rb', line 212 def search_columns(_scope, _columns, _query) raise NotImplementedError end |
#table_name ⇒ String?
Database table name (or nil for non-SQL backends).
100 101 102 |
# File 'lib/iron_admin/adapters/base.rb', line 100 def table_name raise NotImplementedError end |
#transaction { ... } ⇒ Object
Wraps a block in a transaction.
269 270 271 |
# File 'lib/iron_admin/adapters/base.rb', line 269 def transaction(&) raise NotImplementedError end |
#unscope_column(_scope, _column) ⇒ Object
Removes a WHERE condition on a specific column from a scope.
279 280 281 |
# File 'lib/iron_admin/adapters/base.rb', line 279 def unscope_column(_scope, _column) raise NotImplementedError end |
#update(_record, _attrs) ⇒ Boolean
Updates a record's attributes.
236 237 238 |
# File 'lib/iron_admin/adapters/base.rb', line 236 def update(_record, _attrs) raise NotImplementedError end |
#update_record(record, attrs) ⇒ Boolean
Updates a record and returns true when persistence succeeds.
253 254 255 |
# File 'lib/iron_admin/adapters/base.rb', line 253 def update_record(record, attrs) update(record, attrs) end |
#wrap_rollback { ... } ⇒ void
This method returns an undefined value.
Executes a block, converting IronAdmin::Rollback to the adapter-native rollback.
305 306 307 |
# File 'lib/iron_admin/adapters/base.rb', line 305 def wrap_rollback(&) raise NotImplementedError end |