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

Support polymorphic relations by using has_many :through #571

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ajclaasen
Copy link

Hello, thanks again for all of your work!

Closes #570

This proposal is a minimal implementation to support polymorphic associations from multiple models that call resourcify, to multiple models that call rolify.

It replaces, in a rudimentary way, the has_and_belongs_to_many into two has_many relations with an explicit join table and passes the :as option to that join table.

I couldn't quite get all of the specs to run after trying for a while, but 49 of them did succeed before the rest failed on database setup. I also could not find the file or spec context for the rolify method, but I would love to spec this feature & help out!

Database setup error log
An error occurred while loading ./spec/rolify/config_spec.rb.
Failure/Error:
      create_table(table) do |t|
        t.string :name
        t.references :resource, :polymorphic => true
  
        t.timestamps
      end

ArgumentError:
  wrong number of arguments (given 5, expected 1..4)
# ./spec/support/schema.rb:5:in `block (2 levels) in <top (required)>'
# ./spec/support/schema.rb:4:in `each'
# ./spec/support/schema.rb:4:in `block in <top (required)>'
# ./spec/support/schema.rb:1:in `<top (required)>'
# ./spec/support/adapters/active_record.rb:8:in `<top (required)>'
# ./spec/spec_helper.rb:21:in `load'
# ./spec/spec_helper.rb:21:in `<top (required)>'
# ./spec/rolify/config_spec.rb:1:in `require'
# ./spec/rolify/config_spec.rb:1:in `<top (required)>'

Example polymorphic setup

class User < ApplicationRecord
  rolify role_join_table_name: 'users_roles', as: 'roleable'
end

class Invitation < ApplicationRecord
  rolify role_join_table_name: 'users_roles', as: 'roleable'
end

class UsersRole < ApplicationRecord
  belongs_to :roleable, polymorphic: true
  belongs_to :role
end

Usage

> user.add_role(:admin)
> invitation.add_role(:admin)

> user.has_role?(:admin)
=> true

> invitation.has_role?(:admin)
=> true

> admin_role.users_roles.map(&:roleable)
=> [#<User xxx: "yyy">, #<Invitation xxx: "yyy">]

Please let me know what you think and let me know if there is anything I can do to help!

Other related issues: #318, #411
Related PR: #181

@thomas-mcdonald
Copy link
Member

Hi @rjclaasen, this looks good! Need to get the CI working for pull requests in #573 then we can see what's going on

@krtschmr
Copy link

rip CI

@cedm
Copy link

cedm commented Jul 23, 2024

What's the status on this PR? Has it been superseded by another solution?

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.

Add support for polymorphic User associations
4 participants