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 has_many :through instead of just only has_and_belongs_to_many #181

Open
wants to merge 1 commit 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
21 changes: 15 additions & 6 deletions lib/rolify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,24 @@ def rolify(options = {})
self.role_cname = options[:role_cname]
self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")

default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
options.reverse_merge!({:role_join_table_name => default_join_table})
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 ].include? k.to_sym })

has_and_belongs_to_many :roles, rolify_options
# Option to support has_many :through
has_many_through_table = options.delete(:has_many_through)
if has_many_through_table

This comment was marked as resolved.

self.role_join_table_name = has_many_through_table
Copy link
Author

Choose a reason for hiding this comment

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

Because I need to use that variable here :)

rolify_options[:through] = self.role_join_table_name

has_many :roles, rolify_options
else
default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
options.reverse_merge!({:role_join_table_name => default_join_table})
self.role_join_table_name = options[:role_join_table_name]
rolify_options.merge!({ :join_table => self.role_join_table_name }) if Rolify.orm == "active_record"

has_and_belongs_to_many :roles, rolify_options
end

self.adapter = Rolify::Adapter::Base.create("role_adapter", self.role_cname, self.name)
load_dynamic_methods if Rolify.dynamic_shortcuts
Expand Down