Speed up mass assignment by avoiding extra loops.

This commit is contained in:
José Valim 2012-03-16 13:40:42 +01:00
parent 6d0047aff8
commit 034ccf4048
1 changed files with 5 additions and 7 deletions

View File

@ -3,18 +3,16 @@ module ActiveModel
class Sanitizer
# Returns all attributes not denied by the authorizer.
def sanitize(attributes, authorizer)
sanitized_attributes = attributes.reject { |key, value| authorizer.deny?(key) }
debug_protected_attribute_removal(attributes, sanitized_attributes)
rejected = []
sanitized_attributes = attributes.reject do |key, value|
rejected << key if authorizer.deny?(key)
end
process_removed_attributes(rejected) unless rejected.empty?
sanitized_attributes
end
protected
def debug_protected_attribute_removal(attributes, sanitized_attributes)
removed_keys = attributes.keys - sanitized_attributes.keys
process_removed_attributes(removed_keys) if removed_keys.any?
end
def process_removed_attributes(attrs)
raise NotImplementedError, "#process_removed_attributes(attrs) suppose to be overwritten"
end