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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



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

#attributeObject

Returns the value of attribute attribute.



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

def attribute
  @attribute
end

#default_languageObject

Returns the value of attribute default_language.



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

def default_language
  @default_language
end

#default_modeObject

Returns the value of attribute default_mode.



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

def default_mode
  @default_mode
end

#default_orderObject

Returns the value of attribute default_order.



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

def default_order
  @default_order
end

#default_rankObject

Returns the value of attribute default_rank.



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

def default_rank
  @default_rank
end

#klassObject

Returns the value of attribute klass.



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

def klass
  @klass
end

#klass_moduleObject

Returns the value of attribute klass_module.



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

def klass_module
  @klass_module
end

#optionsObject

Returns the value of attribute options.



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_moduleObject

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



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 "    def \#{scope_name}(value\#{scope_args})\n      attr = arel_table['\#{attribute}']\n      fn = ::Torque::PostgreSQL::FN\n\n      lang = language.to_s if !language.is_a?(::Symbol)\n      lang ||= arel_table[language.to_s] if has_attribute?(language)\n      lang ||= public_send(language) if respond_to?(language)\n\n      function = {\n        default: :to_tsquery,\n        phrase: :phraseto_tsquery,\n        plain: :plainto_tsquery,\n        web: :websearch_to_tsquery,\n      }[mode.to_sym]\n\n      raise ::ArgumentError, <<~MSG.squish if lang.blank?\n        Unable to determine language from \\\#{language.inspect}.\n      MSG\n\n      raise ::ArgumentError, <<~MSG.squish if function.nil?\n        Invalid mode \\\#{mode.inspect} for full text search.\n      MSG\n\n      value = fn.bind(:value, value.to_s, attr.type_caster)\n      lang = fn.bind(:lang, lang, attr.type_caster) if lang.is_a?(::String)\n\n      query = fn.public_send(function, lang, value)\n      ranker = fn.ts_rank(attr, query) if rank || order\n\n      result = where(fn.infix(:\"@@\", attr, query))\n      result = result.order(ranker.public_send(order == :desc ? :desc : :asc)) if order\n      result.select_extra_values += [ranker.as(rank == true ? 'rank' : rank.to_s)] if rank\n      result\n    end\n  RUBY\nend\n", __FILE__, __LINE__ + 1

#buildObject

Create the proper scope



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



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_argsObject

Returns the arguments to be used on the scope



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_nameObject

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



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