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

Bypassing the adapter? #265

Open
bytheway875 opened this issue Jul 8, 2014 · 4 comments
Open

Bypassing the adapter? #265

bytheway875 opened this issue Jul 8, 2014 · 4 comments

Comments

@bytheway875
Copy link

As it stands, each has_role? call queries the db using the kind-of-complicated query in the adapter. In our app internally, because we eager-load roles with User, this is very heavy and seems kind of unnecessary. So we wrote our own has_role? method that more closely resembles the new record implementation of the method and its working really well so far. So I have a couple of questions:

  • Why the adapter? From what I can tell, its because of the differences between ActiveRecord and Mongoid? But are there other functional reason to have these methods query the db every time?
  • If it isn't fully necessary to have it hit the db every time, would it be possible to add an implementation of has_role? and the other finders that bypass the adapter? There would be like a config variable that you set to bypass the adapter, and if that's set to true, you don't have to hit the db on every query.

Anyways, I'm not too sure whether this is something that is feasible or necessary or wanted, but its how we've adapted rolify to work internally. If it is a feature we can add, I'm more than willing to work on it!

@tinynumbers
Copy link

Could you share your implementation of has_role??

I ask because I've taken the same approach to cut down on what I also see as unnecessary role queries. My implementation is exactly like the new record implementation:

  def has_role?(role_name, resource = nil)
    self.roles.detect { |r| r.name == role_name.to_s && (r.resource == resource || resource.nil?) }.present?
  end

Works for me, but just wanting to make sure I'm not overlooking anything.

@jbyers16
Copy link

We're currently testing adding a role collection present check. So far seems to work for eager and non-eager loaded roles.

def has_role?(role_name, resource = nil)
    if new_record? || self.roles.present?
      role_array = self.roles.detect { |r| r.name.to_s == role_name.to_s && (r.resource == resource || resource.nil?) }
    else
      role_array = self.class.adapter.where(self.roles, name: role_name, resource: resource)
    end

    return false if role_array.nil?
    role_array != []
  end

@tinynumbers
Copy link

Thanks @jbyers16

@wldcordeiro
Copy link
Member

If you guys find a better solution that works with Mongo we'd love a PR.

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

4 participants