Open daniely opened 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
@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
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.
@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.
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:
UsersRole
model inmodels/users_role.rb