Constants instead of string allocations for Ruby < 2.1

It costs a bit of code readability but avoids allocating
new string objects repeatedly.
This commit is contained in:
Jon Atack 2014-11-20 23:56:41 +01:00
parent 69afc02393
commit a33ace336d
5 changed files with 36 additions and 16 deletions

View File

@ -79,7 +79,7 @@ module Ransack
end
end
if ::ActiveRecord::VERSION::STRING >= '4.1'.freeze
if ::ActiveRecord::VERSION::STRING >= Ransack::Constants::RAILS_4_1
def join_associations
raise NotImplementedError,
@ -213,14 +213,14 @@ module Ransack
join_dependency.alias_tracker.aliases[join.left.name.downcase] = 1
end
if ::ActiveRecord::VERSION::STRING >= '4.1'.freeze
if ::ActiveRecord::VERSION::STRING >= Ransack::Constants::RAILS_4_1
join_dependency
else
join_dependency.graft(*stashed_association_joins)
end
end
if ::ActiveRecord::VERSION::STRING >= '4.1'.freeze
if ::ActiveRecord::VERSION::STRING >= Ransack::Constants::RAILS_4_1
def build_or_find_association(name, parent = @base, klass = nil)
found_association = @join_dependency.join_root.children

View File

@ -8,12 +8,19 @@ module Ransack
AND = 'and'.freeze
SORT = 'sort'.freeze
SORT_LINK = 'sort_link'.freeze
CAP_SEARCH = "Search".freeze
SEARCH = 'search'.freeze
SEARCHES = 'searches'.freeze
ATTRIBUTE = 'attribute'.freeze
COMBINATOR = 'combinator'.freeze
SPACE = ' '.freeze
COMMA_SPACE = ', '.freeze
COLON_SPACE = ': '.freeze
TWO_COLONS = '::'.freeze
UNDERSCORE = '_'.freeze
LEFT_PARENTHESIS = '('.freeze
Q = 'q'.freeze
I = 'i'.freeze
NON_BREAKING_SPACE = '&nbsp;'.freeze
EMPTY = ''.freeze
@ -34,11 +41,13 @@ module Ransack
AREL_PREDICATES = %w(
eq not_eq matches does_not_match lt lteq gt gteq in not_in
).freeze
A_S_I = %w(a s i).freeze
EQ = 'eq'.freeze
NOT_EQ = 'not_eq'.freeze
EQ_ANY = 'eq_any'.freeze
NOT_EQ_ALL = 'not_eq_all'.freeze
RAILS_4_1 = '4.1'.freeze
end
end

View File

@ -28,15 +28,15 @@ module Ransack
alias_method :cache_key, :collection
def initialize
super("Search".freeze)
@singular = "search".freeze
@plural = "searches".freeze
@element = "search".freeze
@human = "Search".freeze
super(Ransack::Constants::CAP_SEARCH)
@singular = Ransack::Constants::SEARCH
@plural = Ransack::Constants::SEARCHES
@element = Ransack::Constants::SEARCH
@human = Ransack::Constants::CAP_SEARCH
@collection = "ransack/searches".freeze
@partial_path = "#{@collection}/#{@element}".freeze
@param_key = "q".freeze
@route_key = "searches".freeze
@param_key = Ransack::Constants::Q
@route_key = Ransack::Constants::SEARCHES
@i18n_key = :ransack
end
end
@ -51,4 +51,4 @@ module Ransack
end
end
end
end

View File

@ -113,7 +113,7 @@ module Ransack
([:scope, @scope_args] if @scope_args.present?),
[:base, base.inspect]
]
.compact.map { |d| d.join(': '.freeze) }
.compact.map { |d| d.join(Ransack::Constants::COLON_SPACE) }
.join(Ransack::Constants::COMMA_SPACE)
"Ransack::Search<#{details}>"
@ -146,15 +146,24 @@ module Ransack
def collapse_multiparameter_attributes!(attrs)
attrs.keys.each do |k|
if k.include?('('.freeze)
if k.include?(Ransack::Constants::LEFT_PARENTHESIS)
real_attribute, position = k.split(/\(|\)/)
cast = %w(a s i).freeze.include?(position.last) ? position.last : nil
cast =
if Ransack::Constants::A_S_I.include?(position.last)
position.last
else
nil
end
position = position.to_i - 1
value = attrs.delete(k)
attrs[real_attribute] ||= []
attrs[real_attribute][position] =
if cast
value.blank? && cast == 'i'.freeze ? nil : value.send("to_#{cast}")
if value.blank? && cast == Ransack::Constants::I
nil
else
value.send("to_#{cast}")
end
else
value
end

View File

@ -54,7 +54,9 @@ module Ransack
DISPATCH = Hash.new do |hash, klass|
hash[klass] = "visit_#{
klass.name.gsub('::'.freeze, Ransack::Constants::UNDERSCORE)
klass.name.gsub(
Ransack::Constants::TWO_COLONS, Ransack::Constants::UNDERSCORE
)
}"
end