# Put this in config/initializers/unprotected_attributes.rb and remember to reload your
# development environment afterwards.

class ActiveRecord::Base
  class << self
    def create_with_unprotected(attributes = nil, guard_protected_attributes = true, &block)
      if attributes.is_a?(Array)
        attributes.collect { |attr| create(attr, &block) }
      else
        object = new(attributes, guard_protected_attributes)
        yield(object) if block_given?
        object.save
        object
      end
    end
    alias_method_chain :create, :unprotected
  end
  
  def initialize_with_unprotected(attributes = nil, guard_protected_attributes = true)
    @attributes = attributes_from_column_definition
    @attributes_cache = {}
    @new_record = true
    ensure_proper_type
    send :attributes=, attributes, guard_protected_attributes unless attributes.nil?
    assign_attributes(self.class.send(:scope, :create)) if self.class.send(:scoped?, :create)
    result = yield self if block_given?
    callback(:after_initialize) if respond_to_without_attributes?(:after_initialize)
    result
  end
  alias_method_chain :initialize, :unprotected
  
  def update_attributes_with_unprotected(attributes, guard_protected_attributes = true)
    send :attributes=, attributes, guard_protected_attributes
    save
  end
  alias_method_chain :update_attributes, :unprotected

  def update_attributes_with_unprotected!(attributes, guard_protected_attributes = true)
    send :attributes=, attributes, guard_protected_attributes
    save!
  end
  alias_method_chain :update_attributes!, :unprotected
end

