RolifyCommunity / rolify

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

RuntimeError: can't modify frozen Array when passing a frozen array to with_role #509

Closed btrd closed 4 years ago

btrd commented 5 years ago

Context of the bug

class Place < ApplicationRecord
  resourcify
end

class Role < ApplicationRecord
  PLACE_MANAGER = %w[
    coowner_assistant
    coowner_manager
  ].freeze
end

Then I try to do this :

Place.with_roles(Role::PLACE_MANAGER, current_user)
RuntimeError: can't modify frozen Array
from /Users/jean/.rbenv/versions/2.4.2/lib/ruby/gems/2.4.0/gems/rolify-5.2.0/lib/rolify/resource.rb:14:in `map!'

Is it normal that Rolify modify my array that I pass in argument ?

I did this to quickfix the "bug" :

Place.with_roles(Role::PLACE_MANAGER.dup, current_user)
thomas-mcdonald commented 5 years ago

Looks like it is just mutated in place as a convenience:

https://github.com/RolifyCommunity/rolify/blob/e196d70def9ea6e5cbf9e5db00b53a90be768559/lib/rolify/resource.rb#L13-L15