Module: ActiveRecord::ReadonlyModel

Defined in:
lib/active_record-readonly_model.rb,
lib/active_record-readonly_model/version.rb

Constant Summary collapse

VERSION =
"0.0.2"
@@bypass_flag =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bypass(&block) ⇒ Object



8
9
10
11
12
# File 'lib/active_record-readonly_model.rb', line 8

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

.included(base) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/active_record-readonly_model.rb', line 19

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

Instance Method Details

#readonly?Boolean

Prevents objects from being created

Returns:

  • (Boolean)


15
16
17
# File 'lib/active_record-readonly_model.rb', line 15

def readonly?
  super && !@@bypass_flag
end