Class: Torque::PostgreSQL::Attributes::Builder::FullTextSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/postgresql/attributes/builder/full_text_search.rb,
sig/torque/postgresql/attributes/builder/full_text_search.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attribute, options = {}) ⇒ FullTextSearch

Returns a new instance of FullTextSearch.

Parameters:

  • klass (model)
  • attribute (column_ref)
  • options (Hash[Symbol, untyped]) (defaults to: {})


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 11

def initialize(klass, attribute, options = {})
  @klass = klass
  @attribute = attribute
  @options = options

  @default_rank = options[:with_rank] == true ? 'rank' : options[:with_rank]&.to_s
  @default_mode = options[:mode] || PostgreSQL.config.full_text_search.default_mode

  @default_order =
    case options[:order]
    when :asc, true then :asc
    when :desc then :desc
    else false
    end

  @default_language = options[:language] if options[:language].is_a?(String) ||
    options[:language].is_a?(Symbol)
  @default_language ||= PostgreSQL.config.full_text_search.default_language.to_s
end

Instance Attribute Details

#attribute ⇒ column_ref

Returns the value of attribute attribute.

Returns:

  • (column_ref)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def attribute
  @attribute
end

#default_language ⇒ String, Symbol

Returns the value of attribute default_language.

Returns:

  • (String, Symbol)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def default_language
  @default_language
end

#default_mode ⇒ Symbol

Returns the value of attribute default_mode.

Returns:

  • (Symbol)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def default_mode
  @default_mode
end

#default_order ⇒ :asc, ...

Returns the value of attribute default_order.

Returns:

  • (:asc, :desc, false)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def default_order
  @default_order
end

#default_rank ⇒ String?

Returns the value of attribute default_rank.

Returns:

  • (String, nil)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def default_rank
  @default_rank
end

#klass ⇒ model

Returns the value of attribute klass.

Returns:

  • (model)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def klass
  @klass
end

#klass_module ⇒ Module?

Returns the value of attribute klass_module.

Returns:

  • (Module, nil)


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def klass_module
  @klass_module
end

#options ⇒ Hash[Symbol, untyped]

Returns the value of attribute options.

Returns:

  • (Hash[Symbol, untyped])


8
9
10
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 8

def options
  @options
end

Instance Method Details

#add_scope_to_module ⇒ Object

Creates a class method as the scope that builds the full text search

Returns:

  • (Object)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 57

def add_scope_to_module
  klass_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{scope_name}(value#{scope_args})
      attr = arel_table['#{attribute}']
      fn = ::Torque::PostgreSQL::FN

      lang = language.to_s if !language.is_a?(::Symbol)
      lang ||= arel_table[language.to_s] if has_attribute?(language)
      lang ||= public_send(language) if respond_to?(language)

      function = {
        default: :to_tsquery,
        phrase: :phraseto_tsquery,
        plain: :plainto_tsquery,
        web: :websearch_to_tsquery,
      }[mode.to_sym]

      raise ::ArgumentError, <<~MSG.squish if lang.blank?
        Unable to determine language from \#{language.inspect}.
      MSG

      raise ::ArgumentError, <<~MSG.squish if function.nil?
        Invalid mode \#{mode.inspect} for full text search.
      MSG

      value = fn.bind(:value, value.to_s, attr.type_caster)
      lang = fn.bind(:lang, lang, attr.type_caster) if lang.is_a?(::String)

      query = fn.public_send(function, lang, value)
      ranker = fn.ts_rank(attr, query) if rank || order

      result = where(fn.infix(:"@@", attr, query))
      result = result.order(ranker.public_send(order == :desc ? :desc : :asc)) if order
      result.select_extra_values += [ranker.as(rank == true ? 'rank' : rank.to_s)] if rank
      result
    end
  RUBY
end

#build ⇒ Object

Create the proper scope

Returns:

  • (Object)


50
51
52
53
54
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 50

def build
  @klass_module = Module.new
  add_scope_to_module
  klass.extend klass_module
end

#conflicting? ⇒ Boolean

Just check if the scope name is already defined

Returns:

  • (Boolean)


41
42
43
44
45
46
47
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 41

def conflicting?
  return if options[:force] == true

  if klass.dangerous_class_method?(scope_name)
    raise Interrupt, scope_name.to_s
  end
end

#scope_args ⇒ String

Returns the arguments to be used on the scope

Returns:

  • (String)


97
98
99
100
101
102
103
104
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 97

def scope_args
  args = +''
  args << ", order: #{default_order.inspect}"
  args << ", rank: #{default_rank.inspect}"
  args << ", language: #{default_language.inspect}"
  args << ", mode: :#{default_mode}"
  args
end

#scope_name ⇒ String

What is the name of the scope to be added to the model

Returns:

  • (String)


32
33
34
35
36
37
38
# File 'lib/torque/postgresql/attributes/builder/full_text_search.rb', line 32

def scope_name
  @scope_name ||= [
    options[:prefix],
    :full_text_search,
    options[:suffix],
  ].compact.join('_')
end