Skip to content
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

Move breadcrumbs setting from views to controllers #3191

Closed

Conversation

cavalle
Copy link

@cavalle cavalle commented Apr 22, 2019

Description

This PR moves the definition of all the breadcrumbs in the admin module from the views to a before_action method in the controllers. This fixes #3149 and other similar bugs, and will avoid reintroducing them in the future.

Since this change affects all the breadcrumbs in the admin engine, the PR is quite large and so I've split it in several cohesive commits. The description of each commit elaborates on the changes implemented in it. I suggest reviewing this PR commit by commit.

These are the key decisions I made:

  1. This PR implements the general approach suggested by @kennyadsl in Fix issue with user breadcrumbs #3152. That is, by setting the breadcrumbs in a before_action of the controller, we ensure the logic of the actions haven't changed yet any objects used in the breadcrumb, thus avoiding bugs like Breadcrumbs displays invalid emails modifying users  #3149.

  2. I took a look at https://github.com/weppos/breadcrumbs_on_rails and (the seemingly better maintained) https://github.com/piotrmurach/loaf for inspiration, but finally decided to go for a simpler solution based on the existing helpers.

  3. After these changes, every controller in the admin module (that renders breadcrumbs) includes a set_breadcrumbs method that is called for all the actions of the controller. Since different actions usually have different breadcrumbs, after the common breadcrumbs are set, this method usually uses a few conditionals to set the action-specific breadcrumbs

    An alternative solution would be to set the common breadcrumbs in a common method while any action-specific breadcrumb at the top of each action method. I chose to put everything in the same method because I like better to have everything (both shared and action-specific breadcrumbs) in the same place, rather than scattered around the controller. However, I don't have a very strong opinion about this choice, I understand the merits of the aforementioned alternative.

You can find more details about the changes in each commit's description.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

@@ -0,0 +1 @@
<%= text %> (<%= t(state, scope: 'spree.return_authorization_states')%>)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a trailing newline at the end of the file.
Use 1 space before %> instead of 0 space.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! I only checked with Rubocop locally. I'll fix these!

<%= link_to text, path %>
<% else %>
<%= text %>
<% end %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a trailing newline at the end of the file.

<%= text %>
<span class="pill pill-<%= state %>">
<%= t(state, scope: 'spree.payment_states') %>
</span>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a trailing newline at the end of the file.

@cavalle cavalle force-pushed the 3149-move-breadcrumbs-to-controllers branch from e26459d to 336fc57 Compare April 23, 2019 08:55
Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

@cavalle cavalle force-pushed the 3149-move-breadcrumbs-to-controllers branch from 336fc57 to 26d41c6 Compare April 23, 2019 09:07
Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the accurate research and work on this @cavalle, I think this is the right approach!

Even if I'm not sure about your same doubt on if it's better to put specific breadcrumbs in each controller action or in a single method with some ifs, I'd probably go for the former, probably I'm usually happy when I see ifs disappear. Let see what others think about this, in the meantime, I left some comments, let me know your thoughts!

add_breadcrumb t('spree.settings')
add_breadcrumb t('spree.admin.tab.checkout')
add_breadcrumb plural_resource_name(Spree::AdjustmentReason), spree.admin_adjustment_reasons_path
add_breadcrumb @adjustment_reason.name if params[:id].present?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are avoiding spaces to align code on different lines, because if one of the other lines changes we have to update all the other lines, degrading git history.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for letting me know about this convention. I wasn't aware of it. I'll update everywhere I'm using spaces to align code.

backend/app/controllers/spree/admin/base_controller.rb Outdated Show resolved Hide resolved
backend/spec/features/admin/users_spec.rb Outdated Show resolved Hide resolved
backend/spec/helpers/admin/navigation_helper_spec.rb Outdated Show resolved Hide resolved
core/spec/lib/spree/core/controller_helpers/common_spec.rb Outdated Show resolved Hide resolved
@@ -0,0 +1 @@
<%= text %> (<%= t(state, scope: 'spree.return_authorization_states') %>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we could take advantage of this PR and remove this custom breadcrumb in some way?

Copy link
Author

@cavalle cavalle Apr 26, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting one. This particular breadcrumb could be perfectly specified from the controller itself using simple string interpolation. In fact, I did it like that in an initial version. However, the string interpolated ended up being a bit too long and a bit too complex and, since I had to implement the ability to use custom partials for another breadcrumb, I thought it would make sense to use it in this case for clarity.

Now, one thing I wondered is whether this additional level of complexity (i.e. the ability to specify custom breadcrumbs with partials) was really justified if we only needed for one or two breadcrumbs. Everything would be simpler if all the breadcrumbs would be just text, and didn't need special HTML formatting like in this particular case in the whole app. I'd appreciate your thoughts on this. Is it really needed and valuable to show the status information with colours in the breadcrumbs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the HTML from breadcrumbs, even if it probably requires a new PR and an alternative UI method to show the state of the parent object in the page, which I think was the original reason why they have been introduced.

@cavalle
Copy link
Author

cavalle commented Apr 26, 2019

@kennyadsl thanks for your very clear feedback! I'll respond and work on it during this morning.

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

@cavalle
Copy link
Author

cavalle commented Apr 26, 2019

Just pushed additional changes to address the points discussed above. I didn't rebase or amend the previous commits, but if you usually work that way let me know and I'll do it.

@kennyadsl
Copy link
Member

Thanks @cavalle, I'll talk in the next core team meeting about:

  • better to use a single method or define the part of the breadcrumb into each specific action?
  • can we remove the status from the breadcrumb?

I hope to come back soon with a response.

Copy link
Member

@gmacdougall gmacdougall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks very much for this contribution. It's looking very good so far.

@kennyadsl
Copy link
Member

@cavalle we had a quick chat about this PR in the last Core Team meeting and this is what we decided:

better to use a single method or define the part of the breadcrumb into each specific action?

We are not sure but the current implementation is a great starting point that we can revisit later if needed. We also discussed the possibility to create a "DSL" around this, that allows setting specific features only for specific actions, maybe something like:

add_breadcrumb @adjustment_reason.name, only: [:show, :edit, :update]

That would actually take care of pushing stuff in the right action but for now, this is just an idea, what we have is great.


can we remove the status from the breadcrumb?

Yes, we can. We prefer to have this PR simplified than having a pill component into breadcrumbs. It could be nice to have but I think we should live without it or even better, move the status pill into another ad-hoc UI component in the page, which we can probably discuss into a separate issue.


So missing TODO items are:

  • remove status pills from breadcrumbs
  • remove complex templates that did come from pills usage
  • rebase against master squashing last commits to have a clean git history

Thanks a lot again for this great contribution!

@cavalle
Copy link
Author

cavalle commented May 9, 2019

Awesome! I'll take care of the TODO items as soon as I can.

@cavalle cavalle force-pushed the 3149-move-breadcrumbs-to-controllers branch from 8cf7ee8 to 02a3abe Compare May 16, 2019 07:25
Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

@cavalle
Copy link
Author

cavalle commented May 20, 2019

I implemented all points in the previous comment and CI passed. This one is ready for review again!

@kennyadsl
Copy link
Member

@cavalle Hey Luismi, thanks! I just noticed another thing that we should take care of before merging:

if we remove the admin_breadcrumb helper method at all we will break existing stores and extensions that could be using that method when creating new pages in the admin. We should instead keep it working and print a deprecation message so they can gradually transition to the new way of setting breadcrumbs. If you don't have time, don't worry, just let me know and I can take care of it in another PR asking for your review when done.

Thanks again 🙏

@cavalle
Copy link
Author

cavalle commented May 27, 2019

@kennyadsl right! that makes a lot of sense, and I'd love to take care of it, but I'm afraid I no longer have much available time. So, if you don't mind taking care of it in another PR, I'd very much appreciate it!

@kennyadsl
Copy link
Member

Sure @cavalle, I'm thinking I could also open a PR on your fork to address the issue so my changes will be part of this one. Does it work for you?

@cavalle
Copy link
Author

cavalle commented May 27, 2019

No problem with that! I made you collaborator of my fork.

cavalle added 4 commits May 28, 2019 14:13
This commit makes the `add_breadcrumb` method available in all
controllers to allow to define breadcrumbs from there rather than from
views. It also moves the definition of the breadcrumbs for all the
users' pages from the views to the controller.

Setting the breadcrumbs in the `before_action` of the controller,
ensures objects haven't been changed by the logic of the action yet,
thus avoiding bugs like solidusio#3149.

This commit includes a regression test for the aforementioned bug, and
tests for the new breadcrumbs rendering logic.
This commit moves the setting of all the breadcrumbs that don't require
custom formatting from the views to the controllers, using the new
`add_breadcrumb` method. This will avoid bugs like the one reported
in solidusio#3149.
This commit removes the pills from those breadcrumbs that were
using them. This way there's no need for a custom format and the
breadcrumbs can be set from the controllers using the existing
helpers.
This commit deletes a template that is not reachable. I found no code
referencing to it and no test broke after removing the template.
@kennyadsl kennyadsl force-pushed the 3149-move-breadcrumbs-to-controllers branch from 02a3abe to b706e07 Compare May 28, 2019 12:17
Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

@kennyadsl kennyadsl force-pushed the 3149-move-breadcrumbs-to-controllers branch from b706e07 to 8878a86 Compare May 28, 2019 12:19
Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

cavalle added 3 commits May 28, 2019 14:35
Once we have migrated all the breadcrumbs setting logic to controllers,
we no longer need the ability to set breadcrumbs from views. Not being
able to set breadcrumbs from views will avoid reintroducing bugs like
the one reported in solidusio#3149.

This commit deprecates using the admin_breadcrumb method but still
lets its usage available, so extensions and stores can gradually migrate
to the new way of setting breadcrumbs.

To avoid breadcrumbs duplication if stores has replaced backend admin
views, a check has been added to avoid adding the view breadcrumbs in
case a breadcrumb with the same name is already there.

strip_tags into admin_page_title is still required since deprecated
breadcrumbs coming from that method will probably be html links.
Once we remove the method we can probably also avoid stripping tags
to set admin page title.
@kennyadsl kennyadsl force-pushed the 3149-move-breadcrumbs-to-controllers branch from 8878a86 to a4df140 Compare May 28, 2019 12:35
Copy link

@houndci-bot houndci-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some files could not be reviewed due to errors:

warning: parser/current is loading parser/ruby26, which recognizes
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.1.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
1 error(s) were found in ERB files
Linting 1 files with 12 linters...

Avoid using 'javascript_tag do' as it confuses tests that validate html, use inline <script> instead
In file: backend/app/views/spree/admin/adjustments/index.html.erb:19

@kennyadsl
Copy link
Member

kennyadsl commented May 28, 2019

@cavalle I think I've done, the only commit I changed is 64230bf. I've also changed the commit message and description to reflect/explain the changes I've made. Please, let me know your thoughts if you have time.


Notes to add the CHANGELOG:

Setting breadcrumbs from views has been deprecated and now should do that from the controller.
For example, if you are setting a breadcrumb in your spree/admin/custom_models/index.html.erb view like this:

<% admin_breadcrumb(link_to plural_resource_name(Spree::CustomModel), spree.admin_custom_models_path) %>

You should remove it and add the following into your app/controllers/spree/custom_models_controller.rb controller:

before_action :set_breadcrumbs, only: :index

# ...

private

# ...

def set_breadcrumbs
  add_breadcrumb plural_resource_name(Spree::CustomModel), spree.admin_custom_models_path
end

@cavalle
Copy link
Author

cavalle commented May 28, 2019

@kennyadsl the changes you made look good to me 👍

Copy link
Contributor

@ericsaupe ericsaupe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were the notes for the CHANGELOG going to be added in this PR as well?

Overall, I like it and it cleans things up nicely.

@kennyadsl
Copy link
Member

@ericsaupe I'll take care of adding this notes to the CHANGELOG when we'll release Solidus 2.9

Copy link
Member

@tvdeyen tvdeyen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all thank you very much for the contribution and all the hard work. 🎈

But imo the approach taken here is not what we should do. This is a lot of breaking code for fixing a very edgy problem. I see the advantages this approach might have, but also think that a controller module is not the right place for a UI (views only) feature. Unfortunately Rails does not give us view objects, so an alternative is also not very pleasing, I know.

The approach taken in #3152 to fix the very edgy "bug" in #3149 is a simple fix that I would like to see instead of this approach. But, if all other core team members agree that this apporach is what we want to go with I am not rejecting this change (simple democracy ^_^).

In that case I have some comments I would like see addressed.

Thanks again for you well done contribution.

module Spree
module Admin
module Breadcrumbs
def add_breadcrumb(name, path = nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this methods protected so they don't become actions?

module Admin
module Breadcrumbs
def add_breadcrumb(name, path = nil)
@admin_breadcrumbs ||= []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a Set, so that we do not get potential duplicates and gain some performance?

@@ -3,6 +3,8 @@
module Spree
module Admin
class BaseController < Spree::BaseController
include Spree::Admin::Breadcrumbs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add the before_action :set_breadcumbs here and add an empty method that gets called if the child class does not implement set_breadcrumbs? We might even have a "defaut breadcrumb" (build from the resource) for controllers. We could remove a lot of code then.

@@ -9,6 +9,7 @@ class CustomerReturnsController < ResourceController
before_action :parent # ensure order gets loaded to support our pseudo parent-child relationship
before_action :load_form_data, only: [:new, :edit]
before_action :build_return_items_from_params, only: [:create]
before_action :set_breadcrumbs
Copy link
Member

@tvdeyen tvdeyen May 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this before_action into the spree/admin/resource_controller


def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb t('spree.cart') if action_name == 'cart'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move the breadcrumb into the cart action instead of this if

def set_breadcrumbs
set_order_breadcrumbs
add_breadcrumb t('spree.cart') if action_name == 'cart'
add_breadcrumb t('spree.confirm_order') if action_name == 'confirm'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, why not add the breadcrumb only in the confirm action?


layout :get_layout

before_action :set_user_language
end

def plural_resource_name(resource_class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this method into the admin/resource_controller instead? This module gets included in many (even frontend) controllers.

@kennyadsl
Copy link
Member

@tvdeyen Thanks for the review. I think that the edge case reported in #3149 is actually present in all admin edit forms, where "the thing" (name, title, whatever) that is rendered in the breadcrumbs has some validation.

I see your point that this is a somehow drastic change and potentially could break something, even if I think the only issue will be with extensions The solution should use both approaches, until v2.8 goes EOL (if we merge it now), conditionally switching from a way of setting breadcrumbs to another depending on the version. Eg. in extensions:

<% if Solidus.version < '2.9' %>
  <% admin_breadcrumb(link_to plural_resource_name(Spree::CustomModel), spree.admin_custom_models_path) %>
<% end %>

and in the controller:

def set_breadcrumbs
  return if Solidus.version < '2.9'

  # ... rest of the code

which is not ideal at all.

TL;DR Despite I proposed this path, I'm not sure as well that this is the best solution. Maybe we should go with a third option? At RailsConf @joelhawksley presented this talk about adding a "component" layer into Rails, that is used at GitHub. Here's a demo app. IMO it is a fresh approach that we could consider, even because it will be probably be merged into Rails soon.

@waiting-for-dev waiting-for-dev added the type:enhancement Proposed or newly added feature label Aug 25, 2022
@kennyadsl
Copy link
Member

Closing, we'll keep this as reference for some future work. Thanks to anyone involved!

@kennyadsl kennyadsl closed this Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement Proposed or newly added feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Breadcrumbs displays invalid emails modifying users
7 participants