Skip to content

eac/mass_assign_more

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mass assign more

Note: Builds off mass assignment refactoring patch. Doesn’t work with edge Rails yet.

Context-based mass assignment for Rails. Depends on ActiveRecord/MassAssignmentSecurity

Install

sudo gem install mass_assign_more

The Basic Idea

Allows attr_accessible to be temporarily expanded depending on the context. For example, an admin wishing to change their account should be able to mass assign more attributes than a non-admin.

Sample Usage

Here’s one use case with a model. This could just as easily be a controller or any object. See /examples for details.

In examples/model.rb:

class Account < ActiveRecord::Base
  include MassAssignMore::Context

  belongs_to :user

  attr_accessible :name
  accessible_attributes[:admin] = [ :plan_id ]

  protected

  # Assumes User#role returns :admin when appropriate.
  def mass_assignment_context
    user.role if user
  end

end

@account.user.role
# => nil
@account.attributes = { :name => "Eric's Account", :plan_id => 5 }
@account.attributes
# => { :name => "Eric's Account" }

@account.user.role = :admin
@account.attributes = { :plan_id => 5 }
@account.attributes
# => { :name => "Eric's Account", :plan_id => 5 }

Enjoy

This project is hosted at github.com/eac/mass_assign_more. Feedback and enhancements can be directed that way.

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but

    bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright © 2009 Eric Chapweske. See LICENSE for details.

About

Context-based mass assignment for Rails

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages