Module: Skr::Concerns::ImmutableModel::ClassMethods
- Defined in:
- lib/skr/concerns/immutable_model.rb
Instance Method Summary collapse
-
#is_immutable(options = {}) ⇒ Object
Once it’s created it may not be updated or destroyed.
Instance Method Details
#is_immutable(options = {}) ⇒ Object
Once it’s created it may not be updated or destroyed.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/skr/concerns/immutable_model.rb', line 12 def is_immutable( = {}) [:except] = [*[:except]].map{|name| name.to_s } # make sure except is present and an array before_destroy do raise ActiveRecord::ReadOnlyRecord.new( "Can not destroy #{self.class.model_name}, only create is allowed" ) end before_update do unless ( changes.keys - change_tracking_fields - [:except] ).blank? raise ActiveRecord::ReadOnlyRecord.new( "Can not update, only create #{self.class.model_name}" ) end end end |