Permalink
Browse files

handle ActiveRecord::Base#[]= for immutable attributes

  • Loading branch information...
nmertaugh committed Jul 17, 2011
1 parent ad48bd9 commit f1e4f27fab2aac90b26d306b8d668da3a60b9c5d
Showing with 10 additions and 0 deletions.
  1. +10 −0 lib/immutable_attributes.rb
@@ -21,6 +21,16 @@ def attr_immutable(*args)
end
end
end
+ # handle ActiveRecord::Base#[]=
+ class_eval <<-RUBY
+ def []=(attr_name, value)
+ if #{args}.include? attr_name
+ raise(ActiveRecord::ImmutableAttributeError, "\#{attr_name} is immutable!")
+ else
+ write_attribute(attr_name, value)
+ end
+ end
+ RUBY
end
def validates_immutable(*attr_names)

0 comments on commit f1e4f27

Please sign in to comment.