Module: ActiveRecord::Clone::InstanceMethods

Defined in:
lib/active_record/clone.rb

Instance Method Summary collapse

Instance Method Details

#clone_ar(options = {}) {|newObj, _self| ... } ⇒ Object

Yields:

  • (newObj, _self)

Yield Parameters:



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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