Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

  • Loading branch information...
commit f1e4f27fab2aac90b26d306b8d668da3a60b9c5d 1 parent ad48bd9
@nmertaugh authored
Showing with 10 additions and 0 deletions.
  1. +10 −0 lib/immutable_attributes.rb
View
10 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)
Please sign in to comment.
Something went wrong with that request. Please try again.