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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/rolify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,13 @@ def rolify(options = {})
self.role_join_table_name = options[:role_join_table_name]

rolify_options = { :class_name => options[:role_cname].camelize }
rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"
rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove, :inverse_of ].include? k.to_sym })

has_and_belongs_to_many :roles, **rolify_options
join_table_options = { }
join_table_options.merge!(options.reject{ |k,v| ![ :as ].include? k.to_sym })

has_many self.role_join_table_name.to_sym, dependent: :destroy, **join_table_options
has_many :roles, through: self.role_join_table_name.to_sym, **rolify_options

self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)

Expand Down