Reduce object allocations and speed up Ransack by

extracting commonly used strings into top level Ransack
constants.
This commit is contained in:
Jon Atack 2014-10-04 00:44:28 +02:00
parent 0ed7ab9d66
commit ea46d4d8de
4 changed files with 37 additions and 41 deletions

View File

@ -1,6 +1,22 @@
require 'ransack/constants'
require 'ransack/predicate'
ASC = 'asc'.freeze
DESC = 'desc'.freeze
ASC_DESC = %w(asc desc).freeze
ASC_ARROW = '▲'.freeze
DESC_ARROW = '▼'.freeze
OR = 'or'.freeze
AND = 'and'.freeze
SORT = 'sort'.freeze
SORT_LINK = 'sort_link'.freeze
SUFFIXES = %w(_any _all).freeze
ATTRIBUTE = 'attribute'.freeze
SEARCH = 'search'.freeze
DEFAULT_SEARCH_KEY = 'q'.freeze
SPACE = ' '.freeze
NON_BREAKING_SPACE = ' '.freeze
module Ransack
module Configuration
@ -24,7 +40,7 @@ module Ransack
self.predicates[name] = Predicate.new(opts)
['_any', '_all'].each do |suffix|
SUFFIXES.each do |suffix|
compound_name = name + suffix
self.predicates[compound_name] = Predicate.new(
opts.merge(

View File

@ -27,7 +27,7 @@ module Ransack
def attribute_select(options = nil, html_options = nil, action = nil)
options = options || {}
html_options = html_options || {}
action = action || 'search'
action = action || SEARCH
default = options.delete(:default)
raise ArgumentError, formbuilder_error_message(
"#{action}_select") unless object.respond_to?(:context)
@ -55,7 +55,7 @@ module Ransack
end
def sort_select(options = {}, html_options = {})
attribute_select(options, html_options, 'sort') +
attribute_select(options, html_options, SORT) +
sort_direction_select(options, html_options)
end
@ -157,14 +157,14 @@ module Ransack
end
def sort_array
[['asc', object.translate('asc')], ['desc', object.translate('desc')]]
[[ASC, object.translate(ASC)], [DESC, object.translate(DESC)]]
end
def combinator_choices
if Nodes::Condition === object
[['or', Translate.word(:any)], ['and', Translate.word(:all)]]
[[OR, Translate.word(:any)], [AND, Translate.word(:all)]]
else
[['and', Translate.word(:all)], ['or', Translate.word(:any)]]
[[AND, Translate.word(:all)], [OR, Translate.word(:any)]]
end
end
@ -231,7 +231,7 @@ module Ransack
end
def formbuilder_error_message(action)
"#{action.sub('search', 'attribute')
"#{action.sub(SEARCH, ATTRIBUTE)
} must be called inside a search FormBuilder!"
end

View File

@ -2,26 +2,6 @@ module Ransack
module Helpers
module FormHelper
def asc
'asc'.freeze
end
def desc
'desc'.freeze
end
def asc_arrow
'▲'.freeze
end
def desc_arrow
'▼'.freeze
end
def non_breaking_space
' '.freeze
end
def search_form_for(record, options = {}, &proc)
if record.is_a?(Ransack::Search)
search = record
@ -48,7 +28,7 @@ module Ransack
"#{search.klass.to_s.underscore}_search",
:method => :get
}
options[:as] ||= 'q'.freeze
options[:as] ||= DEFAULT_SEARCH_KEY
options[:html].reverse_merge!(html_options)
options[:builder] ||= FormBuilder
@ -108,18 +88,18 @@ module Ransack
# if the user didn't specify the sort direction, detect the previous
# sort direction on this field and reverse it
if %w(asc desc).none? { |d| d == new_dir }
if ASC_DESC.none? { |d| d == new_dir }
if existing_sort = search.sorts.detect { |s| s.name == attr_name }
current_dir = existing_sort.dir
end
new_dir =
if current_dir
current_dir == desc ? asc : desc
current_dir == DESC ? ASC : DESC
elsif default_order_is_a_hash
default_order[attr_name] || asc
default_order[attr_name] || ASC
else
default_order || asc
default_order || ASC
end
end
@ -131,14 +111,14 @@ module Ransack
sort_params = sort_params.first if sort_params.size == 1
html_options = args.first.is_a?(Hash) ? args.shift.dup : {}
css = ['sort_link', field_current_dir].compact.join(' ')
html_options[:class] = [css, html_options[:class]].compact.join(' ')
css = [SORT_LINK, field_current_dir].compact.join(SPACE)
html_options[:class] = [css, html_options[:class]].compact.join(SPACE)
query_hash = {}
query_hash[search.context.search_key] = search_params
.merge(:s => sort_params)
options.merge!(query_hash)
options_for_url = params.merge options
options_for_url = params.merge(options)
url =
if routing_proxy && respond_to?(routing_proxy)
@ -157,15 +137,15 @@ module Ransack
def link_name(label_text, dir)
[ERB::Util.h(label_text), order_indicator_for(dir)]
.compact
.join(non_breaking_space)
.join(NON_BREAKING_SPACE)
.html_safe
end
def order_indicator_for(dir)
if dir == asc
asc_arrow
elsif dir == desc
desc_arrow
if dir == ASC
ASC_ARROW
elsif dir == DESC
DESC_ARROW
else
nil
end

View File

@ -36,7 +36,7 @@ module Ransack
def dir=(dir)
dir = dir.try(:downcase)
@dir = %w(asc desc).include?(dir) ? dir : 'asc'
@dir = ASC_DESC.include?(dir) ? dir : ASC
end
end