Created
February 3, 2011 09:33
-
-
Save svenwin/809264 to your computer and use it in GitHub Desktop.
attributes are set at the end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def initialize_with_defaults(attrs = nil) | |
initialize_without_defaults() do | |
if attrs | |
stringified_attrs = attrs.stringify_keys | |
safe_attrs = sanitize_for_mass_assignment(stringified_attrs) | |
safe_attribute_names = safe_attrs.keys.map do |x| | |
x.to_s | |
end | |
end | |
self.class._default_attribute_values.each do |attribute, container| | |
if safe_attribute_names.nil? || safe_attribute_names.none? { |attr_name| attr_name =~ /^#{attribute}($|\()/ } | |
__send__("#{attribute}=", container.evaluate(self)) | |
changed_attributes.delete(attribute) | |
end | |
end | |
yield(self) if block_given? | |
end | |
self.attributes= attrs | |
self | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment