Module: CouchRest::Mixins::AttributeProtection

Included in:
ExtendedDocument
Defined in:
lib/couchrest/mixins/attribute_protection.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Attribute protection from mass assignment to CouchRest properties

Protected methods will be removed from

* new 
* update_attributes
* upate_attributes_without_saving
* attributes=

There are two modes of protection

1) Declare accessible poperties, assume all the rest are protected 
  property :name, :accessible => true
  property :admin                     # this will be automatically protected

2) Declare protected properties, assume all the rest are accessible
  property :name                      # this will not be protected
  property :admin, :protected => true

Note: you cannot set both flags in a single class



23
24
25
# File 'lib/couchrest/mixins/attribute_protection.rb', line 23

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#accessible_propertiesObject



37
38
39
# File 'lib/couchrest/mixins/attribute_protection.rb', line 37

def accessible_properties
  self.class.accessible_properties
end

#protected_propertiesObject



41
42
43
# File 'lib/couchrest/mixins/attribute_protection.rb', line 41

def protected_properties
  self.class.protected_properties
end

#remove_protected_attributes(attributes) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/couchrest/mixins/attribute_protection.rb', line 45

def remove_protected_attributes(attributes)
  protected_names = properties_to_remove_from_mass_assignment.map { |prop| prop.name }
  return attributes if protected_names.empty?

  attributes.reject! do |property_name, property_value|
    protected_names.include?(property_name.to_s)
  end

  attributes || {}
end