-
-
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
Display model's class and id when to_s not defined #70
Conversation
@@ -52,7 +52,7 @@ | |||
|
|||
def find_option(associated_model, field_id) | |||
field = find("#order_" + field_id) | |||
field.find("option", text: associated_model.to_s) | |||
field.find("option", text: "#{associated_model.class.to_s.titleize} ##{associated_model.id}") |
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.
Line is too long. [99/80]
https://trello.com/c/9OfUvC3x Why? Administrate relied on `#to_s` to display objects throughout the system, but in order for it to work correctly developers needed to define #to_s on each model that they wanted to use with Administrate. No more. Now, if a developer doesn't define a custom #to_s on a model, Administrate will detect that and default to showing the object's class and database id. TODO: refactor... especially HasMany and BelongsTo relationships.
This could also be resolved by monkey patching module ActiveRecord
class Base
def to_s
"#{self.class.to_s.titleize} ##{id}"
end
end
end Thoughts on which method is better? |
If |
We might be able to add a method to require "administrate/base_dashboard"
class CustomerDashboard < Administrate::BaseDashboard
ATTRIBUTE_TYPES = {
created_at: Field::String,
# ...
updated_at: Field::String,
}
TABLE_ATTRIBUTES = ATTRIBUTE_TYPES.keys
SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys - [:name]
FORM_ATTRIBUTES = [:name, :email]
def display_resource(resource)
"#{resouce.class.to_s.titleize} ##{resource.id}"
end
end Thoughts? |
That method is asking, not telling. The main issue with that is that it requires the objects to expose their instance variables. I'd be very comfortable seeing this in various bits of code: def display_resource
to_s
end |
If we used a method like I think the solution I'm most comfortable with at this point is to tell developers something like:
...then we'd need to release the |
Oh I didn't understand: you want to use AR's |
if __getobj__.method(:to_s).owner == Kernel | ||
"#{__getobj__.class.to_s.titleize} ##{__getobj__.id}" | ||
else | ||
__getobj__.to_s |
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.
Thoughts on extracting __getobj__
to a private method? Maybe decorated_resource
or similar?
@mike-burns if I understand your question, then yep! When we need to display Here's a screenshot that shows how we're using The decision to use |
https://trello.com/c/9OfUvC3x
Why?
Administrate relied on
#to_s
to display objects throughout the system,but in order for it to work correctly developers needed to define #to_s
on each model that they wanted to use with Administrate.
No more.
Now, if a developer doesn't define a custom #to_s on a model,
Administrate will detect that and default to showing the object's class
and database id.