Skip to content

Instantly share code, notes, and snippets.

@svenwin
Created February 3, 2011 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenwin/809264 to your computer and use it in GitHub Desktop.
Save svenwin/809264 to your computer and use it in GitHub Desktop.
attributes are set at the end
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