RolifyCommunity / rolify

Role management library with resource scoping
https://rolifycommunity.github.io/rolify/
MIT License
3.17k stars 405 forks source link

rolify and rails fixtures #337

Open daniely opened 9 years ago

daniely commented 9 years ago

I'm using rails 4.2 and had a tough time with rolify and rails fixtures. After some mucking I finally got it working. Posting here incase someone else needs to do something similar and also creating this issue because it seems like the difficulty with fixtures is something rolify should "fix". But perhaps fixing simply means documenting somewhere. If that's desired I'd be happy to add a wiki.

To get rolify working with rails fixtures:

  1. Create a UsersRole model in models/users_role.rb
# pretty much exclusively created for our fixtures to work
class UsersRole < ActiveRecord::Base
  belongs_to :user
  belongs_to :role
end
  1. Create fixtures like so:
# users.yml
some_user:

# roles.yml - key here is to NOT include "resource" 
admin_role:
  name: :admin

# users_roles.yml
users_roles1:
  user: some_user
  role: admin
  1. Add the fixtures to your tests
# in some test file
fixtures :users, :roles, :users_roles

users(:some_user).has_role? :admin # => true
antonzhyliuk commented 9 years ago

I think that creating additional model only for fixtures is big overhead but I agree that existing fixtures API is terrifying, I lost about 2-3 hours to persist object-scoped role to user for my integration tests. Goal was to generate by fixtures a user model object with role scoped to dealership object defined in fixtures. Key here to not try define "users:" in roles.yml fixtures, because I dont know why, but if I try to set users in fixture then attribute not set correctly.

example:

# roles.yml
first_dealership_admin_role:
  name: :dealership_admin
  resource_id: 1
  resource_type: 'Dealership'

# users.yml
dealership_admin:
  id: 4
  email: 'da@test.test'
  encrypted_password: <%= Devise.bcrypt(User, 'password') %>
  dealership_id: 1
  roles: first_dealership_admin_role

# dealerships.yml
one:
  id: 1
  name: dealership_name
daniely commented 9 years ago

@Crashtown thanks for sharing your way of doing it. I agree it's weird adding a model just for the fixtures to work with rolify but I don't think there's anything wrong with it. After all, the users_roles table does exist and we're just creating a concrete model for it.

Also, I prefer not having to manage id fields in my fixtures.

But again, thank you for sharing! It's great seeing how others are tackling this problem

wldcordeiro commented 9 years ago

If you guys would like to add this to the wiki, it'd be great. I'm also open to suggestions regarding solutions that would simplify this process.

daniely commented 9 years ago

@wldcordeiro sure, will add to the wiki later today.

Unfortunately, I don't have any better ideas on how to solve this right now. Maybe once I take a deeper look at the rolify code something will come up.

daniely commented 9 years ago

@wldcordeiro ok added it here and also added a link to it on the main wiki page.

@Crashtown I also put your solution in there as well.