Class: Mincer::Processors::PgSearch::Processor
- Inherits:
-
Object
- Object
- Mincer::Processors::PgSearch::Processor
- Includes:
- Helpers
- Defined in:
- lib/mincer/processors/pg_search/processor.rb
Instance Method Summary collapse
- #apply ⇒ Object
- #apply_pg_search(relation, args) ⇒ Object
- #conditions(args) ⇒ Object
-
#default_search_statements ⇒ Object
We use only text/string columns and avoid array.
-
#initialize(mincer) ⇒ Processor
constructor
A new instance of Processor.
- #options ⇒ Object
- #params ⇒ Object
- #pg_search_engines(args, search_statement) ⇒ Object
- #rank(args) ⇒ Object
- #search_statement_default_options(engines) ⇒ Object
- #search_statements ⇒ Object
- #search_statements_from_params ⇒ Object
Methods included from Helpers
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
#apply ⇒ Object
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, [:join_with] || :or) end.compact join_expressions(search_statements_conditions, [:join_with] || :or).try(:to_sql) end |
#default_search_statements ⇒ Object
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, ([:fulltext]).merge(engines: [:fulltext]))] end |
#options ⇒ Object
84 85 86 |
# File 'lib/mincer/processors/pg_search/processor.rb', line 84 def @mincer.send(:pg_search_options) end |
#params ⇒ Object
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 (engines) (engines & [:fulltext, :trigram, :array]).inject({}) do |, engine| = Mincer.config.pg_search.send("#{engine}_engine").merge() end end |
#search_statements ⇒ Object
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_params ⇒ Object
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), (param[:engines]).merge(par)) end end |