module ActiveRecord::Clone::InstanceMethods

Public Instance Methods

clone_ar(options={}) { |newObj, self| ... } click to toggle source
# File lib/active_record/clone.rb, line 53
def clone_ar(options={})
  options = (self.instance_variable_get(:@options) ? self.instance_variable_get(:@options) : self.class.send(:default_options)).keep_merge(options)
  attrs = []
  if options[:only] and options[:only].is_a? Array
    attrs = self.attribute_names.reject {|item| options[:only].include? item}
  else
    excluded = options[:excluded] + (options[:skip_relations] ? self.class.send(:foreing_keys) : [])
    attrs = self.attribute_names.reject { |item| excluded.include? item.to_sym }
  end
  
  newObj = self.class.new
  attrs.each do |attribute|
    newObj.send(:write_attribute, attribute.to_sym, self.read_attribute(attribute.to_sym))
  end        
  yield newObj, self if block_given?
  return newObj
end