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

Allow overriding the routes proxy in the ResourceController #5219

Conversation

mamhoff
Copy link
Contributor

@mamhoff mamhoff commented Jul 4, 2023

Summary

Within Solidus, we always want to use the spree routes proxy as that is where all routes are defined. However, many people use the Resource controller as a parent controller in their main app, and because of this they need to override all the URL generation methods in the Resource Controller.

This allows one to specify the routes proxy in a custom controller as follows:

module MyApp
  class WidgetsController < Spree::Admin::ResourceController
  private

  def routes_proxy
    main_app
  end
end

Engine Developers can use this for engines, as well:

module MyEngine
  class WidgetsController < Spree::Admin::ResourceController
  private

  def routes_proxy
    my_engine
  end
end

Checklist

Check out our PR guidelines for more details.

The following are mandatory for all PRs:

The following are not always needed:

  • 📖 I have updated the README to account for my changes.
  • 📑 I have documented new code with YARD.
  • 🛣️ I have opened a PR to update the guides.
  • ✅ I have added automated tests to cover my changes.
  • 📸 I have attached screenshots to demo visual changes.

@mamhoff mamhoff requested a review from a team as a code owner July 4, 2023 14:34
@github-actions github-actions bot added the changelog:solidus_backend Changes to the solidus_backend gem label Jul 4, 2023
@codecov
Copy link

codecov bot commented Jul 4, 2023

Codecov Report

Merging #5219 (3957b5e) into main (2f59fa0) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main    #5219   +/-   ##
=======================================
  Coverage   88.67%   88.67%           
=======================================
  Files         564      564           
  Lines       13893    13895    +2     
=======================================
+ Hits        12319    12321    +2     
  Misses       1574     1574           
Impacted Files Coverage Δ
...app/controllers/spree/admin/resource_controller.rb 98.80% <100.00%> (+0.01%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

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 @mamhoff, excellent work as always. I left some initial questions.

@@ -2,23 +2,34 @@

require 'spec_helper'

module Spree
module MyEngine
Copy link
Member

Choose a reason for hiding this comment

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

What do you think about duplicating these specs so that we also have some for Spree, instead of replacing them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the spree routes proxy is implicitly tested in all of Solidus' admin controllers, so I believe that's not really necessary. If you insist though I can go that route.

Copy link
Contributor

@waiting-for-dev waiting-for-dev left a comment

Choose a reason for hiding this comment

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

Thanks, @mamhoff! That will be helpful! I am only concerned about the chosen test strategy, where we're modifying the Dummy app routes mounting an engine that it's later removed. From Solidus's point of view, this is more of a refactor, so I don't think there's the need to change anything. At most, we can just test the new routes_proxy public method.

@mamhoff mamhoff force-pushed the resource-controller-for-foreign-engines branch from 6bb9386 to 1f62a61 Compare July 7, 2023 07:55
@mamhoff
Copy link
Contributor Author

mamhoff commented Jul 7, 2023

I am only concerned about the chosen test strategy, where we're modifying the Dummy app routes mounting an engine that it's later removed. From Solidus's point of view, this is more of a refactor, so I don't think there's the need to change anything. At most, we can just test the new routes_proxy public method.

Hm, I did develop the feature using those tests. If we aren't concerned about "how the sausage was made", I'll happily drop the spec change. Y'all decide. :)

@kennyadsl
Copy link
Member

kennyadsl commented Jul 11, 2023

@mamhoff We discussed internally and we think we can drop those specs changes. The setup complexity/impact ratio is probably not worth it. As @waiting-for-dev suggested, probably we can just unit test the new method.

On a side note, I checked existing extensions and I found some occurrences of using the AdminResource controller without the need for this change (e.g. https://github.com/solidusio-contrib/solidus_content/blob/da2de159fa3c14c139f9f2aedff80636a5398ff8/app/controllers/solidus_content/resource_controller.rb#L4). Do you know why it's not needed there? Maybe it's just because of this?

@mamhoff
Copy link
Contributor Author

mamhoff commented Jul 11, 2023

On a side note, I checked existing extensions and I found some occurrences of using the AdminResource controller without the need for this change (e.g. https://github.com/solidusio-contrib/solidus_content/blob/da2de159fa3c14c139f9f2aedff80636a5398ff8/app/controllers/solidus_content/resource_controller.rb#L4). Do you know why it's not needed there? Maybe it's just because of this?

In most Solidus extensions, we create routes within the Spree engine. I have one that isolates the namespace to SolidusFriendlyPromotions, and there it just won't work. Besides, in that example, all the relevant methods (new_object_url, edit_object_url etc.) are overridden, thus sending to whatever the local resource proxy is in that controller, rather than directly addressing a resource proxy.

@kennyadsl
Copy link
Member

Besides, in that example, all the relevant methods (new_object_url, edit_object_url etc.) are overridden, thus sending to whatever the local resource proxy is in that controller, rather than directly addressing a resource proxy.

Yep, I saw that but reading the git history, I think it's for another reason, more specific to that extension's purpose and would happen anyway.

In most Solidus extensions, we create routes within the Spree engine. I have one that isolates the namespace to SolidusFriendlyPromotions, and there it just won't work.

Isolating the engine is legit in the Rails world, even if maybe not a common practice in the Soldius' extensions ecosystem. I think it's more than legit for us to support this use case. Thanks for adding some context. 👍

Within Solidus, we always want to use the `spree` routes proxy as that
is where all routes are defined. However, many people use the Resource
controller as a parent controller in their main app, and because of this
they need to override all the URL generation methods in the Resource
Controller.

This allows one to specify the routes proxy in a custom controller as
follows:

```rb
module MyApp
  class WidgetsController < Spree::Admin::ResourceController
  private

  def routes_proxy
    main_app
  end
end
```

Engine Developers can use this for engines, as well:

```rb
module MyEngine
  class WidgetsController < Spree::Admin::ResourceController
  private

  def routes_proxy
    my_engine
  end
end
```
@mamhoff mamhoff force-pushed the resource-controller-for-foreign-engines branch from 1f62a61 to 3957b5e Compare July 11, 2023 09:27
@waiting-for-dev
Copy link
Contributor

Thanks, @mamhoff!

@waiting-for-dev waiting-for-dev merged commit 568f4ed into solidusio:main Jul 11, 2023
11 checks passed
@elia elia changed the title ResourceController: Allow overriding routes proxy Allow overriding the routes proxy in the ResourceController Sep 26, 2023
mamhoff added a commit to friendlycart/solidus_friendly_promotions that referenced this pull request Oct 5, 2023
The controller code we were using relied on a patch to Solidus[1] that will
only be released with Solidus 4.2. We want to be compatible with Solidus
4 though, so we're incorporating that patch into our own BaseController
  and use that instead of Solidus' ResourceController.

[1] solidusio/solidus#5219
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog:solidus_backend Changes to the solidus_backend gem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants