10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/ts_vectors/model.rb', line 10
def ts_vector(attribute, options = {})
attr = Attribute.new(attribute, options)
@ts_vectors ||= {}
@ts_vectors[attribute] = attr
scope "with_all_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attr.format_operand} @@ #{attr.format_query}", values.join(' & '))
else
where('false')
end
}
scope "with_any_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attr.format_operand} @@ #{attr.format_query}", values.join(' | '))
else
where('false')
end
}
scope "without_all_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attr.format_operand} @@ (!! #{attr.format_query})", values.join(' & '))
else
where('false')
end
}
scope "without_any_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attr.format_operand} @@ (!! #{attr.format_query})", values.join(' | '))
else
where('false')
end
}
scope "order_by_#{attribute}_rank", lambda { |values, direction = nil|
direction = 'DESC' unless %w(asc ascending desc descending).include?(direction.try(:downcase))
values = attr.normalize_values(values)
if values.any?
order(sanitize_sql_array([
"ts_rank(#{attr.format_operand}, #{attr.format_query}) #{direction}", values.join(' | ')]))
else
order('false')
end
}
define_method(attribute) do
attr.parse_values(read_attribute(attribute))
end
define_method("#{attribute}=") do |values|
write_attribute(attribute, attr.serialize_values(
attr.normalize_values(values)))
end
end
|