Sha256: 6b736cf7459af18e70ed46ebe0b2a4dd676768360800163cad572a73acc9f812

Contents?: true

Size: 637 Bytes

Versions: 1

Compression:

Stored size: 637 Bytes

Contents

require 'active_record'
require 'active_record-readonly_model/version'

module ActiveRecord
  module ReadonlyModel
    @@bypass_flag = false

    def self.bypass(&block)
      @@bypass_flag = true
      yield
      @@bypass_flag = false
    end

    # Prevents objects from being created
    def readonly?
      super && !@@bypass_flag
    end

    def self.included(base)
      base.class_eval do
        def readonly
          true
        end
        before_save { raise ActiveRecord::ReadOnlyRecord unless @@bypass_flag }
        before_destroy { raise ActiveRecord::ReadOnlyRecord unless @@bypass_flag }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord-readonly_model-0.0.2 lib/active_record-readonly_model.rb