From bbae652fd0f4b14c2720b3652d2f3b124e428b49 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Sat, 29 Aug 2015 12:19:18 +0200 Subject: [PATCH] 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 --- lib/ransack/helpers/form_helper.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/ransack/helpers/form_helper.rb b/lib/ransack/helpers/form_helper.rb index 39ea94a..f17c617 100644 --- a/lib/ransack/helpers/form_helper.rb +++ b/lib/ransack/helpers/form_helper.rb @@ -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