Sha256: b4f4747766a24322e01b96f64618792feecddf01e1cdbc5c1f359db836525fe9

Contents?: true

Size: 1.05 KB

Versions: 9

Compression:

Stored size: 1.05 KB

Contents

module Skr
    module Concerns

        module ImmutableModel

            extend ActiveSupport::Concern

            module ClassMethods

                # Once it's created it may not be updated or destroyed.
                # @raise [ActiveRecord::ReadOnlyRecord] if destroy, delete or save is called after it's been created.
                def is_immutable(options = {})

                    options[:except] = [*options[: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 - options[:except] ).blank?
                            raise ActiveRecord::ReadOnlyRecord.new(  "Can not update, only create #{self.class.model_name}" )
                        end
                    end

                end

            end

        end
    end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
stockor-0.5.0 lib/skr/concerns/immutable_model.rb
stockor-0.4.0 lib/skr/concerns/immutable_model.rb
stockor-0.3.0 lib/skr/concerns/immutable_model.rb
stockor-0.2 lib/skr/concerns/immutable_model.rb
stockor-0.1.9 lib/skr/concerns/immutable_model.rb
stockor-0.1.8 lib/skr/concerns/immutable_model.rb
stockor-0.1.7 lib/skr/concerns/immutable_model.rb
stockor-0.1.5 lib/skr/concerns/immutable_model.rb
stockor-core-0.2 lib/skr/concerns/immutable_model.rb