Sha256: 39320de7ba023fc8bb50439638e3b3e1da4c4c986cf452d2d4bdf0ec1980fbff
Contents?: true
Size: 1.71 KB
Versions: 17
Compression:
Stored size: 1.71 KB
Contents
module ActiveRecordCloning def self.included(base) base.extend(ClassMethods) end module ClassMethods def inherited(subclass) super subclass.cloned_attributes_hash = cloned_attributes_hash end def attributes_included_in_cloning cloned_attributes_hash[:include].dup end def attributes_excluded_from_cloning cloned_attributes_hash[:exclude].dup end def clones_attributes(*attributes) cloned_attributes_hash[:include] = attributes.map(&:to_sym) end def clones_attributes_except(*attributes) cloned_attributes_hash[:exclude] = attributes.map(&:to_sym) end def clones_attributes_reset @cloned_attributes = nil end def exclude_attributes(cloned, excludes) excluded_attributes(excludes).each do |attr| cloned.send("#{attr}=", nil) end end def excluded_attributes(excludes) all_attributes = attribute_names.map(&:to_sym) included_attributes = if attributes_included_in_cloning.empty? all_attributes else all_attributes & attributes_included_in_cloning end all_attributes - included_attributes + attributes_excluded_from_cloning + excludes end protected def cloned_attributes_hash @cloned_attributes ||= {:include => [], :exclude => []} end def cloned_attributes_hash=(attributes_hash) @cloned_attributes = attributes_hash end end def clone_excluding(excludes=[]) method = ActiveRecord::Base.instance_methods(false).include?(:clone) ? :clone : :dup cloned = send(method) excludes ||= [] excludes = [excludes] unless excludes.is_a?(Enumerable) self.class.exclude_attributes(cloned, excludes) cloned end end
Version data entries
17 entries across 17 versions & 1 rubygems