You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/customizing_dashboards.md
+130Lines changed: 130 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -436,3 +436,133 @@ en:
436
436
```
437
437
438
438
If not defined (see `SHOW_PAGE_ATTRIBUTES` above), Administrate will default to the given strings.
439
+
440
+
## Virtual Attributes
441
+
442
+
For all field types, you can use the `getter` option to change where the data is retrieved from or to set the data directly.
443
+
444
+
By using this, you can define an attribute in `ATTRIBUTE_TYPES` that doesn’t exist in the model, and use it for various purposes.
445
+
446
+
### Attribute Aliases
447
+
448
+
You can create an alias for an attribute. For example:
449
+
450
+
```ruby
451
+
ATTRIBUTE_TYPES = {
452
+
shipped_at: Field::DateTime,
453
+
shipped_on: Field::Date.with_options(
454
+
getter: :shipped_at
455
+
)
456
+
}
457
+
COLLECTION_ATTRIBUTES = [
458
+
:shipped_on
459
+
}
460
+
SHOW_PAGE_ATTRIBUTES = [
461
+
:shipped_at
462
+
}
463
+
```
464
+
465
+
In this example, a virtual attribute `shipped_on` based on the value of `shipped_at` is defined as a `Date` type and used for display on the index page (this can help save table cell space).
466
+
467
+
### Decorated Attributes
468
+
469
+
You can also use this to decorate data. For example:
470
+
471
+
```ruby
472
+
ATTRIBUTE_TYPES = {
473
+
price: Field::Number,
474
+
price_including_tax: Field::Number.with_options(
475
+
getter: -> (field) {
476
+
field.resource.price * 1.1 if field.resource.price.present?
477
+
}
478
+
)
479
+
}
480
+
```
481
+
482
+
### Composite Attributes
483
+
484
+
You can dynamically create a virtual attribute by combining multiple attributes for display. For example:
485
+
486
+
```ruby
487
+
ATTRIBUTE_TYPES = {
488
+
first_name: Field::String,
489
+
last_name: Field::String,
490
+
full_name: Field::String.with_options(
491
+
getter: -> (field) {
492
+
[
493
+
field.resource.first_name,
494
+
field.resource.last_name
495
+
].compact_blank.join(' ')
496
+
}
497
+
)
498
+
}
499
+
```
500
+
501
+
## Virtual Fields
502
+
503
+
Custom fields can also be combined with virtual attributes.
504
+
505
+
```ruby
506
+
ATTRIBUTE_TYPES = {
507
+
id: Field::Number,
508
+
receipt: Field::ReceiptLink
509
+
}
510
+
```
511
+
512
+
```ruby
513
+
module Administrate
514
+
module Field
515
+
class ReceiptLink < Base
516
+
def data
517
+
resource.id
518
+
end
519
+
520
+
def filename
521
+
"receipt-#{data}.pdf"
522
+
end
523
+
524
+
def url
525
+
"/files/receipts/#{filename}"
526
+
end
527
+
end
528
+
end
529
+
end
530
+
```
531
+
532
+
```erb
533
+
<%= link_to field.filename, field.url %>
534
+
```
535
+
536
+
### Custom Actions via Virtual Field
537
+
538
+
By creating custom fields that are not dependent on specific attributes, you can insert custom views into any screen.
539
+
For example, you can add custom buttons like this:
0 commit comments