Repository is archived
No commit activity in last 3 years
No release in over 3 years
This gem allows you to pass multiple roles into methods like save and update_attributes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

~> 3.2.3
 Project Readme

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