-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Support for virtual fields #2658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
f377679
a575bde
7266daa
07e927d
c6c32a4
bb4e196
cab6096
d1fd3a9
bf727b7
87c7e4c
ff843b8
9d43275
157485a
4ea760a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,10 @@ class CustomerDashboard < Administrate::BaseDashboard | |
| email_subscriber: Field::Boolean, | ||
| lifetime_value: Field::Number.with_options(prefix: "$", decimals: 2), | ||
| name: Field::String, | ||
| nickname: Field::String.with_options( | ||
| getter: ->(field) { "Mr. #{field.resource.name}man" if field.resource.name.present? }, | ||
| searchable: false | ||
| ), | ||
|
||
| orders: Field::HasMany.with_options(limit: 2, sort_by: :id), | ||
| log_entries: Field::HasManyVariant.with_options(limit: 2, sort_by: :id), | ||
| updated_at: Field::DateTime, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| require "rails_helper" | ||
| require "administrate/field/base" | ||
|
|
||
| module Administrate | ||
| module Field | ||
| class VirtualField < Field::Base | ||
|
||
| def self.searchable? | ||
| false | ||
| end | ||
|
|
||
| def read_value(data = nil) | ||
| resource.inspect | ||
| end | ||
|
|
||
| def foobar | ||
| "This is a Foobar: #{data}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe Administrate::Field::VirtualField do | ||
| describe "#foobar" do | ||
| it "displays the foobar" do | ||
| resource = double("Model", inspect: "Inspecting") | ||
|
|
||
| field = Administrate::Field::VirtualField.new( | ||
| :virtual_field, | ||
| nil, | ||
| :show, | ||
| resource: resource | ||
| ) | ||
|
|
||
| expect(field.foobar).to eq("This is a Foobar: Inspecting") | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, the example I mentioned with
Foobardoesn't work:public_sendfails because the method doesn't exist in the resource. Note that the safe navigation operator (&.) doesn't help here because it's useful only when the resource is nil.However, I think it can be fixed with a
respond_to?instead, as below:This should also fix the linter error asking for
elsifto be used.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the oversight. I’ve made the fixes and checked with standardrb.
I reviewed the your reference code and felt that
trywould be sufficient, so I rewrote it usingtry. Do you have any concerns about this approach?