Class: Mincer::Processors::PgSearch::Processor

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/mincer/processors/pg_search/processor.rb

Instance Method Summary collapse

Methods included from Helpers

#join_expressions

Constructor Details

#initialize(mincer) ⇒ Processor

Returns a new instance of Processor.



8
9
10
# File 'lib/mincer/processors/pg_search/processor.rb', line 8

def initialize(mincer)
  @mincer, @args, @relation = mincer, mincer.args, mincer.relation
end

Instance Method Details

#applyObject



12
13
14
15
16
17
18
# File 'lib/mincer/processors/pg_search/processor.rb', line 12

def apply
  if Mincer.postgres?
    @relation = apply_pg_search(@relation, @args)
  else
    @relation
  end
end

#apply_pg_search(relation, args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mincer/processors/pg_search/processor.rb', line 20

def apply_pg_search(relation, args)
  rel = relation.where(conditions(args))
  rank = rank(args)
  if rank.blank? || (@mincer.respond_to?(:skip_order_by_rank) && @mincer.skip_order_by_rank)
    rel
  elsif @mincer.default_sorting
    rel.reorder(rank)
  else
    rel.order(rank)
  end
end

#conditions(args) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/mincer/processors/pg_search/processor.rb', line 32

def conditions(args)
  search_statements_conditions = search_statements.map do |search_statement|
    conditions = pg_search_engines(args, search_statement).map do |pg_search_engine|
      pg_search_engine.conditions
    end.compact
    join_expressions(conditions, options[:join_with] || :or)
  end.compact
  join_expressions(search_statements_conditions, options[:join_with] || :or).try(:to_sql)
end

#default_search_statementsObject

We use only text/string columns and avoid array



71
72
73
74
75
76
77
78
# File 'lib/mincer/processors/pg_search/processor.rb', line 71

def default_search_statements
  column_names = @relation.columns.reject do |column|
    ![:string, :text].include?(column.type) || column.array
  end.map do |column|
    "#{@relation.table_name}.#{column.name}"
  end
  [SearchStatement.new(column_names, search_statement_default_options([:fulltext]).merge(engines: [:fulltext]))]
end

#optionsObject



84
85
86
# File 'lib/mincer/processors/pg_search/processor.rb', line 84

def options
  @mincer.send(:pg_search_options)
end

#paramsObject



80
81
82
# File 'lib/mincer/processors/pg_search/processor.rb', line 80

def params
  Array.wrap(@mincer.send(:pg_search_params))
end

#pg_search_engines(args, search_statement) ⇒ Object



53
54
55
56
57
# File 'lib/mincer/processors/pg_search/processor.rb', line 53

def pg_search_engines(args, search_statement)
  Mincer.config.pg_search.engines.map do |engine_class|
    engine_class.new(args, [search_statement])
  end
end

#rank(args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/mincer/processors/pg_search/processor.rb', line 42

def rank(args)
  search_statements_conditions = search_statements.map do |search_statement|
    conditions = pg_search_engines(args, search_statement).map do |pg_search_engine|
      pg_search_engine.rank
    end.compact
    join_expressions(conditions, :+)
  end.compact
  rank = join_expressions(search_statements_conditions, :+).try(:to_sql)
  Arel.sql("#{rank} DESC") if rank.present?
end

#search_statement_default_options(engines) ⇒ Object



88
89
90
91
92
93
# File 'lib/mincer/processors/pg_search/processor.rb', line 88

def search_statement_default_options(engines)
  (engines & [:fulltext, :trigram, :array]).inject({}) do |options, engine|
    options = Mincer.config.pg_search.send("#{engine}_engine").merge(options)
    options
  end
end

#search_statementsObject



59
60
61
# File 'lib/mincer/processors/pg_search/processor.rb', line 59

def search_statements
  @search_statements ||= params.any? { |param| param[:columns] } ? search_statements_from_params : default_search_statements
end

#search_statements_from_paramsObject



63
64
65
66
67
68
# File 'lib/mincer/processors/pg_search/processor.rb', line 63

def search_statements_from_params
  params.map do |param|
    par = param.dup
    SearchStatement.new(par.delete(:columns), search_statement_default_options(param[:engines]).merge(par))
  end
end