54
55
56
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/ts_vectors/model.rb', line 54
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("#{attribute} @@ to_tsquery('simple', ?)", values.join(' & '))
else
where('false')
end
}
scope "with_any_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attribute} @@ to_tsquery('simple', ?)", values.join(' | '))
else
where('false')
end
}
scope "without_all_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attribute} @@ (!! to_tsquery('simple', ?))", values.join(' & '))
else
where('false')
end
}
scope "without_any_#{attribute}", lambda { |values|
values = attr.normalize_values(values)
if values.any?
where("#{attribute} @@ (!! to_tsquery('simple', ?))", 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(#{attribute}, to_tsquery('simple', ?)) #{direction}", values.join(' | ')]))
else
order('false')
end
}
define_method(attribute) do
attr.parse_values(read_attribute(attribute))
end
define_method("#{attribute}=") do |values|
values = attr.normalize_values(values)
if values.any?
write_attribute(attribute, values.join(' '))
else
write_attribute(attribute, nil)
end
end
end
|