Skip to content

Commit 807f75e

Browse files
committed
Replaced search query_string eval with a call to send
1 parent 3952967 commit 807f75e

File tree

2 files changed

+82
-86
lines changed

2 files changed

+82
-86
lines changed

app/controllers/protips_controller.rb

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ def liked
6363
end
6464
end
6565

66-
def search
67-
search_params = params.permit(:search)
68-
@context = "search"
69-
query_string, @scope = expand_query(search_params[:search])
70-
facets = top_tags_facet << Protip::Search::Facet.new('suggested-networks', :terms, :networks, [size: 4])
71-
@protips = Protip::Search.new(Protip, Protip::Search::Query.new(query_string), @scope, Protip::Search::Sort.new(:trending_score), facets, search_options).execute
72-
@suggested_networks = suggested_networks
73-
find_a_job_for(@protips)
74-
render :index
75-
end
76-
7766
def topic
7867
topic_params = params.permit(:tags, :page, :per_page)
7968

@@ -398,12 +387,43 @@ def preview
398387
render partial: 'protip', locals: { protip: protip, mode: 'preview', include_comments: false, job: nil }
399388
end
400389

390+
def search
391+
search_params = params.permit(:search)
392+
393+
@context = 'search'
394+
query_string, @scope = expand_query(search_params[:search])
395+
facets = top_tags_facet << Protip::Search::Facet.new('suggested-networks', :terms, :networks, [size: 4])
396+
397+
@protips = Protip::Search.new(
398+
Protip,
399+
Protip::Search::Query.new(query_string),
400+
@scope,
401+
Protip::Search::Sort.new(:trending_score),
402+
facets,
403+
search_options
404+
).execute
405+
@suggested_networks = suggested_networks
406+
find_a_job_for(@protips)
407+
render :index
408+
end
409+
401410
private
411+
412+
def expand_query(query_string)
413+
scopes = []
414+
query = query_string.nil? ? '' : query_string.dup
415+
query.scan(/#([\w\.\-#]+)/).flatten.reduce(query) do |query, tag|
416+
query.slice!("##{tag}")
417+
scopes << Protip::Search::Scope.new(:network, tag)
418+
query
419+
end
420+
[query, scopes.blank? ? nil : scopes.reduce(&:<<)]
421+
end
422+
402423
def lookup_protip
403424
@protip = Protip.find_by_public_id(params.permit(:id)[:id].downcase)
404425
end
405426

406-
private
407427
def choose_protip_layout
408428
if [:show, :random, :new, :edit, :create, :update].include? action_name.to_sym
409429
'protip'
@@ -414,8 +434,6 @@ def choose_protip_layout
414434
end
415435
end
416436

417-
private
418-
419437
def search_options
420438
search_options_params = params.permit(:page, :per_page)
421439

@@ -493,17 +511,6 @@ def private_scope?
493511
params[:scope] == "following"
494512
end
495513

496-
def expand_query(query_string)
497-
scopes = []
498-
query = query_string.nil? ? "" : query_string.dup
499-
query.scan(/#([\w\.\-#]+)/).flatten.reduce(query) do |query, tag|
500-
query.slice!("##{tag}")
501-
scopes << Protip::Search::Scope.new(:network, tag)
502-
query
503-
end
504-
[query, scopes.blank? ? nil : scopes.reduce(&:<<)]
505-
end
506-
507514
def track_discovery
508515
record_event('Discover', what: @context, scope: @scope)
509516
end

lib/search.rb

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,61 @@ def rebuild_index(name = nil)
1111
end
1212
end
1313

14-
def self.included(base)
15-
base.extend(ClassMethods)
16-
end
14+
def self.included(base) ; base.extend(ClassMethods) ; end
1715

1816
class Search
17+
def initialize(context, query=nil, scope=nil, sort=nil, facet=nil, options={})
18+
@context = context
19+
@query = query
20+
@scope = scope
21+
@sort = sort
22+
@facet = facet
23+
@options = failover_strategy.merge(options || {})
24+
end
25+
26+
def execute
27+
query_criteria, filter_criteria, sort_criteria, facets, context = [@query, @scope, @sort, @facet, @context]
28+
29+
@context.tire.search(@options) do
30+
query do
31+
signature = query_criteria.to_tire
32+
method = signature.shift
33+
self.send(method, *signature)
34+
end unless query_criteria.nil? || query_criteria.to_tire.blank?
35+
36+
filter_criteria.to_tire.each do |fltr|
37+
filter *fltr
38+
end unless filter_criteria.nil?
39+
40+
sort { by sort_criteria.to_tire } unless sort_criteria.nil?
41+
42+
ap facets
43+
ap facets.to_tire unless facets.nil?
44+
eval(facets.to_tire) unless facets.nil?
45+
46+
Rails.logger.debug "[search](#{context.to_s}):" + JSON.pretty_generate(to_hash)
47+
end
48+
rescue Tire::Search::SearchRequestFailed, Errno::ECONNREFUSED
49+
if @options[:failover].nil?
50+
raise
51+
else
52+
@options[:failover].limit(@options[:per_page] || 18).offset(((@options[:page] || 1)-1) * (@options[:per_page] || 19))
53+
end
54+
end
55+
56+
def sort_criteria ; @sort ; end
57+
58+
def failover_strategy ; { failover: @context.order('created_at DESC') } ; end
59+
1960
class Scope
2061
def initialize(domain, object)
2162
@domain = domain
2263
@object = object
2364
@filter = to_hash
2465
end
2566

26-
def to_tire
27-
@filter
28-
end
29-
30-
def to_hash
31-
{}
32-
end
67+
def to_tire ; @filter ; end
68+
def to_hash ; {} ; end
3369

3470
def <<(other)
3571
@filter.deep_merge(other.to_tire)
@@ -43,30 +79,23 @@ def initialize(fields, direction = 'desc')
4379
@direction = direction
4480
end
4581

46-
def to_tire
47-
@fields.map { |field| {field => @direction} }
48-
end
82+
def to_tire ; @fields.map { |field| {field => @direction} } ; end
4983
end
5084

5185
class Query
52-
def default_query
53-
''
54-
end
86+
def default_query ; '' ; end
5587

5688
def initialize(query_string, default_operator = 'AND', default_query_string = default_query)
5789
@query_string = default_query_string + ' ' + query_string
5890
@default_operator = default_operator
5991
end
6092

6193
def to_tire
62-
unless @query_string.blank?
63-
"string '#{@query_string}', :default_operator => '#{@default_operator}'"
64-
end
94+
[:string, "#{@query_string}", { default_operator: "#{@default_operator}" }] unless @query_string.blank?
6595
end
6696
end
6797

6898
class Facet
69-
7099
def initialize(name, type, field, options)
71100
@name = name
72101
@type = type
@@ -82,9 +111,7 @@ def to_eval_form
82111
"end"
83112
end
84113

85-
def to_tire
86-
@facet
87-
end
114+
def to_tire ; @facet ; end
88115

89116
def <<(other_facet)
90117
@facet << "\n" << other_facet.to_eval_form
@@ -96,44 +123,6 @@ def <<(other_facet)
96123
def evaluatable_options
97124
', ' + @options.join(', ') unless @options.blank?
98125
end
99-
100-
end
101-
102-
def initialize(context, query=nil, scope=nil, sort=nil, facet=nil, options={})
103-
@context = context
104-
@query = query
105-
@scope = scope
106-
@sort = sort
107-
@facet = facet
108-
@options = failover_strategy.merge(options || {})
109-
end
110-
111-
def sort_criteria
112-
@sort
113-
end
114-
115-
def failover_strategy
116-
{ failover: @context.order('created_at DESC') }
117-
end
118-
119-
def execute
120-
query_criteria, filter_criteria, sort_criteria, facets, context = [@query, @scope, @sort, @facet, @context]
121-
122-
@context.tire.search(@options) do
123-
query { eval query_criteria.to_tire } unless query_criteria.nil? || query_criteria.to_tire.blank?
124-
filter_criteria.to_tire.each do |fltr|
125-
filter *fltr
126-
end unless filter_criteria.nil?
127-
sort { by sort_criteria.to_tire } unless sort_criteria.nil?
128-
eval(facets.to_tire) unless facets.nil?
129-
Rails.logger.debug "[search](#{context.to_s}):" + JSON.pretty_generate(to_hash)
130-
end
131-
rescue Tire::Search::SearchRequestFailed, Errno::ECONNREFUSED
132-
if @options[:failover].nil?
133-
raise
134-
else
135-
@options[:failover].limit(@options[:per_page] || 18).offset(((@options[:page] || 1)-1) * (@options[:per_page] || 19))
136-
end
137126
end
138127
end
139128
end

0 commit comments

Comments
 (0)