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 to easily extend Auth#store_location behavior #3369

Merged

Conversation

spaghetticode
Copy link
Member

@spaghetticode spaghetticode commented Oct 8, 2019

Description

Often makes sense change the behavior of the method Spree::Core::ControllerHelpers::Auth#store_location, which is responsible for setting session[:spree_user_return_to]` value to the current URL. When necessity arises, the
only way ATM is to monkey patch Solidus version of this method.

This PR allows adding custom behavior by less obtrusive means, ie. by using custom rules.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have updated Guides and README accordingly to this change (if needed)
  • I have added tests to cover this change (if needed)

@spaghetticode spaghetticode force-pushed the spaghetticode/extendable-store-location branch 2 times, most recently from 6a99c20 to 8c6dd76 Compare October 11, 2019 15:28
@spaghetticode spaghetticode force-pushed the spaghetticode/extendable-store-location branch 3 times, most recently from 26385dc to 25a8fad Compare October 18, 2019 08:07
@spaghetticode spaghetticode self-assigned this Oct 18, 2019
@spaghetticode spaghetticode force-pushed the spaghetticode/extendable-store-location branch from 25a8fad to a84179e Compare October 18, 2019 10:14
@spaghetticode spaghetticode force-pushed the spaghetticode/extendable-store-location branch 2 times, most recently from edbcdc8 to 35fc1a3 Compare October 18, 2019 10:45
@spaghetticode spaghetticode marked this pull request as ready for review October 18, 2019 10:49
Copy link
Contributor

@mdesantis mdesantis left a comment

Choose a reason for hiding this comment

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

I like it!

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.

Very nice!

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.

I love this PR, both for its implementation and documentation, thanks Andrea. 💘

I left mostly unblocking comments, let me know your thoughts!

# @example This method can be used also to add more rules
# Spree::UserReturnToStorer.rules << 'CustomRule'
#
# @examle it can be used also removing unwanted rules
Copy link
Member

Choose a reason for hiding this comment

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

examle -> example

# This service object is responsible for storing the current path into
# into `session[:spree_user_return_to]` for redirects after successful
# user/admin authentication.
class UserReturnToStorer
Copy link
Member

Choose a reason for hiding this comment

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

This name is a bit hard to understand maybe, at least for me at the first look. I immediately thought about Returns and I'm wondering if we can use another terminology, which conflicts less with other elements of the Solidus domain, like UserLastLocationStorer maybe? I think it could be better even because this proposed name is not connected with how we'll use it, while ReturnTo is. Not a strong opinion here, what's yours?

Copy link
Member Author

Choose a reason for hiding this comment

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

My strong opinion here is that I don't like that name either 😄

I think UserLastLocationStorer is good and better than mine, but it's a bit long. What about UserLastUrlStorer or UserLocationStorer?

As soon as we agree on a name I'll revisit the PR with the other suggestions as well... so much namespace and filenames to change 😄

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 UserLastUrlStorer is fine. Let's see if we have some other opinion here :)

class Configuration
def rules
@rules ||= ::Spree::Core::ClassConstantizer::Set.new.tap do |set|
set << 'Spree::UserReturnToStorer::Rules::AuthenticationRule'
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 it could be better setting the default rule into core/lib/spree/app_configuration.rb where we are creating the preference to better split the implementation and how we are actually using it in Solidus and also for consistency with the other preferences that seem to have defaults set there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good 👍

@spaghetticode spaghetticode force-pushed the spaghetticode/extendable-store-location branch 3 times, most recently from 32d8e01 to 71d13fb Compare November 8, 2019 13:44
Spree::UserLastUrlStorer can be used to selectively store the
current path in `session[:spree_user_return_to]`. This object
allows easy extension/change of behavior by using a list of
rules. When at least one rule is matched, the request path is
not stored in session.

The class ships with the default rule AutenticationRule, that was
extacted from Spree::Core::ControllerHelpers::Auth#store_location
By using Spree::UserLastUrlStorer one can change
ControllerHelpers::Auth#store_location behavior without
resorting to monkey patching.
@spaghetticode spaghetticode force-pushed the spaghetticode/extendable-store-location branch from 71d13fb to 55d1772 Compare November 8, 2019 14:26
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, @spaghetticode. This PR is fantastic!

@kennyadsl kennyadsl merged commit bdb3c43 into solidusio:master Nov 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants