-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support for virtual fields #2658
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "rails_helper" | ||
require "administrate/field/base" | ||
|
||
module Administrate | ||
module Field | ||
class VirtualField < Field::Base | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if we can find an example in the dashboard where we can define this type of virtual field without having to drop it here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What else is needed here? Documentation, maybe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Answering on the main thread 🙂 |
||
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.
Thinking of an example that doesn't feel too artificial: how about a
full_address
field forOrder
? Something like this:Then its
SHOW_PAGE_ATTRIBUTES
can be something like this: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.
Thanks. I added.
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.
Looks like
searchable: false
is excess. If getter has been provided it makes field as unsearchable.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.
Have you thought about using decorators(like draper) for virtual fields?
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.
Sorry, should have been more clear: remove this example (which I find too artificial) and we can leave the one for the orders, which I think is better 🙂
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.
Good point on
searchable
. Perhaps it should befalse
by default ifgetter
is supplied? Otherwise it breaks the search.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.
@Nitr - It's a bit tricky due to how Administrate works internally. Also we tend to avoid adding dependencies like draper.
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.
@Nitr @pablobm
Thank you for the advice. I have made the changes. cab6096
I'm not sure if we should always set
searchable
tofalse
when agetter
is present, but since I can't think of any use cases at the moment, this seems appropriate.