Commit 76ab2f3f authored by Ohad Dahan's avatar Ohad Dahan

Add nativepluck2 (temp name) to act as original :pluck, after review it will become the new API

parent 82abf080
......@@ -12,4 +12,5 @@ group :development, :test do
gem 'memory_profiler'
gem 'benchmark-ips'
gem 'activerecord-import'
gem 'pry'
end
......@@ -61,6 +61,7 @@ GEM
builder (3.2.3)
case_transform (0.2)
activesupport
coderay (1.1.2)
concurrent-ruby (1.0.5)
crass (1.0.4)
erubi (1.7.1)
......@@ -87,6 +88,9 @@ GEM
mini_portile2 (~> 2.3.0)
oj (3.6.12)
pg (1.1.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
rack (2.0.5)
rack-test (1.1.0)
rack (>= 1.0, < 3)
......@@ -140,6 +144,7 @@ DEPENDENCIES
memory_profiler
nativepluck!
oj
pry
BUNDLED WITH
1.16.4
......@@ -39,7 +39,7 @@ module Nativepluck
def self.included(klass)
klass.extend(ClassMethods)
init_nativepluck
end
end
def self.init_nativepluck
return if @nativepluck_init
......@@ -63,6 +63,7 @@ module Nativepluck
end
def self.nativepluck(input)
out = []
begin
sql = input.respond_to?(:to_sql) ? input.to_sql : input
Nativepluck.set_pg_native_casters
......@@ -90,6 +91,28 @@ module Nativepluck
str
end
module InstanceMethods
def nativepluck2(*column_names)
# Extracted (before modifications) from:
# ruby-2.5.0/gems/activerecord-5.2.1/lib/active_record/relation/calculations.rb
if loaded? && (column_names.map(&:to_s) - @klass.attribute_names - @klass.attribute_aliases.keys).empty?
return records.pluck(*column_names)
end
if has_include?(column_names.first)
relation = apply_join_dependency
relation.pluck(*column_names)
else
enforce_raw_sql_whitelist(column_names)
relation = spawn
relation.select_values = column_names.map { |cn|
@klass.has_attribute?(cn) || @klass.attribute_alias?(cn) ? arel_attribute(cn) : cn
}
Nativepluck.nativepluck(relation)
end
end
end
module ClassMethods
def nativepluck(*columns, **opts)
raise ArgumentError.new('No columns to pluck were provided') if columns.size == 0
......@@ -114,4 +137,13 @@ module Nativepluck
return out
end
end
module ::ActiveRecord
module Calculations
include Nativepluck::InstanceMethods
end
module Querying
delegate :nativepluck2, to: :all
end
end
end
......@@ -14,6 +14,16 @@ class GenericmodelTest < ActiveSupport::TestCase
assert(failed.empty?, "#{__method__}:: pluck != nativepluck for attributes : #{failed.join(', ')}")
end
##########################################################
test 'pluck_nativepluck_2each_attribute' do
failed = []
Genericmodel.attribute_names.each do |attr_name|
pluck = Genericmodel.order(id: :asc).pluck(attr_name)
nativepluck2 = Genericmodel.order(id: :asc).nativepluck2(attr_name)
failed << attr_name if pluck != nativepluck2
end
assert(failed.empty?, "#{__method__}:: pluck != nativepluck2 for attributes : #{failed.join(', ')}")
end
##########################################################
test 'pluck_permutations' do
row_limit = 100
failed = []
......
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'active_record'
require "nativepluck"
require "minitest/autorun"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment