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

Insert new model adapters at the start of the list #640

Merged
merged 1 commit into from
Sep 13, 2020
Merged

Insert new model adapters at the start of the list #640

merged 1 commit into from
Sep 13, 2020

Conversation

ghiculescu
Copy link
Contributor

@ghiculescu ghiculescu commented Jun 19, 2020

This makes implementing new model adapters easier. The example in the docs adds an adapter for non-activerecord. That works fine - none of the builtin adapters will work with it. But consider the use case where you want to add a more specific adapter for activerecord, and don't want to fork cancancan.

module CanCan
  module ModelAdapters
    class IDProxyAdapter < ActiveRecord5Adapter
      AbstractAdapter.inherited(self)

      def self.for_class?(model_class)
        super
      end

Currently this will not work, because IDProxyAdapter will come after ActiveRecord5Adapter in CanCan::ModelAdapters::AbstractAdapters#subclasses. So ActiveRecord5Adapter will always get used.

By adding new adapters to the start of the subclasses list, we inverse this. All the builtin adapters will end up last in the list; all adapters added in userland will be considered first.

This is safe with all built in adapters because they are written in an exclusive way (AR4, AR5). As a result, no tests should break here. However, this could cause problems for users who have added AR adapters in the wild if they haven't added to them the start of the subclasses list. For that reason I think this should be considered a breaking change.

If this is accepted I'm happy to also tidy up the docs to make it more clear how easy adapters are to add.

@coorasse
Copy link
Member

I like it! Give me some time when I am back from holidays to properly schedule it in a release.

@coorasse coorasse self-assigned this Jun 27, 2020
@ghiculescu
Copy link
Contributor Author

No rush. Enjoy the summer!

@coorasse
Copy link
Member

Good! This can be merged, although forking cancancan is not the way to go anyway. a separate adapter can be written without forking. The docs are outdated. I'll take care of updating them

@coorasse coorasse merged commit 880897d into CanCanCommunity:develop Sep 13, 2020
@ghiculescu ghiculescu deleted the patch-1 branch October 9, 2020 18:41
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