-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't show unpersisted
has_one
associations
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.
- Loading branch information
1 parent
4a261c1
commit dab4454
Showing
7 changed files
with
67 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,10 @@ def nested_show | |
) | ||
end | ||
|
||
def linkable? | ||
data.try(:persisted?) | ||
end | ||
|
||
private | ||
|
||
def resolver | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters