123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Attributes::Readonly

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/attributes/readonly.rb

Overview

This module defines behavior for readonly attributes.

Instance Method Summary

DSL Calls

included

[ GitHub ]


11
12
13
14
# File 'lib/mongoid/attributes/readonly.rb', line 11

included do
  class_attribute :readonly_attributes
  self.readonly_attributes = ::Set.new
end

Instance Method Details

#_loaded?(name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/mongoid/attributes/readonly.rb', line 40

def _loaded?(name)
  __selected_fields.nil? || projected_field?(name)
end

#as_writable_attribute!(name, value = :nil) (private)

[ GitHub ]

  
# File 'lib/mongoid/attributes/readonly.rb', line 31

def as_writable_attribute!(name, value = :nil)
  normalized_name = database_field_name(name)
  if attribute_writable?(normalized_name)
    yield(normalized_name)
  else
    raise Errors::ReadonlyAttribute.new(name, value)
  end
end

#attribute_writable?(name) ⇒ true | false

Are we able to write the attribute with the provided name?

Examples:

Can we write the attribute?

model.attribute_writable?(:title)

Parameters:

Returns:

  • (true | false)

    If the document is new, or if the field is not readonly.

[ GitHub ]

  
# File 'lib/mongoid/attributes/readonly.rb', line 25

def attribute_writable?(name)
  new_record? || (!self.class.readonly_attributes.include?(name) && _loaded?(name))
end

#projected_field?(name) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/mongoid/attributes/readonly.rb', line 44

def projected_field?(name)
  projected = (__selected_fields || {}).keys.select { |f| __selected_fields[f] == 1 }
  projected.empty? || projected.include?(name)
end