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 subclasses of session store to override session_class #204

Merged
merged 1 commit into from
Aug 15, 2023
Merged

Allow subclasses of session store to override session_class #204

merged 1 commit into from
Aug 15, 2023

Conversation

TobiasBales
Copy link
Contributor

@TobiasBales TobiasBales commented Aug 15, 2023

With the current implementation it is not possible for different subclasses to use different session models since it uses a class variable to access it which is shared amongst inherited classes.

This changes uses the accessor method instead which can be overridden by the subclass(es).

This includes the changes from #194 to make the pipeline work again

@@ -67,7 +67,7 @@ def get_session(request, sid)
# If the sid was nil or if there is no pre-existing session under the sid,
# force the generation of a new sid and associate a new session associated with the new sid
sid = generate_sid
session = @@session_class.new(:session_id => sid.private_id, :data => {})
session = session_class.new(:session_id => sid.private_id, :data => {})
Copy link
Member

Choose a reason for hiding this comment

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

session_class is a cattr_accessor, meaning subclasses will fight over that variable. You want class_attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cattr_accessor actually generates https://apidock.com/rails/Class/cattr_accessor but agreed, I will change it to self.class.session_class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated it

Copy link
Member

Choose a reason for hiding this comment

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

cattr_accessor actually generates

That's not the problem, the problem is that it is backed by a class variable (@@) https://github.com/rails/rails/blob/522c86f35ccc80453ed9fb6ca8b394db321f9a69/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb#L124

Copy link
Member

Choose a reason for hiding this comment

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

updated it

I didn't mean to chance for self.class.... calls, but to declare the variable with class_attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, never ran across class_attribute before

With the current implementation it is not possible for different subclasses
to use different session models since it uses a class variable to access
it which is shared amongst inherited classes.
This uses the accessor method instead which can be overriden by subclasses
@byroot byroot merged commit 0fa232b into rails:master Aug 15, 2023
9 checks passed
@TobiasBales TobiasBales deleted the allow-subclassing-session-store branch August 15, 2023 13:37
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.

2 participants