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

Version Path
rails_core_extensions-0.12.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.11.3 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.11.2 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.11.1 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.11.0.beta.11 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.10.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.9.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.8.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.7.1 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.7.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.6.1 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.6.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.4.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.3.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.2.0 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.1.1 lib/rails_core_extensions/active_record_cloning.rb
rails_core_extensions-0.1.0 lib/rails_core_extensions/active_record_cloning.rb