Sha256: 993c3f74e65a237009a17006f453a4d9946b8cf54eef4e6c8cc6565c84669d81

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module ActiveRecord
  module Core
    def initialize_dup(other) # :nodoc:
      @attributes = @attributes.dup
      # CPK
      # @attributes.reset(self.class.primary_key)
      Array(self.class.primary_key).each {|key| @attributes.reset(key)}

      run_callbacks(:initialize) unless _initialize_callbacks.empty?

      @aggregation_cache = {}
      @association_cache = {}

      @new_record  = true
      @destroyed   = false

      super
    end
    
    
  end
end


module CompositePrimaryKeys
  module ActiveRecordCoreConcernIncludedExtension
    extend ActiveSupport::Concern

    included do
      def self.find(*ids)
        if composite?
          super(cpk_parse_ids(ids))
        else
          super
        end
      end
      
      private
      def self.cpk_parse_ids(ids)
        result = []
        ids.each do |id|
          if id.is_a?(String)
            if id.index(",")
              result << [id.split(",")]
            else
              result << [id]
            end
          elsif id.is_a?(Array) && id.count > 1 && id.first.to_s.index(",")
            result << id.map{|subid| subid.split(",")}
          else
            result << [id]
          end
        end
        
        copy_to_find_depth, depth = result.dup, -1

        until copy_to_find_depth == result.flatten
          depth += 1
          copy_to_find_depth = copy_to_find_depth.flatten(1)
        end
        
        result = result.flatten(depth)
        return result
      end
    end
  end
end
  
ActiveRecord::Base.send(:include, CompositePrimaryKeys::ActiveRecordCoreConcernIncludedExtension)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
composite_primary_keys-8.0.0 lib/composite_primary_keys/core.rb