Skip to content

A validation method to make a model attribute immutable. That is, the attribute can not be changed after it is set the first time.

License

Notifications You must be signed in to change notification settings

wmoxam/immutable-attribute-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnsuresImmutabilityOf
=====================

There are many cases where a model attribute should not be changed once it's set. This plugin makes it dead simple.

Example
=======

class Account < ActiveRecord::Base
  ensures_immutability_of :username, :email
end

account = Account.create(:username => 'jgreen')
...
account.update(:username => 'jgreen')  # raises ActiveRecord::ImmutableAttributeError

Collections can all be immutable as well (thanks to Dmitry Ratnikov on #rubyonrails)

class Account  < ActiveRecord::Base
  has_many :infos
  ensures_immutability_of :username, :email, :infos   # note: this must come after the has_many declaration, otherwise
                                                      # AR will overwrite the setter
end

account = Account.create(:username => 'wmoxam')
account.infos = Info.find(:all, :conditions => ["id < ?", 3]
account.infos =  Info.find(:all, :conditions => ["id > ?", 3]  # raises ActiveRecord::ImmutableAttributeError

Copyright (c) 2007-2008 Wesley Moxam - Savvica Inc, released under the MIT license

About

A validation method to make a model attribute immutable. That is, the attribute can not be changed after it is set the first time.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages