-
-
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
Don't show unpersisted has_one
associations
#1794
Don't show unpersisted has_one
associations
#1794
Conversation
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.
Thank you for your PR! On one hand, I agree that we shouldn't be displaying links to unpersisted resources. On the other hand, I wonder if the problem here is not so much with Administrate as with the underlying model, forcing an unpersisted association and confusing the code that enters in contact with it.
I'd like a second opinion on this one. Perhaps @nickcharlton or @c4lliope have thoughts?
75c6139
to
71ad3c4
Compare
@pablobm I understand your point. From what I saw until now in rails projects is quite common to find has_one relationships described in the model like in my example. However, that doesn't mean it's the best approach and I'm also not sure if this is enough reason to merge the PR (I'll keep doing these rewritings in my projects, though). In any case, I updated the commit with the test names you suggested to keep. |
@rwehresmann - Can you also solve Hound's lint warnings, please? |
Can we add a check like... <%= @product.product_meta_tag.persisted? ? link_to(@product.product_meta_tag) : @product.product_meta_tag.name %> So the link is only rendered for persisted models, and otherwise we only display plain text on the page? Nice catch here, this seems like a use case that I wouldn't want to break peoples' applications over. |
688897e
to
3d11211
Compare
@pablobm PR updated with the corrections. @c4lliope I'm thinking if it's worthy. We actually don't have how to control the name of the model fields (we don't have the warranty that there is a |
Could you access administrate/spec/dashboards/customer_dashboard_spec.rb Lines 70 to 79 in b85abcf
Or, i believe, there is |
Ah, here; administrate/lib/administrate/field/associative.rb Lines 6 to 8 in 844c270
|
@c4lliope Oh, I see what you mean. I checked and
The output for the situation we're treating in this PR would be I could change the |
Ooh, good digging.
Hm. Hmmmm.
Changing `display_resource` wouldn't be a *breaking* change, but it would
be a visible change on peoples' dashboards.
I'm curious what our options would be for making that change; I chose
`"#{resource.class} ##{resource.id}"` as a sensible default for persisted
records; I'm not sure a similar sensible default is possible for
unpersisted.
Unless, maybe;
```ruby
def display_resource(resource)
resource.persisted?
? "#{resource.class} ##{resource.id}"
: "Unpersisted #{resource.class}"
end
```
|
Sorry, I guess email breaks comment formatting. |
dab4454
to
d74aa43
Compare
@c4lliope I still think that from a user perspective display nothing at all is the best option. It's possible that they don't even understand what is "persistence" in the context of the application. We could use "unrecord" for instance, but it seems redundant to me. However, if we should display something, I think your suggestion is the way to go. I updated the PR with the described solution. Just to record: I'm calling always |
Go ahead and display a blank, I think that is a fine solution also.
…On Sun, Nov 1, 2020, 9:09 PM Rodrigo Walter Ehresmann < ***@***.***> wrote:
@c4lliope <https://github.com/c4lliope> I still think that from a user
perspective display nothing at all is the best option. It's possible that
they don't even understand what is "persistence" in the context of the
application. We could use "unrecord" for instance, but it seems redundant
to me. However, if we should display something, I think your suggestion is
the way to go.
I updated the PR with the described solution.
Just to record: I'm calling always try(:persisted?). I never used another
ORM then ActiveRecord, and I'm not sure if Administrate works just fine
with another. In any case, if the ORM doesn't implement persisted? these
alterations will not break the application, we'll just display text
instead. If that becomes a problem, it'll be easy to change it to make it
work with another ORM.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1794 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHILHKTGWWWCKWFW6EFBASDSNYIEFANCNFSM4SNVJTLQ>
.
|
We don't need to worry about other ORMs, thankfully, so |
Showing unpersisted `has_one` associations can be misleading and break the application is some cases. Let's consider the following example: ``` class Product < ApplicationRecord has_one :product_meta_tag def product_meta_tag super || build_product_meta_tag end end ``` That opens for a scenario where a `product.product_meta_tag` will return a non-persisted instance of a ProductMetaTag. In the `_show` partial we're just checking if `field.data` (i.e., ProductMetaTag instance) exists to display it, and a non-persisted instance would be displayed. The link displayed in the partial (`link_to( field.display_associated_resource, [namespace, field.data]`) would be pointing to the index page of product meta tags, what is not desired. Also, in the absence of an index route, the page would break. The solution: just check if data is persisted to display it or not.
d74aa43
to
90ebf9a
Compare
Sorry for the delay on this. @c4lliope done: display a link if persisted, or blank if not. @nickcharlton great. Thanks for the tips! |
Co-authored-by: Nick Charlton <nick@nickcharlton.net>
Showing unpersisted
has_one
associations can be misleading and break the application is some cases.Let's consider the following example:
That opens for a scenario where a
product.product_meta_tag
will return a non-persisted instance of a ProductMetaTag. In the_show
partial we're just checking iffield.data
(i.e., ProductMetaTag instance) exists to display it, and a non-persisted instance would be displayed. The link displayed in the partial (link_to( field.display_associated_resource, [namespace, field.data]
) would be pointing to the index page of product meta tags, what is not desired. Also, in the absence of an index route, the page would break.The solution: just check if data is persisted to display it or not.