Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
This gem allows you to pass multiple roles into methods like save and update_attributes
Ruby
branch: master
Failed to load latest commit information.
lib
test
.gitignore
.rvmrc
.travis.yml
Gemfile
LICENSE
README.md
Rakefile
mass_assignment_with_multiple_roles.gemspec

README.md

MassAssignmentWithMultipleRoles Build Status

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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request
Something went wrong with that request. Please try again.