Quite often your user can have multiple roles at the same time. ActiveModel
does not provide the ability to sanitize attributes using intersection of attr_accessible
for several role names at the same time.
This gem is all about to solve this problem.
Example of usage
Consider we have the following model code:
class Student < ActiveRecord::Base
attr_accessible :phone_number
attr_accessible :name, as: :admin
attr_accessible :email, as: :user
end
Now, while updating in controller we can write:
student = Student.find(params[:id])
if student.update_attributes(params[:student], :as => [:admin, :teacher])
Role names can be passed in ActiveModel
style:
student.update_attributes(params[:student], :as => :admin)
all it's functionality is fully supported.
Several roles can be passed as an array symbols:
student.update_attributes(params[:student], :as => [:admin, :teacher])
But in most cases, role names can be obtained from user model. If your user
model provides method called role_names
, which returns as array of role
names, you can write code which is quite easy to understand.
In model:
class User < ActiveRecord::Base
has_many :roles
def role_names
roles.map &:name
end
end
In controller:
student.update_attributes(params[:student], :as => current_user)
Installation
Add this line to your application's Gemfile:
gem 'mass_assignment_with_multiple_roles'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mass_assignment_with_multiple_roles
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request