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

remove all roles for a resource #426

Open
Genkilabs opened this issue Nov 23, 2016 · 1 comment
Open

remove all roles for a resource #426

Genkilabs opened this issue Nov 23, 2016 · 1 comment

Comments

@Genkilabs
Copy link

Given a User with a collection of unknown roles for a known resource, I would like to remove them all on some condition.
Of course I could do something like

	rolify :before_add => :remove_old_roles
	def remove_old_roles(role)
		roles.where(:resource => role.resource).collect(&:names).each do |old_role_name|
			remove_role old_role_name, role.resource
		end if do_refresh
	end

But what would be superior would be to allow some kind of blank/any syntax such as:
current_user.remove_role nil, resource
This currently does not work because the remove_role method casts the param role_name to a string before handing it off to the adapter.

One less-intrusive solution I imagined might be to change the included adapters as such:

  def remove(relation, role_name, resource = nil)
    cond = {}
    cond[:name] = role_name unless role_name.blank?
    cond[:resource_type] = (resource.is_a?(Class) ? resource.to_s : resource.class.name) if resource

(For reference, I'm thinking in here: https://github.com/RolifyCommunity/rolify/blob/master/lib/rolify/adapters/active_record/role_adapter.rb#L53)

I took a crack at doing this myself and would be happy to make a PR, but I can't seem to get the gem to build...? LoadError: cannot load such file -- rolify/adapters/active_record/resource_adapter.rb <_<

Anyways, does this seem like a good change, or is there a better remove_all_roles(resource) solution I am unaware of?

If no one wants to make it, I can, but is there a build procedure or contribution doc I should follow?

@Genkilabs
Copy link
Author

This is the gist of what I'm thinking Genkilabs@eb96834

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

No branches or pull requests

1 participant