Use Ruby instead of Rails here

and replace Array#second with Array#[1].

Also, Array#[] is faster than Array#first and Array#second:

```
require 'benchmark/ips'

ARRAY = ['test', 2, 3, 4]

Benchmark.ips do |x|
  x.report('Array#[0]') { ARRAY[0] }
  x.report('Array#first') { ARRAY.first }
  x.compare!
end
```
  Array#[0]:  9954444.8 i/s
Array#first:  8092468.5 i/s - 1.23x slower

```
Benchmark.ips do |x|
  x.report('Array#[1]') { ARRAY[1] }
  x.report('Array#second') { ARRAY.second }
  x.compare!
end
```
       Array#[1]: 10052039.6 i/s
Array#second:  6818468.0 i/s - 1.47x slower
This commit is contained in:
Jon Atack 2015-08-29 12:19:18 +02:00
parent f5ee03a5f0
commit bbae652fd0
1 changed files with 5 additions and 5 deletions

View File

@ -64,7 +64,7 @@ module Ransack
end
def extract_search_and_routing_proxy(search)
return [search.second, search.first] if search.is_a?(Array)
return [search[1], search[0]] if search.is_a?(Array)
[search, nil]
end
@ -114,17 +114,17 @@ module Ransack
private
def extract_sort_fields_and_mutate_args!(args)
return args.shift if args.first.is_a?(Array)
return args.shift if args[0].is_a?(Array)
[@field]
end
def extract_label_and_mutate_args!(args)
return args.shift if args.first.is_a?(String)
return args.shift if args[0].is_a?(String)
Translate.attribute(@field, context: @search.context)
end
def extract_options_and_mutate_args!(args)
return args.shift.with_indifferent_access if args.first.is_a?(Hash)
return args.shift.with_indifferent_access if args[0].is_a?(Hash)
{}
end
@ -138,7 +138,7 @@ module Ransack
def sort_params
sort_array = recursive_sort_params_build(@sort_fields)
return sort_array[0] if sort_array.size == 1
return sort_array[0] if sort_array.length == 1
sort_array
end