Skip to content

Commit 381a83c

Browse files
committed
Freeze strings in array constants and begin moving
from using global constants to frozen strings (cc @avit). It's not pretty, but it's faster and better for understanding the code without having to look up what the special constants represent. From the discussion in #530: "Things are evolving, but with Ruby 2.2 here is my current understanding: - I believe you are correct that the strings inside the array could benefit from freezing, in hot spots. - Freezing a string may now be faster than looking up a frozen string constant. So, it might make sense now in Ransack master and upcoming releases, to optimize for Ruby 2.2+ and stop using frozen constants, and replace them with frozen strings or symbols." Closes #530.
1 parent 5686c5c commit 381a83c

File tree

11 files changed

+57
-67
lines changed

11 files changed

+57
-67
lines changed

lib/ransack/adapters/active_record/3.0/compat.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,16 @@ def visit_Arel_Nodes_NamedFunction o
138138
"#{
139139
o.name
140140
}(#{
141-
o.distinct ? Ransack::Constants::DISTINCT : Ransack::Constants::EMPTY
141+
o.distinct ? Ransack::Constants::DISTINCT : ''.freeze
142142
}#{
143-
o.expressions.map { |x| visit x }.join(Ransack::Constants::COMMA_SPACE)
143+
o.expressions.map { |x| visit x }.join(', '.freeze)
144144
})#{
145-
o.alias ? " AS #{visit o.alias}" : Ransack::Constants::EMPTY
145+
o.alias ? " AS #{visit o.alias}" : ''.freeze
146146
}"
147147
end
148148

149149
def visit_Arel_Nodes_And o
150-
o.children.map { |x| visit x }.join(Ransack::Constants::SPACED_AND)
150+
o.children.map { |x| visit x }.join(' AND '.freeze)
151151
end
152152

153153
def visit_Arel_Nodes_Not o
@@ -164,7 +164,7 @@ def visit_Arel_Nodes_Values o
164164
quote(value, attr && column_for(attr))
165165
end
166166
}
167-
.join(Ransack::Constants::COMMA_SPACE)
167+
.join(', '.freeze)
168168
})"
169169
end
170170
end

lib/ransack/constants.rb

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
module Ransack
22
module Constants
3-
ASC = 'asc'.freeze
4-
DESC = 'desc'.freeze
5-
ASC_DESC = [ASC, DESC].freeze
6-
73
ASC_ARROW = '▲'.freeze
84
DESC_ARROW = '▼'.freeze
95

106
OR = 'or'.freeze
117
AND = 'and'.freeze
12-
SPACED_AND = ' AND '.freeze
13-
14-
SORT = 'sort'.freeze
15-
SORT_LINK = 'sort_link'.freeze
16-
SORT_DIRECTION = 'sort_direction'.freeze
178

189
CAP_SEARCH = 'Search'.freeze
1910
SEARCH = 'search'.freeze
@@ -23,17 +14,12 @@ module Constants
2314
ATTRIBUTES = 'attributes'.freeze
2415
COMBINATOR = 'combinator'.freeze
2516

26-
SPACE = ' '.freeze
27-
COMMA_SPACE = ', '.freeze
28-
COLON_SPACE = ': '.freeze
2917
TWO_COLONS = '::'.freeze
3018
UNDERSCORE = '_'.freeze
3119
LEFT_PARENTHESIS = '('.freeze
3220
Q = 'q'.freeze
3321
I = 'i'.freeze
34-
NON_BREAKING_SPACE = ' '.freeze
3522
DOT_ASTERIX = '.*'.freeze
36-
EMPTY = ''.freeze
3723

3824
STRING_JOIN = 'string_join'.freeze
3925
ASSOCIATION_JOIN = 'association_join'.freeze
@@ -44,14 +30,17 @@ module Constants
4430
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE'].to_set
4531
BOOLEAN_VALUES = (TRUE_VALUES + FALSE_VALUES).freeze
4632

47-
S_SORTS = %w(s sorts).freeze
48-
AND_OR = %w(and or).freeze
49-
IN_NOT_IN = %w(in not_in).freeze
50-
SUFFIXES = %w(_any _all).freeze
51-
AREL_PREDICATES = %w(
52-
eq not_eq matches does_not_match lt lteq gt gteq in not_in
53-
).freeze
54-
A_S_I = %w(a s i).freeze
33+
AND_OR = ['and'.freeze, 'or'.freeze].freeze
34+
IN_NOT_IN = ['in'.freeze, 'not_in'.freeze].freeze
35+
SUFFIXES = ['_any'.freeze, '_all'.freeze].freeze
36+
AREL_PREDICATES = [
37+
'eq'.freeze, 'not_eq'.freeze,
38+
'matches'.freeze, 'does_not_match'.freeze,
39+
'lt'.freeze, 'lteq'.freeze,
40+
'gt'.freeze, 'gteq'.freeze,
41+
'in'.freeze, 'not_in'.freeze
42+
].freeze
43+
A_S_I = ['a'.freeze, 's'.freeze, 'i'.freeze].freeze
5544

5645
EQ = 'eq'.freeze
5746
NOT_EQ = 'not_eq'.freeze

lib/ransack/context.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ def for_object(object, options = {})
1717
end
1818

1919
def for(object, options = {})
20-
context = Class === object ?
21-
for_class(object, options) :
22-
for_object(object, options)
20+
context =
21+
if Class === object
22+
for_class(object, options)
23+
else
24+
for_object(object, options)
25+
end
2326
context or raise ArgumentError,
2427
"Don't know what context to use for #{object}"
2528
end
@@ -59,7 +62,7 @@ def bind(object, str)
5962
end
6063

6164
def traverse(str, base = @base)
62-
str ||= Constants::EMPTY
65+
str ||= ''.freeze
6366

6467
if (segments = str.split(/_/)).size > 0
6568
remainder = []
@@ -68,13 +71,12 @@ def traverse(str, base = @base)
6871
# Strip the _of_Model_type text from the association name, but hold
6972
# onto it in klass, for use as the next base
7073
assoc, klass = unpolymorphize_association(
71-
segments.join(Constants::UNDERSCORE)
74+
segments.join('_'.freeze)
7275
)
7376
if found_assoc = get_association(assoc, base)
7477
base = traverse(
75-
remainder.join(
76-
Constants::UNDERSCORE), klass || found_assoc.klass
77-
)
78+
remainder.join('_'.freeze), klass || found_assoc.klass
79+
)
7880
end
7981

8082
remainder.unshift segments.pop
@@ -88,7 +90,7 @@ def traverse(str, base = @base)
8890

8991
def association_path(str, base = @base)
9092
base = klassify(base)
91-
str ||= Constants::EMPTY
93+
str ||= ''.freeze
9294
path = []
9395
segments = str.split(/_/)
9496
association_parts = []
@@ -131,15 +133,15 @@ def ransackable_scope?(str, klass)
131133
klass.ransackable_scopes(auth_object).any? { |s| s.to_s == str }
132134
end
133135

134-
def searchable_attributes(str = Constants::EMPTY)
136+
def searchable_attributes(str = ''.freeze)
135137
traverse(str).ransackable_attributes(auth_object)
136138
end
137139

138-
def sortable_attributes(str = Constants::EMPTY)
140+
def sortable_attributes(str = ''.freeze)
139141
traverse(str).ransortable_attributes(auth_object)
140142
end
141143

142-
def searchable_associations(str = Constants::EMPTY)
144+
def searchable_associations(str = ''.freeze)
143145
traverse(str).ransackable_associations(auth_object)
144146
end
145147
end

lib/ransack/helpers/form_builder.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def value(object)
1515
RANSACK_FORM_BUILDER = 'RANSACK_FORM_BUILDER'.freeze
1616

1717
require 'simple_form' if
18-
(ENV[RANSACK_FORM_BUILDER] || Ransack::Constants::EMPTY)
19-
.match('SimpleForm'.freeze)
18+
(ENV[RANSACK_FORM_BUILDER] || ''.freeze).match('SimpleForm'.freeze)
2019

2120
module Ransack
2221
module Helpers
@@ -47,7 +46,7 @@ def attribute_select(options = nil, html_options = nil, action = nil)
4746
raise ArgumentError, formbuilder_error_message(
4847
"#{action}_select") unless object.respond_to?(:context)
4948
options[:include_blank] = true unless options.has_key?(:include_blank)
50-
bases = [Constants::EMPTY] + association_array(options[:associations])
49+
bases = [''.freeze].freeze + association_array(options[:associations])
5150
if bases.size > 1
5251
collection = attribute_collection_for_bases(action, bases)
5352
object.name ||= default if can_use_default?(
@@ -66,13 +65,13 @@ def attribute_select(options = nil, html_options = nil, action = nil)
6665
def sort_direction_select(options = {}, html_options = {})
6766
unless object.respond_to?(:context)
6867
raise ArgumentError,
69-
formbuilder_error_message(Constants::SORT_DIRECTION)
68+
formbuilder_error_message('sort_direction'.freeze)
7069
end
7170
template_collection_select(:dir, sort_array, options, html_options)
7271
end
7372

7473
def sort_select(options = {}, html_options = {})
75-
attribute_select(options, html_options, Constants::SORT) +
74+
attribute_select(options, html_options, 'sort'.freeze) +
7675
sort_direction_select(options, html_options)
7776
end
7877

@@ -135,7 +134,7 @@ def predicate_select(options = {}, html_options = {})
135134
else
136135
only = Array.wrap(only).map(&:to_s)
137136
keys = keys.select {
138-
|k| only.include? k.sub(/_(any|all)$/, Constants::EMPTY)
137+
|k| only.include? k.sub(/_(any|all)$/, ''.freeze)
139138
}
140139
end
141140
end

lib/ransack/helpers/form_helper.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def initialize(search, attribute, args, params)
9393
def name
9494
[ERB::Util.h(@label_text), order_indicator]
9595
.compact
96-
.join(Constants::NON_BREAKING_SPACE)
96+
.join(' '.freeze)
9797
.html_safe
9898
end
9999

@@ -106,8 +106,8 @@ def url_options
106106
def html_options(args)
107107
html_options = extract_options_and_mutate_args!(args)
108108
html_options.merge(
109-
class: [[Constants::SORT_LINK, @current_dir], html_options[:class]]
110-
.compact.join(Constants::SPACE)
109+
class: [['sort_link'.freeze, @current_dir], html_options[:class]]
110+
.compact.join(' '.freeze)
111111
)
112112
end
113113

@@ -159,7 +159,7 @@ def detect_previous_sort_direction_and_invert_it(attr_name)
159159
if sort_dir = existing_sort_direction(attr_name)
160160
direction_text(sort_dir)
161161
else
162-
default_sort_order(attr_name) || Constants::ASC
162+
default_sort_order(attr_name) || 'asc'.freeze
163163
end
164164
end
165165

@@ -179,17 +179,17 @@ def order_indicator
179179
end
180180

181181
def no_sort_direction_specified?(dir = @current_dir)
182-
!Constants::ASC_DESC.include?(dir)
182+
!['asc'.freeze, 'desc'.freeze].freeze.include?(dir)
183183
end
184184

185185
def direction_arrow
186-
return Constants::DESC_ARROW if @current_dir == Constants::DESC
186+
return Constants::DESC_ARROW if @current_dir == 'desc'.freeze
187187
Constants::ASC_ARROW
188188
end
189189

190190
def direction_text(dir)
191-
return Constants::ASC if dir == Constants::DESC
192-
Constants::DESC
191+
return 'asc'.freeze if dir == 'desc'.freeze
192+
'desc'.freeze
193193
end
194194
end
195195
end

lib/ransack/nodes/condition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def inspect
224224
]
225225
.reject { |e| e[1].blank? }
226226
.map { |v| "#{v[0]}: #{v[1]}" }
227-
.join(Constants::COMMA_SPACE)
227+
.join(', '.freeze)
228228
"Condition <#{data}>"
229229
end
230230

lib/ransack/nodes/grouping.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def values
6868
def respond_to?(method_id)
6969
super or begin
7070
method_name = method_id.to_s
71-
writer = method_name.sub!(/\=$/, Constants::EMPTY)
71+
writer = method_name.sub!(/\=$/, ''.freeze)
7272
attribute_method?(method_name) ? true : false
7373
end
7474
end
@@ -114,7 +114,7 @@ def groupings=(groupings)
114114

115115
def method_missing(method_id, *args)
116116
method_name = method_id.to_s
117-
writer = method_name.sub!(/\=$/, Constants::EMPTY)
117+
writer = method_name.sub!(/\=$/, ''.freeze)
118118
if attribute_method?(method_name)
119119
if writer
120120
write_attribute(method_name, *args)
@@ -169,7 +169,7 @@ def inspect
169169
]
170170
.reject { |e| e[1].blank? }
171171
.map { |v| "#{v[0]}: #{v[1]}" }
172-
.join(Constants::COMMA_SPACE)
172+
.join(', '.freeze)
173173
"Grouping <#{data}>"
174174
end
175175

lib/ransack/nodes/sort.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ def name=(name)
3838
def dir=(dir)
3939
dir = dir.downcase if dir
4040
@dir =
41-
if Constants::ASC_DESC.include?(dir)
41+
if ['asc'.freeze, 'desc'.freeze].freeze.include?(dir)
4242
dir
4343
else
44-
Constants::ASC
44+
'asc'.freeze
4545
end
4646
end
4747

lib/ransack/predicate.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def names
1010
end
1111

1212
def names_by_decreasing_length
13-
names.sort { |a,b| b.length <=> a.length }
13+
names.sort { |a, b| b.length <=> a.length }
1414
end
1515

1616
def named(name)
@@ -19,7 +19,7 @@ def named(name)
1919

2020
def detect_and_strip_from_string!(str)
2121
if p = detect_from_string(str)
22-
str.sub! /_#{p}$/, Constants::EMPTY
22+
str.sub! /_#{p}$/, ''.freeze
2323
p
2424
end
2525
end

lib/ransack/search.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def result(opts = {})
3737

3838
def build(params)
3939
collapse_multiparameter_attributes!(params).each do |key, value|
40-
if Constants::S_SORTS.include?(key)
40+
if ['s'.freeze, 'sorts'.freeze].freeze.include?(key)
4141
send("#{key}=", value)
4242
elsif base.attribute_method?(key)
4343
base.send("#{key}=", value)
@@ -92,7 +92,7 @@ def new_sort(opts = {})
9292

9393
def method_missing(method_id, *args)
9494
method_name = method_id.to_s
95-
getter_name = method_name.sub(/=$/, Constants::EMPTY)
95+
getter_name = method_name.sub(/=$/, ''.freeze)
9696
if base.attribute_method?(getter_name)
9797
base.send(method_id, *args)
9898
elsif @context.ransackable_scope?(getter_name, @context.object)
@@ -113,8 +113,8 @@ def inspect
113113
[:base, base.inspect]
114114
]
115115
.compact
116-
.map { |d| d.join(Constants::COLON_SPACE) }
117-
.join(Constants::COMMA_SPACE)
116+
.map { |d| d.join(': '.freeze) }
117+
.join(', '.freeze)
118118

119119
"Ransack::Search<#{details}>"
120120
end

0 commit comments

Comments
 (0)