Skip to content

Commit d1fd3a9

Browse files
committed
Add docs about virtual fields
1 parent cab6096 commit d1fd3a9

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

docs/customizing_dashboards.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,3 +436,133 @@ en:
436436
```
437437
438438
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:
540+
541+
```ruby
542+
ATTRIBUTE_TYPES = {
543+
id: Field::Number,
544+
custom_index_actions: Field::CustomActionButtons,
545+
custom_show_actions: Field::CustomActionButtons,
546+
}
547+
```
548+
549+
```ruby
550+
module Administrate
551+
module Field
552+
class CustomActionButtons < Base
553+
def data
554+
resource.id
555+
end
556+
end
557+
end
558+
end
559+
```
560+
561+
```erb
562+
<%# app/views/fields/custom_action_buttons/_index.html.erb %>
563+
<% if field.data.present? %>
564+
<%= button_to "some action 1", [:some_action_1, namespace, field.resource] %>
565+
<%= button_to "some action 2", [:some_action_2, namespace, field.resource] %>
566+
<%= button_to "some action 3", [:some_action_3, namespace, field.resource] %>
567+
<% end %>
568+
```

0 commit comments

Comments
 (0)