Allow mixing of protected and accessible properties.
Any unspecified properties are now assumed to be protected by default
This commit is contained in:
parent
bf22222fd9
commit
aac6b80d26
4 changed files with 67 additions and 55 deletions
|
@ -1,25 +1,31 @@
|
|||
module CouchRest
|
||||
module Model
|
||||
module AttributeProtection
|
||||
# Attribute protection from mass assignment to CouchRest properties
|
||||
#
|
||||
# Attribute protection from mass assignment to CouchRest::Model properties
|
||||
#
|
||||
# Protected methods will be removed from
|
||||
# * new
|
||||
# * 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
|
||||
# There are two modes of protection
|
||||
# 1) Declare accessible poperties, and assume all unspecified properties are protected
|
||||
# property :name, :accessible => true
|
||||
# property :admin # this will be automatically protected
|
||||
#
|
||||
# 2) Declare protected properties, and assume all unspecified properties are accessible
|
||||
# property :name # this will not be protected
|
||||
# property :admin, :protected => true
|
||||
#
|
||||
# Note: you cannot set both flags in a single class
|
||||
|
||||
# 3) Mix and match, and assume all unspecified properties are protected.
|
||||
# property :name, :accessible => true
|
||||
# property :admin, :protected => true
|
||||
# property :phone # this will be automatically protected
|
||||
#
|
||||
# Note: the timestamps! method protectes the created_at and updated_at properties
|
||||
|
||||
|
||||
def self.included(base)
|
||||
base.extend(ClassMethods)
|
||||
end
|
||||
|
@ -56,18 +62,13 @@ module CouchRest
|
|||
private
|
||||
|
||||
def properties_to_remove_from_mass_assignment
|
||||
has_protected = !protected_properties.empty?
|
||||
has_accessible = !accessible_properties.empty?
|
||||
to_remove = protected_properties
|
||||
|
||||
if !has_protected && !has_accessible
|
||||
[]
|
||||
elsif has_protected && !has_accessible
|
||||
protected_properties
|
||||
elsif has_accessible && !has_protected
|
||||
properties.reject { |prop| prop.options[:accessible] }
|
||||
else
|
||||
raise "Set either :accessible or :protected for #{self.class}, but not both"
|
||||
unless accessible_properties.empty?
|
||||
to_remove += properties.reject { |prop| prop.options[:accessible] }
|
||||
end
|
||||
|
||||
to_remove
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue