Module: NoBrainer::Document::Readonly::ClassMethods

Defined in:
lib/no_brainer/document/readonly.rb

Instance Method Summary collapse

Instance Method Details

#field(attr, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/no_brainer/document/readonly.rb', line 5

def field(attr, options={})
  super
  inject_in_layer :readonly do
    case options[:readonly]
    when true
      define_method("#{attr}=") do |value|
        unless new_record?
          if read_attribute(attr) != value
            raise NoBrainer::Error::ReadonlyField.new("#{attr} is readonly")
          end
        end
        super(value)
      end
    when false then remove_method("#{attr}=") if method_defined?("#{attr}=")
    end
  end
end

#remove_field(attr, options = {}) ⇒ Object



23
24
25
26
27
28
# File 'lib/no_brainer/document/readonly.rb', line 23

def remove_field(attr, options={})
  super
  inject_in_layer :readonly do
    remove_method("#{attr}=") if method_defined?("#{attr}=")
  end
end